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

Benjamin L. Russell dekudekuplex at yahoo.com
Mon Apr 14 23:45:05 EDT 2008


Now it works; viz (in response to Brent Yorgey's
suggestion, I have merged hanoi and hanoi_helper):

hanoi_general_list_comprehension_unwords.hs (based on
Neil Mitchell's suggestion, except for the trailing
'.'):

hanoi :: a -> a -> a -> Int -> [(a, a)]
hanoi source using dest n
    | n == 1 = [(source, dest)]
    | otherwise = hanoi source dest using (n-1) 
                  ++ hanoi source using dest 1
                         ++ hanoi using source dest
(n-1)

hanoi_shower :: Show a => [(a, a)] -> String
hanoi_shower moves = unlines [unwords ["Move", show a,
"to", show b, "."] | (a, b) <- moves]

Then, in WinHugs:

Main> :load
hanoi_general_list_comprehension_unwords.hs
Main> putStr (hanoi_shower (hanoi 'a' 'b' 'c' 2))
Move 'a' to 'b' .
Move 'a' to 'c' .
Move 'b' to 'c' .

Main> putStr (hanoi_shower (hanoi 1 2 3 2))
Move 1 to 2 .
Move 1 to 3 .
Move 2 to 3 .

Notwithstanding Neil's advice on removing the trailing
'.', which I appreciate, I would still prefer to
retain it, because, in the interests of literate
programming, I would like a sequence of English
sentences as commands acceptable even to an English
teacher.

So, to be pedantic and remove the ' ' before the '.'
at each line:

hanoi_shower portion of
hanoi_general_list_comprehension_unlines.hs (based on
Neil Mitchell's suggestion):

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

Then, in WinHugs:

Main> :load
hanoi_general_list_comprehension_unlines.hs
Main> putStr (hanoi_shower (hanoi 'a' 'b' 'c' 2))
Move 'a' to 'b'.
Move 'a' to 'c'.
Move 'b' to 'c'.

Main> putStr (hanoi_shower (hanoi 1 2 3 2))
Move 1 to 2.
Move 1 to 3.
Move 2 to 3.

Splendid!

Now, just for fun, let's see what other versions also
work:

hanoi_shower portion of hanoi_general_map_unlines.hs
(based on Tillman Rendel's suggestion):

hanoi_shower :: Show a => [(a, a)] -> String
hanoi_shower moves = unlines (map move moves)
                     where move (a, b) = "Move " ++
show a ++ " to "++ show b ++ "."

Then, in WinHugs:

Main> :load hanoi_general_map_unlines.hs
Main> putStr (hanoi_shower (hanoi 'a' 'b' 'c' 2))
Move 'a' to 'b'.
Move 'a' to 'c'.
Move 'b' to 'c'.

Main> putStr (hanoi_shower (hanoi 1 2 3 2))
Move 1 to 2.
Move 1 to 3.
Move 2 to 3.

Wonderful!  Thanks especially to Neil Mitchell and
Tillman Rendel for their constructive suggestions.

Nevertheless, I'm still working on the recursive
version.  So far, I've gotten this far:

hanoi_shower portion of hanoi_general_recursive.hs
(based on Tillman Rendel's suggestion):

hanoi_shower :: Show a => [(a, a)] -> String
hanoi_shower ((a, b) : moves) 
    | null moves = "Move " ++ show a ++ " to "++ show
b ++ "."
    | otherwise == "Move " ++ show a ++ " to "++ show
b ++ "." ++ hanoi_shower moves

However, in WinHugs, I get the following error:

Hugs> :load hanoi_general_recursive.hs
ERROR file:hanoi_general_recursive.hs:11 - Syntax
error in declaration (unexpected `}', possibly due to
bad layout)

I haven't used recursion in Haskell much so far; I've
only used it in Scheme, so I'm not used to it.

I need to go to lunch now, so I'll work on this part
later.  Perhaps I can get it to work after lunch....

Benjamin L. Russell

--- Neil Mitchell <ndmitchell at gmail.com> wrote:

> 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
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
> 



More information about the Haskell-Cafe mailing list