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
14
  • 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). Commented Sep 30, 2022 at 16:05
  • 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. Commented Sep 30, 2022 at 17:45
  • 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? Commented Sep 30, 2022 at 18:49
  • 1
    You already did 1. and 5.. 3. can be accomplished with reverse, 2. with words, and 4. with unwords. Commented Sep 30, 2022 at 18:54
  • 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" :-) Commented Sep 30, 2022 at 19:35

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.