[Haskell-cafe] First time haskell - parse error!

Ketil Malde ketil at malde.org
Wed Mar 10 02:47:00 EST 2010


"S. Doaitse Swierstra" <doaitse at cs.uu.nl> writes:

>> 	then (s1 ++ s2 ++ s3 ++ s4) where
>> 		s1 = "Golds "
>> 		s2 = show (gold s g)
>> 		s3 = ", Silvers "
>> 		s4 = show (silver s g)

> If you want to keep the definitions local to the expression you should write

..but I think it is better style to avoid this kind of one-off named
values.  I much prefer:

         then "Golds "++show (gold s g)++...

For some reason, this is a style isse that doesn't get much attention,
at least not in the non-functional language tradition, where temporary
variables are scattered all over.   So instead of doing:

   let ns y = not (isSpace y)
       f x = takeWhile ns x
   in map f

We can use anonymous functions in place of the uninformatively named
ones: 

   map (\x -> takeWhile (\y -> not (isSpace y)) x)

and use partial application toward point-free-ness:

   map (takeWhile (not . isSpace))

which IMO is a lot easier to read, taking up less screen and mind estate.

Of course it's possible to overdo the point-free thing (commonly
referred to as "pointless"), but I think it's great when you can
eliminate gratuitous naming like this. 

-k
-- 
If I haven't seen further, it is by standing in the footprints of giants


More information about the Haskell-Cafe mailing list