Haskell IO monad and do notation -
the following haskell snippit not compile , can't figure out why.
runcompiler :: tc -> io () runcompiler tc = let cp' = cp in cp' return () cp = compileprog tc i getting following error ghci:
couldn't match expected type `io a0' actual type `string' in stmt of 'do' block: cp' in expression: { cp'; return () } in expression: let cp' = cp in { cp'; return () } any ideas make compile. can't see why not accept () final value given.
when using do notation sequencing 2 statements:
do action1 action2 is same action1 >> action2
since >> has type monad m => m -> m b -> m b both action1 , action2 should monadic values.
it appears compileprog function has type tc -> string, while compiler expects tc -> io a a since using in do notation.
you can use let
do let _ = compileprog tc return () to compile.
if want output returned string, can use putstrln or print:
do putstrln (compileprog tc) return () since putstrln has type string -> io () can remove return ():
do putstrln (compileprog tc) in fact runcompiler can written as
runcompiler :: tc -> io () runcompiler = putstrln . compileprog
Comments
Post a Comment