[Haskell-cafe] Parse text difficulty

Keith Wansbrough Keith.Wansbrough at cl.cam.ac.uk
Thu Dec 9 07:58:09 EST 2004


> numLines :: [Line] -> [(Int, Line)]
> numLines lines                       -- list of pairs of                
>  = zip [1 .. length lines] lines     -- line no. & line
> 

zip stops when it reaches the end of the shorter list, so you can just say

  zip [1 ..] lines

In fact, most programmers use the infix version of zip, like this:

  [1..] `zip` lines

which is nicely readable.  (any function can be turned into an infix by surrounding it in `backticks`).

--KW 8-)
-- 
Keith Wansbrough <kw217 at cl.cam.ac.uk>
http://www.cl.cam.ac.uk/users/kw217/
University of Cambridge Computer Laboratory.



More information about the Haskell-Cafe mailing list