问题
I'm trying to write the following Haskell function, but in Scala (using scalaz):
ghci>let f = do { x <- getChar; _ <- putChar(x); return () }
ghci>f -- then type '4'
44ghci>
Here's what I tried:
def showInput: IO[Unit] = for {
c <- getChar
_ <- putChar(c)
} yield ()
Then I ran it:
scala> net.repl.Foo.showInput
res2: scalaz.effect.IO[Unit] = scalaz.effect.IOFunctions$$anon$6@73e3ae3a
After running the method, I typed 1234
. But, I had to press enter
before the output (of 1
) showed up:
scala> res2.unsafePerformIO
1
How can I write the above scalaz function to behave identically to the Haskell function?
来源:https://stackoverflow.com/questions/29378195/printing-each-character-typed-w-scalaz