[Haskell-cafe] Line noise

Luke Palmer lrpalmer at gmail.com
Mon Sep 22 15:00:12 EDT 2008

On Sun, Sep 21, 2008 at 1:10 PM, Andrew Coppin
<andrewcoppin at btinternet.com> wrote:
> I posted a snippet of code which included the phrase
>  mapM_ (\(n,v) -> putStrLn $ "[" ++ show n ++ "] = " ++ show v) (zip [0..]
> vs)
> To somebody familiar with Haskell, that is as clear as day. But to a
> newbie... well *you* try explaining that you start at the left end, take the
> thing in brackets, process it from left to right, then take the other thing
> in brackets, process the right side of it, then pass that to putStrLn, oh,
> but that thing right at the left edge is the argument list, and then pass
> the whole lot to mapM_, which is... are you still with me??
> As one experienced C++ programmer put it, "there is no clear flow from left
> to right or right to left".

This is actually one of the two main metrics I use when I'm writing
code -- I try to keep the information content in my code local, so you
don't have to scan across the screen to see what n corresponds to.
This is equivalent to maintaining a left-to-right or right-to-left
flow (and really which direction it is depends more on reading style
than writing style).

The other one I picked up from Perl of all places, and that is end
weight.  In English we tend to put the important parts (the "essence")
of the sentence at the front, and leave the details for the end.  So I
like to emulate that in Haskell when I can.  There is little I loathe
more than the style:

   map (\x -> ...
         ...) xs

As such, the probability that I would have written the mapM_ line
above is very low, since it violates both of these.  In this case,
switching to forM_ brings it much closer to my style:

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

That is because the details of this sentence are in the function, so I
put it at the end.  Also the corresponding [0..] and n are close
together, as are the corresponding vs and v.


More information about the Haskell-Cafe mailing list