Haskell interacts () truncates my output?
I am working on a basic Haskell program that contains this line of code:
interact (unwords . (map pigLatin . words) )
However, after passing an array of strings to my pigLatin function, then wrapping back into a string, it always truncates the last word I typed. For example:
*HaskellPractice> getUserInput
this is broken
histay isway
For some reason, this doesn't print. I am on a Mac, so I have to specify the following parameters declared in getUserInput before calling for interaction:
hSetBuffering stdin LineBuffering
hSetBuffering stdout NoBuffering
I guess this is a small detail that I haven't figured out yet. Thanks for any input / help! OSFTW EDIT: Here's the whole program.
import System.IO
pigLatin :: String -> String
pigLatin word = if ( (head word) `elem`['a', 'e', 'i', 'o', 'u'] )
then (word ++ "way")
else (tail word) ++ [(head word)] ++ "ay"
getUserInput = do
hSetBuffering stdin LineBuffering
hSetBuffering stdout NoBuffering
interact (unwords . (map pigLatin . words) )
source to share
Here is the program I tested:
import System.IO
pigLatin xs = "[" ++ xs ++ "]"
main = do
hSetBuffering stdin LineBuffering
hSetBuffering stdout NoBuffering
interact (unwords . (map pigLatin . words) )
And this is what the session looks like ghci
:
*Main> :main
this is line1
[this] [is] this is line2
[line1] [this] [is] [line2]<stdin>: hGetBuffering: illegal operation (handle is closed)
I typed:
this is line1
this is line2
(Control-d)
This way it doesn't truncate the input - it just doesn't emit the last word in the line until the next line is read (or EOF is encountered.)
If you run it from a shell, you get what you expect:
shell$ (echo this is line1; echo this is line2) | runhaskell ./platin.hs
[this] [is] [line1] [this] [is] [line2]
source to share