Jake
Jake

Reputation: 2917

Haskell data type pattern matching

Lets say you have

data SS=
SSliteral Value

and

data Value=
SSint Int

Now lets say you have n which is of type SS. You want to get the Int value of SS, how would you go about doing so?

Upvotes: 1

Views: 2099

Answers (2)

alternative
alternative

Reputation: 13042

We define with record syntax:

data SS = SSliteral {
    ssValue :: Value
    }

data Value = SSint {
    ssInt :: Int
}

now we define

getIt :: SS -> Int
getIt = ssInt . ssValue

And now we are point-free.

Upvotes: 1

edon
edon

Reputation: 722

You pattern match on n.


getIt :: SS -> Int
getIt (SSliteral (SSint x)) = x 

I suggest you read lyah.

Upvotes: 11

Related Questions