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
reverseStrright 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).wordsandunwordscould help you?1.and5..3.can be accomplished with reverse,2.withwords, and4.withunwords.unwords . map reverse . wordswould 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" :-)