|
|
|
|
|
by danharaj
2573 days ago
|
|
Ah I see, so what you're talking about is the syntax for defining a sum type. This confused me too. In Haskell, a definition of a sum type looks something like: data MaybeInteger = Nothing | Just Integer This defines the type MaybeInteger by saying how to construct the values that inhabit it. `Nothing` is a value that is immediately of type MaybeInteger. `Just` is a way to construct a value of type MaybeInteger by feeding it an Integer. You use `Just` to construct a value by applying it to an Integer: `Just 4`, for example. There is an alternative syntax for defining sum types that might be clearer: data MaybeInteger where
Nothing :: MaybeInteger
Just :: Integer -> MaybeInteger
When using sum types, we use a case statement: case x of
Nothing -> "nothing"
Just 1 -> "one"
Just _ -> "something else"
|
|
How does that work, e.g. how does MaybeInteger is equivalent to Integer -> MaybeInteger? I guess ending up at the same type is crucial, but one looks to be the type directly, while the other a constructor for the type.
And how can "Just" be a constructor for MaybeInteger here, but e.g. MaybeString in some other example? Is it specially blessed? What kind of thing it is in the language?