[Haskell-cafe] let and fixed point operator
Chaddaï Fouché
chaddai.fouche at gmail.com
Thu Aug 30 14:59:57 EDT 2007
Another interesting example of the x = f x use :
coins = [1,2,5,10,20,50,100,200]
beautiful = foldl (\without p ->
let (poor,rich) = splitAt p without
with = poor ++
zipWith (++) (map (map (p:)) with)
rich
in with
) ([[]] : repeat [])
I don't remember who wrote this code (I rewrote it from memory since
it impressed me quite a bit), but it's a very fast and beautiful (in
my eyes at least) solution to the "menu" problem :
(beautiful coins !! 200) would give you all the set of coins you could
use to pay for 200, in less than 40ms on my computer...
But, even more trivial... You use this all the time when you define
recursive function, you know ? You would need to add a "rec" keyword
to the language if you disallowed this.
Myself I'm a big fan of the point-free style (to a limit) and find
that it scale very well indeed when you begin to name the combination
of functions you want to use.
--
Jedaï
More information about the Haskell-Cafe
mailing list