[Haskell-beginners] What is the best practice for code]
legajid
legajid at free.fr
Mon Nov 9 16:46:19 EST 2009
Hello,
i wanted to write a program that searches for all combinations of some
numbers, the sum of which is a given value.
So, i started writing my program, creating a function for each separate
phase : creating list of triples, selecting valuable ones, filtering the
result.
Looking at my code, i've reduced it several ways; the last version holds
on one single line of code.
Please, from the 3 versions i established, which one is " better"? What
are the criterias of a "good" code ?
What about using many anonymous functions?
I think there are other solutions than those i propose.
Following is my code
{- First solution -}
nombres=[9,8..1]
-- all combinations
ftoutes xx = [(x, y, z) | x <- xx, y <- xx, z <- xx]
-- keep valuable ones
futiles xyz = [(x, y, z) | (x,y,z) <- xyz, y < x, z < y ]
-- filter
f_flt (x,y, z) = (x+y+z) == 19
-- final result
f = filter (f_flt) (futiles (ftoutes nombres ))
{- Second solution -}
futiles2 xx = [(x, y, z) | x <- xx, y <- xx, z <- xx, y < x, z < y]
f2 = filter (\(x,y,z) -> (x+y+z)==19) (futiles2 nombres )
{- Third solution -}
f3 = filter (\(x,y,z) -> (x+y+z)==19) ((\ xx -> [(x, y, z) | x <- xx, y
<- xx, z <- xx, y < x, z < y]) nombres )
Thanks for your advice
Didier.
More information about the Beginners
mailing list