[Haskell-cafe] Re: Embedding newlines into a string?

Neil Mitchell ndmitchell at gmail.com
Mon Apr 14 10:41:17 EDT 2008


Hi

>   hanoi_shower [] = ...
>   hanoi_shower ((a, b) : moves) = ...
>
>  or (preferably) with map
>
>  hanoi_shower moves = unlines (map show_move moves) where
>   show_move (a, b) = ...

A nice list comprehension works wonders in these situations:

hanoi_shower moves = unlines ["Move " ++ show a ++ " to " ++ show b ++
"." | (a,b) <- moves]

I would personally remove the "." from the end, as its a list of
commands, not sentences - but that is personal choice. I'd also use
unwords, as its slightly shorter:

hanoi_shower moves = unlines [unwords ["Move", show a, "to", show b] |
(a,b) <- moves]

Thanks

Neil


More information about the Haskell-Cafe mailing list