Programming style (or: too clever by half?)

I just spotted a possible idiom for something that seems to pop up from time to time: foldr const But I can't help feeling that this code is perversely obscure. Does it have any advantages over the more obvious form suggested below? [[ headOrSomething = foldr const headOrSomething1 _ (x:_) = x headOrSomething1 x [] = x h = headOrSomething -- h = headOrSomething1 test1 = h 0 [1,2,3] == 1 test2 = h 0 [] == 0 test3 = h 0 (repeat 42) == 42 ]] This suggests, e.g., an alternative coding of Maybe.listToMaybe: [[ altListToMaybe = foldr (const . Just) Nothing ]] Opinions? #g ------------ Graham Klyne For email: http://www.ninebynine.org/#Contact

On Wed, Nov 12, 2003 at 02:54:32PM +0000, Graham Klyne wrote:
I just spotted a possible idiom for something that seems to pop up from time to time:
foldr const
But I can't help feeling that this code is perversely obscure.
Clever. I usually end up with something like listToMaybe list `orMaybe` default orMaybe (Just x) _ = x orMaybe Nothing y = y Andrew

this is the same as the function 'fromMaybe' in Data.Maybe. the 'maybe' function in that library is also incredibly useful. On Wed, 12 Nov 2003, Andrew Pimlott wrote:
On Wed, Nov 12, 2003 at 02:54:32PM +0000, Graham Klyne wrote:
I just spotted a possible idiom for something that seems to pop up from time to time:
foldr const
But I can't help feeling that this code is perversely obscure.
Clever. I usually end up with something like
listToMaybe list `orMaybe` default
orMaybe (Just x) _ = x orMaybe Nothing y = y
Andrew _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
-- Hal Daume III | hdaume@isi.edu "Arrest this man, he talks in maths." | www.isi.edu/~hdaume
participants (3)
-
Andrew Pimlott
-
Graham Klyne
-
Hal Daume III