How to extract from Maybe & Either in GHCi

I've got some values in GHCi that look like Just (Left (Blah [stuff])) and I'd like to pull out the (Blah [stuff]) to operate on it. Is there a way to do this directly in GHCi without writing a helper function? thanks Lee Short

Use the maybe function from Data.Maybe.
maybe :: b -> (a->b) -> Maybe b -> b
On 13 April 2011 23:14,
I've got some values in GHCi that look like
Just (Left (Blah [stuff]))
and I'd like to pull out the (Blah [stuff]) to operate on it. Is there a way to do this directly in GHCi without writing a helper function?
thanks Lee Short
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners

On Wed, Apr 13, 2011 at 5:14 PM,
I've got some values in GHCi that look like
Just (Left (Blah [stuff]))
and I'd like to pull out the (Blah [stuff]) to operate on it. Is there a way to do this directly in GHCi without writing a helper function?
Pattern matching works in ghci:
let x = Just (Left (Blah [stuff])) let (Just (Left (Blah y)) = x
Antoine
thanks Lee Short
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners

On 14 April 2011 00:52,
Pattern matching works in ghci:
let x = Just (Left (Blah [stuff]))
let (Just (Left (Blah y)) = x
Thanks! That's exactly what I'm looking for.
FWIW there is also Data.Either which has the either function. λ> :t either either :: (a -> c) -> (b -> c) -> Either a b -> c Quite useful sometimes.

Christopher Done
FWIW there is also Data.Either which has the either function.
Only few people seem to realize that 'maybe' and 'either' exist and how useful they are. They are defined in the Prelude, so no imports are necessary. either this that . maybe defValue foo $ someMaybeEitherValue They also help with monadic code: getStuff :: IO (Maybe String) getStuff >>= maybe (throwIO (MyError "Stuff not available")) putStrLn Greets, Ertugrul -- nightmare = unsafePerformIO (getWrongWife >>= sex) http://ertes.de/

On Thursday 14 April 2011 00:14:19, blackcat@pro-ns.net wrote:
I've got some values in GHCi that look like
Just (Left (Blah [stuff]))
and I'd like to pull out the (Blah [stuff]) to operate on it. Is there a way to do this directly in GHCi without writing a helper function?
I'm not quite sure what you want, you can bind the Blah [stuff] to a name using a pattern-match, perhaps that's what you want. After the fact: ghci> let Just (Left blah) = it ghci> doSomethingWith blah or ghci> case it of Just (Left blah) -> doSomethingWith blah Before: replace 'it' with the expression generating Just (Left (Blah [stuff])).
participants (6)
-
Antoine Latter
-
Benjamin Edwards
-
blackcat@pro-ns.net
-
Christopher Done
-
Daniel Fischer
-
Ertugrul Soeylemez