
[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@haskell.org http://www.haskell.org/mailman/listinfo/haskell
------------ Graham Klyne For email: http://www.ninebynine.org/#Contact

The recent post of Graham Klyne (below) reminds me that I have meant to ask: is there a ``top 20'' things a serious programmer should know when writing code in Haskell? Of course there is a lot of programming language theory that would be great to know, but I mean really down-to-earth things like the 2 items below (module Maybe, the 'maybe' function). The Haskell libraries are quite large, and it is unrealistic to try to get familiar with all of them right away. But getting a ``small'' list would be very useful - I think of this as step 2 after one learns to get comfortable with a language. I had done this (for Maple) for training new hires at Maplesoft, and I definitely noticed that they became more idiomatic programmers faster this way. Jacques PS: of course, this could already exist on haskell.org and/or the Wiki, but not in an 'obvious' enough place as I missed it... -----Original Message----- From: haskell-cafe-bounces@haskell.org [mailto:haskell-cafe-bounces@haskell.org] On Behalf Of Graham Klyne Sent: February 7, 2005 10:09 AM To: Yuri D'Elia; haskell-cafe@haskell.org Subject: [Haskell-cafe] Re: [Haskell] [newbye] 'Just a' 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 #g --

It's not exactly what you ask for, but I wrote down some of the things I learned in my early days with Haskell: http://www.ninebynine.org/Software/Learning-Haskell-Notes.html #g -- At 10:31 07/02/05 -0500, Jacques Carette wrote:
The recent post of Graham Klyne (below) reminds me that I have meant to ask: is there a ``top 20'' things a serious programmer should know when writing code in Haskell? Of course there is a lot of programming language theory that would be great to know, but I mean really down-to-earth things like the 2 items below (module Maybe, the 'maybe' function).
The Haskell libraries are quite large, and it is unrealistic to try to get familiar with all of them right away. But getting a ``small'' list would be very useful - I think of this as step 2 after one learns to get comfortable with a language. I had done this (for Maple) for training new hires at Maplesoft, and I definitely noticed that they became more idiomatic programmers faster this way.
Jacques
PS: of course, this could already exist on haskell.org and/or the Wiki, but not in an 'obvious' enough place as I missed it...
-----Original Message----- From: haskell-cafe-bounces@haskell.org [mailto:haskell-cafe-bounces@haskell.org] On Behalf Of Graham Klyne Sent: February 7, 2005 10:09 AM To: Yuri D'Elia; haskell-cafe@haskell.org Subject: [Haskell-cafe] Re: [Haskell] [newbye] 'Just a'
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
#g --
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
------------ Graham Klyne For email: http://www.ninebynine.org/#Contact

On Mon, 07 Feb 2005 15:08:56 +0000, Graham Klyne wrote:
[I've switched my response to the Haskell-cafe list]
I'm following all lists through gmane, hoping the post will work. I've received a double-ack first from gmane and then from the mailing-list manager.
What do you intend if both (get a) and (get b) return Nothing?
In a proper function I'd specify a default value, I guess. My intent was to explicitly remove the Maybe type from the data (no real applications here).
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.
Thanks for the tips everyone.
participants (3)
-
Graham Klyne
-
Jacques Carette
-
Yuri D'Elia