[Haskell-beginners] [Haskell-Beginners] Just

Daniel Fischer daniel.is.fischer at googlemail.com
Tue Jun 14 17:36:27 CEST 2011


On Tuesday 14 June 2011, 17:16:12, Michael Xavier wrote:
> I'm someone will offer a much more eloquent answer after me but here's
> my shot as a fellow beginner:
> 
> The type Maybe is defined:
> 
> data Maybe a = Just a | Nothing

Minor correction:

data Maybe a = Nothing | Just a

which makes a difference for the derived Ord instance (Nothing < Just 
whatever instead of the other way round).

> 
> It means that a value of type Maybe String can manifest in 1 of 2
> values: either Nothing, which typically signifies an error has occurred

"error" is too strong.
Maybe a as a result type signifies that a computation may produce a result 
or not (e.g. an integer may have an integral k-th root or not), it's quite 
useful if you're doing the same computation for many arguments, you do the 
next step for those which produce a (Just something).
As an argument type, it's a way to indicate whether to use some default or 
a caller-supplied value.

> or a "null" value that you'd see in other programming languages.
> 
> Just "foo" is a value that represent a value that is *not* nothing, such
> as a successful result from a computation. You can pattern match to get
> the value out of it:
> 
> case somethingThatProducesAMaybe of
>   Just success -> doStuff success
>   Nothing -> error "Oh the humanity!"
> 
> I hope that helps.



More information about the Beginners mailing list