
What does the word "Just" mean in Haskell code? What is its purpose? Regards _John Sampson_

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

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@haskell.org http://www.haskell.org/mailman/listinfo/beginners

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
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@haskell.org http://www.haskell.org/mailman/listinfo/beginners
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners
-- -- Regards, KC

KC
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?
That would be misleading. First of all, think in terms of Maybe, not in terms of Just. "Just 3" is a value of type Maybe Integer just like "True" is a value of type Bool. There is really nothing special about it. Think of Maybe as a list type, which can contain at most one element. In that sense [] corresponds Nothing and [3] corresponds to Just 3. You can pattern-match against both of them in the same way: case maybeValue of Nothing -> "We got nothing." Just _ -> "We got a value." case listValue of [] -> "We got nothing." [_] -> "We got one value." [_,_] -> "We got two values." _ -> "We got more than two values." So why is it misleading? Because (not considering that everything is pure in Haskell) there is nothing impure about Maybe. Greets, Ertugrul -- nightmare = unsafePerformIO (getWrongWife >>= sex) http://ertes.de/

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.

Michael Xavier wrote:
You can pattern match to get the value out of it...
That is certainly true. But I find that I rarely do that, because we have so many nice functions for using Maybe values. The two most common and simplest are already enough that you almost never need to pattern match a Maybe: maybe :: a -> (b -> a) -> Maybe b -> a This applies a function to the value inside Just, or gives a default value when it's a Nothing. fromMaybe :: a -> Maybe a -> a -- from Data.Maybe Like "maybe", but just gives you the value inside the Just without applying a function. There are so many others. Regards, Yitz

"Just" is a context adder to a pure value; so now the value can exist
or be "Nothing".
data Maybe a = Just a | Nothing
The Maybe data type is an instance of monad to implement its behaviour.
Without the Maybe type one would have to use the output value of a
function to signal an error; e.g. -1; so the Maybe type makes code a
lot cleaner.
On Tue, Jun 14, 2011 at 8:00 AM, John Sampson
What does the word "Just" mean in Haskell code? What is its purpose?
Regards
_John Sampson_
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners
-- -- Regards, KC
participants (7)
-
Daniel Fischer
-
Elvio Toccalino
-
Ertugrul Soeylemez
-
John Sampson
-
KC
-
Michael Xavier
-
Yitzchak Gale