[Haskell-cafe] Re: Line noise

Stefan Monnier monnier at iro.umontreal.ca
Wed Sep 24 12:04:22 EDT 2008


>  qsort [] = []
>  qsort (x:xs) = qsort (filter (x <) xs) ++ [x] ++ qsort (filter (x >=) xs)

Note that you can help the reader by making the precedence a bit more
obvious:

 qsort [] = []
 qsort (x:xs) = qsort (filter (x <) xs)
                ++ [x]
                ++ qsort (filter (x >=) xs)

I find that my students can understand this much better than the
1½ liner.  Regarding the mapM_ example:

> mapM_ (\(n,v) -> putStrLn $ "[" ++ show n ++ "] = " ++ show v) (zip [0..] vs)
>
> To somebody familiar with Haskell, that is as clear as day.

I guess I'm not familiar with Haskell, because I find the paren-counting
to be difficult to do in my head (and god knows that I'm familiar with
parenthese).

Also, I tend to shy away from ($) and (.) and to only use them when
I have a list of functions to compose/apply and want to avoid growing
the depth of my AST with the length of the list.  So I'd probably write
the mapM_ example as:

 mapM_ (\(n,v) ->
         putStrLn ("[" ++ show n ++ "] = " ++ show v))
       (zip [0..] vs)

which I find a lot easier to read.


        Stefan



More information about the Haskell-Cafe mailing list