[Haskell-cafe] Re: [Haskell] [newbye] 'Just a'

Graham Klyne GK at ninebynine.org
Mon Feb 7 10:08:56 EST 2005


[I've switched my response to the Haskell-cafe list]

What do you intend if both (get a) and (get b) return Nothing?

You might also be interested in the library function 'maybe':
   http://www.haskell.org/onlinereport/standard-prelude.html#$vmaybe

or maybe (sic) Maybe.fromMaybe in:
   http://www.haskell.org/onlinereport/maybe.html

Using one of these, I think your function could reasonably be expressed as 
a simple one-liner.

#g
--

At 19:33 04/02/05 +0100, Yuri D'Elia wrote:
>Hi all. I'm experimenting with haskell and its type system.
>I've done a function which scans a list, and returns "Just a"
>value if the element is found, or Nothing.
>
>get :: a -> Maybe a
>
>and implemented
>
>getAorB :: Maybe a -> Maybe a -> a
>getAorB a b = ...
>
>a function which returns "Just (get a)" or "Just (get b)" if (get a) is
>Nothing (excluding failures in both). By now, I've implemented it in
>terms of pattern matching:
>
>getAorB a b = f (get a) (get b)
>     where f (Just a) Nothing = a
>           f Nothing (Just a) = a
>
>but I'd like to know if there are other possible ways to do it, possibly
>without enforcing an evaluation order like pattern matching does.
>
>_______________________________________________
>Haskell mailing list
>Haskell at haskell.org
>http://www.haskell.org/mailman/listinfo/haskell

------------
Graham Klyne
For email:
http://www.ninebynine.org/#Contact



More information about the Haskell-Cafe mailing list