[Haskell-cafe] Parse text difficulty

Thomas Johnsson thomas at skri.net
Wed Dec 15 10:45:33 EST 2004


>> > printastable :: [([Int],Word)] -> String
>> >
>> > printastable l = concat $ map (\(xs,w) -> (show xs) ++ " " ++ w ++
>> > "\n") l
>>
>> I'd use
>>
>> [ c | (xs,w) <- l, c <- (show xs) ++ " " ++ w ++ "\n" ]
>>
>> instead -- after all, list comprehensions provide a much nicer
>> syntax for map, filter and concat.
>
> I try to stay away from list comprehension because I can't memorize in
> which order the conditions are processed and I have to introduce new
> variables. [..]

I find it helpful to compare list comprehensions to nested loops & ifs
in imperative languages, so that eg

   [ E | v1 <- E1, pred2, v3 <- E3 ]

'does the same thing as'

   for( v1 <- E1 ){
      if( pred2 ){
         for( v3 <- E3){
            put-elem-in-resulting-list( E )
         }
      }
   }

-- Thomas




More information about the Haskell-Cafe mailing list