[Haskell-beginners] [Haskell-Beginners] Just

KC kc1956 at gmail.com
Tue Jun 14 18:00:11 CEST 2011


> From a more technical point of view, what 'Just' does is wrapping data.
> Whenever you see 'Just', keep in mind you're looking at a data
> constructor.

Would it be more correct to say that "Just" wraps a pure value?


On Tue, Jun 14, 2011 at 8:35 AM, Elvio Toccalino
<elviotoccalino at gmail.com> wrote:
> I'll add a little to Michael's comment:
>
> If you've done C/C++ maybe you're used to return the result of your
> computation in a value-result argument, and signal failure with an extra
> argument or by returning zero (or -1, or whatnot) in the function's
> return... well, if you don't care why your computation failed (or, more
> likely in Haskell, there's only one way it could have failed), then
> returning the result of the computation wrapped in Maybe is your way to
> go.
>
> From a more technical point of view, what 'Just' does is wrapping data.
> Whenever you see 'Just', keep in mind you're looking at a data
> constructor. The same kind of constructor you could define yourself.
> It's not a special keyword in the language, or anything like that.
> The _type_ is Maybe, and its main purpose is to signal failure or
> success (whatever that means).
>
> On Tue, 2011-06-14 at 08:16 -0700, 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
>>
>>
>> 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 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.
>> --
>> Michael Xavier
>> http://www.michaelxavier.net
>>
>> _______________________________________________
>> Beginners mailing list
>> Beginners at haskell.org
>> http://www.haskell.org/mailman/listinfo/beginners
>
>
>
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://www.haskell.org/mailman/listinfo/beginners
>



-- 
--
Regards,
KC



More information about the Beginners mailing list