3

The output from running this code is not correct, I cannot wrap my head sadly, to know where the wrong is in my own code. I been staring at it for hours.

Code:

import System.IO(isEOF)

reverseIO :: IO ()
reverseIO = do 
  line <- getLine
  done <- isEOF
  if null line
    then return ()
  else if done then
    putStrLn $ reverseStr line
  else do
      reverseIO

reverseStr :: [w] -> [w]
reverseStr [] = []
reverseStr (x:xs) = reverseStr xs ++ [x]

main = do
  reverseIO

Desired: 2. Reverse and print out.

> hello world
world hello

Running:

GHCi, version 8.10.6: https://www.haskell.org/ghc/  :? for help
Loaded GHCi configuration from /home/runner/University-Labs/.ghci
[1 of 1] Compiling Main             ( Main.hs, interpreted )
Ok, one module loaded.
 hello world

EDIT: Pressing Ctrl+D,

GHCi, version 8.10.6: https://www.haskell.org/ghc/  :? for help
Loaded GHCi configuration from /home/runner/University-Labs/.ghci
> hello world
dlrow olleh
John Smith
  • 465
  • 4
  • 15
  • 38
  • 3
    So, `reverseStr` right now is correctly reversing a string. “Hello World” is a string, and “dlroW olleH” is that string backwards. The problem is that reversing a string isn’t what you want right now, what you want is the _words_ reversed. So you’d have to find the _words_ in the string, reverse that list of words, and then put it back into a string (_unwords_ the list, so to say). – Cubic Sep 30 '22 at 16:05
  • Does this answer your question? [How to split a string in Haskell?](https://stackoverflow.com/questions/4978578/how-to-split-a-string-in-haskell) – jthulhu Sep 30 '22 at 16:13
  • @BlackBeans, `let chars = zip line (tail line)` – John Smith Sep 30 '22 at 16:15
  • @BlackBeans, It **DOES NOT**. Because I don't understand. – John Smith Sep 30 '22 at 16:24
  • The library offers a [`words`](https://hackage.haskell.org/package/base-4.17.0.0/docs/Prelude.html#v:words) function. And also `unwords`. – jpmarinier Sep 30 '22 at 17:22
  • 1
    @JohnSmith your problem is mainly that you want to split your input into words, reverse these words, and finally print the result. The question I linked to has answers that show you how to do that. – jthulhu Sep 30 '22 at 17:45
  • @BlackBeans, I have no idea how to do that in my current code. I looked at the code from the link, and I am confused. Really confused. – John Smith Sep 30 '22 at 18:42
  • @jpmarinier, you mean `unwords . map reverse . words` ? – John Smith Sep 30 '22 at 18:43
  • 1
    @JohnSmith what you want is: 1. read input as a string; 2. split that string into a list of strings (list of words); 3. reverse that list; 4. join the list of strings, possibly with a separator, like space; 5. print the result. What you did was: 1. read input as a string (list of characters); 2. reverse that list; 3. print the result. Do you see how `words` and `unwords` could help you? – jthulhu Sep 30 '22 at 18:49
  • @BlackBeans, yes, but, I don't know how to do this properly in Haskell. I know in Python, C#, C/C++ for example. – John Smith Sep 30 '22 at 18:51
  • 1
    You already did `1.` and `5.`. `3.` can be accomplished with [reverse](http://zvon.org/other/haskell/Outputprelude/reverse_f.html), `2.` with `words`, and `4.` with `unwords`. – jthulhu Sep 30 '22 at 18:54
  • @BlackBeans, I am trained in C++, I am a bit confused where I should write the code out right now. – John Smith Sep 30 '22 at 18:55
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/248481/discussion-between-blackbeans-and-john-smith). – jthulhu Sep 30 '22 at 18:59
  • 1
    @JohnSmith - Well, `unwords . map reverse . words` would reverse each word *internally*, giving ["olleh", "dlrow"]. More like: `unwords . reverse . words`. That way you reverse the *word* list. Note that in Haskell we'd rather say "hello impure world" :-) – jpmarinier Sep 30 '22 at 19:35

1 Answers1

1

You have to replace replaceStr with unwords . reverse . words:

reverseIO = do 
  line <- getLine
  done <- isEOF
  if null line
    then return ()
  else if done then
    putStrLn . unwords . reverse . words $ line
  else reverseIO
jthulhu
  • 7,223
  • 2
  • 16
  • 33