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

2008-04-18 Thread Ariel J. Birnbaum
Things to avoid - HaskellWiki - 7 Related Links: http://www.haskell.org/haskellwiki/Things_to_avoid#Related_Links The link was broken (it had an extra chunk of '- Haskell Wiki' ;) ) so I fixed it. For that matter, the Common Hugs Messages link is broken too but I can't seem to find the page it

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

2008-04-18 Thread Benjamin L. Russell
Ariel, --- Ariel J. Birnbaum [EMAIL PROTECTED] wrote: Things to avoid - HaskellWiki - 7 Related Links: http://www.haskell.org/haskellwiki/Things_to_avoid#Related_Links The link was broken (it had an extra chunk of '- Haskell Wiki' ;) ) so I fixed it. Thank you; sorry about the broken

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

2008-04-17 Thread Ariel J. Birnbaum
Common Misunderstandings - HaskellWiki http://www.haskell.org/haskellwiki/Common_Misunderstandings I didn't find this one... maybe it should be in a more prominent place? Things to avoid - HaskellWiki http://www.haskell.org/haskellwiki/Things_to_avoid I thought of this but it has more

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

2008-04-17 Thread Benjamin L. Russell
Ariel, --- Ariel J. Birnbaum [EMAIL PROTECTED] wrote: Common Misunderstandings - HaskellWiki http://www.haskell.org/haskellwiki/Common_Misunderstandings I didn't find this one... maybe it should be in a more prominent place? Things to avoid - HaskellWiki

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

2008-04-16 Thread Ariel J. Birnbaum
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 This seems to be a common pitfall for Haskell newcomers: mistaking a single-element list pattern (such

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

2008-04-16 Thread Benjamin L. Russell
Ariel, Check out the following HaskellWiki pages: Common Misunderstandings - HaskellWiki http://www.haskell.org/haskellwiki/Common_Misunderstandings Things to avoid - HaskellWiki http://www.haskell.org/haskellwiki/Things_to_avoid Hope these help Benjamin L. Russell --- Ariel J. Birnbaum

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

2008-04-16 Thread Benjamin L. Russell
Ariel, In response to your comment, since there was apparently no section devoted to pitfalls of iterating over lists, I have added the section 1.4 Iterating Over a List in the following HaskellWiki page; viz: Common Misunderstandings - HaskellWiki

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

2008-04-15 Thread Tillmann Rendel
Benjamin L. Russel wrote: hanoi_shower ((a, b) : moves) | null moves = ... | otherwise == ... Luke Palmer wrote: More idiomatic pedantry: the way you will see most Haskellers write this style of function is by pattern matching rather than guards: hanoi_shower [] = ... hanoi_shower

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

2008-04-15 Thread Benjamin L. Russell
Ok; I rewrote my recursive version of hanoi, preserving my semantics (i.e., working for lists of length 1 or more, rather than 0 or more, to start with) in a more Haskell-idiomatic manner; viz: hanoi_general_recursive.hs: hanoi :: a - a - a - Int - [(a, a)] hanoi source using dest n | n == 1

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

2008-04-14 Thread Tillmann Rendel
Benjamin L. Russell wrote: but got stuck on outputting newlines as part of the string; quoting is done by the show function in Haskell, so you have to take care to avoid calling show. your code calls show at two positions: (1) when you insert the newline into the string (2) when you output

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

2008-04-14 Thread Benjamin L. Russell
Ok; much better. Here's my new type signature and definition: hanoi.hs: hanoi :: Int - IO () hanoi n = mapM_ putStrLn (hanoi_helper 'a' 'b' 'c' n) hanoi_helper :: Char - Char - Char - Int - [String] hanoi_helper source using dest n | n == 1 = [Move ++ show source ++ to ++ show

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

2008-04-14 Thread Neil Mitchell
Hi mapM_ putStrLn (hanoi 2) -- outputs each move in a new line putStrLn (unlines (hanoi 2)) -- same as previous line putStr (unlines (hanoi 2)) is what you want. Unlines puts a trailing new line at the end of every line, including the final one. putStrLn puts an additional

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

2008-04-14 Thread Tillmann Rendel
Benjamin L. Russell wrote: Ok; much better. Here's my new type signature and definition: hanoi :: Int - IO () hanoi_helper :: Char - Char - Char - Int - [String] If you want, you can separate the algorithm and the output processing even more by providing three functions of these types:

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

2008-04-14 Thread Neil Mitchell
Hi mapM_ putStrLn == putStr . unlines I'm wondering which (==) you mean here ;) Expression equality, defined by: instance (Arbitrary a, Eq b) = Eq (a - b) where f == g = forall x :: a, f x == g x Using QuickCheck to generate the values, and an Eq over IO (), which can be defined

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

2008-04-14 Thread Tillmann Rendel
Neil Mitchell wrote: Unlines puts a trailing new line at the end of every line, including the final one. putStrLn puts an additional trailing new line, so you get 2 at the end. Thanks for that clarification. mapM_ putStrLn == putStr . unlines I'm wondering which (==) you mean here ;)

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

2008-04-14 Thread Benjamin L. Russell
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 :: a - a - a - Int - [(a, a)] hanoi a b c n = hanoi_helper a b c n hanoi_helper :: a - a - a - Int - [(a, a)]

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

2008-04-14 Thread Abhay Parvate
Yes, they are. That's what perhaps Neil Mitchell means by mapM_ putStrLn == putStr . unlines And whether the trailing newline is to be called the last blank line depends upon the convention; The string that is output in both the cases contains a single newline character. Are you calling that a

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

2008-04-14 Thread Brandon S. Allbery KF8NH
On Apr 14, 2008, at 7:51 , Benjamin L. Russell wrote: hanoi_shower :: Show a = [(a, a)] - String hanoi_shower [(a, b)] = Move ++ show a ++ to ++ show b ++ . You've just specified via pattern match that hanoi_shower always gets a 1-element list. Is that really what you intended? --

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

2008-04-14 Thread Neil Mitchell
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

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

2008-04-14 Thread Tillmann Rendel
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 ++

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

2008-04-14 Thread Brent Yorgey
hanoi :: a - a - a - Int - [(a, a)] hanoi a b c n = hanoi_helper a b c n Note that now hanoi is exactly the same function as hanoi_helper, so you may as well just get rid of hanoi_helper. =) -Brent ___ Haskell-Cafe mailing list

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

2008-04-14 Thread Benjamin L. Russell
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 =

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

2008-04-14 Thread Brandon S. Allbery KF8NH
On Apr 14, 2008, at 23:45 , Benjamin L. Russell wrote: 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 `==' after the

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

2008-04-14 Thread Luke Palmer
On Tue, Apr 15, 2008 at 3:45 AM, Benjamin L. Russell [EMAIL PROTECTED] wrote: hanoi_shower ((a, b) : moves) | null moves = Move ++ show a ++ to ++ show b ++ . | otherwise == Move ++ show a ++ to ++ show b ++ . ++ hanoi_shower moves More idiomatic pedantry: the way you will see