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

Tillmann Rendel rendel at daimi.au.dk
Mon Apr 14 10:36:06 EDT 2008


Benjamin L. Russell wrote:
> Wow, that's very general.  So you want to divide hanoi
> into a main function, a helper function, and a display
> function.
> 
> I tried it out, and got this far so far:
> 
> [...]
> 
> hanoi_shower :: Show a => [(a, a)] -> String
> hanoi_shower [(a, b)] = "Move " ++ show a ++ " to " ++
>     show b ++ "."

That's exactly what I was thinking about, but your hanoi_shower only
handles list of exactly one action, but you have to handle longer lists,
too. This could be done with explicit recursion

   hanoi_shower [] = ...
   hanoi_shower ((a, b) : moves) = ...

or (preferably) with map

hanoi_shower moves = unlines (map show_move moves) where
   show_move (a, b) = ...

Note the use of unlines again. I decided to use where to introduce a
local binding to avoid cluttering the top-level scope with function names.

   Tillmann


More information about the Haskell-Cafe mailing list