Haskell IO (Monads?)
Posted: Wed May 28, 2008 9:14 pm
Hello,
I've been working on my Haskell recently, and I've come to an issue. I'm working on an interpreter for a small stack based language I made. However, I can not see how one is to use IO inside the body of a function.
For example, I have a function:
Now, I have a 'print' command.
However, due to the IO, it returns (or should, if Google has proven to be true.) '[Token] IO'. My problem is that the rest of my code expects just [Token].
Does anyone know how this is performed in Haskell? I don't feel that using [Token] IO everywhere would be a good idea, yet other Haskell code I've looked at does seem to do this.
I'm not 100% on monads yet, however, this seems to be a problem of a more 'coding paradigm' sort, so I don't think that should be a problem.
Thanks for any 'pure' enlightenment,
Alboin
I've been working on my Haskell recently, and I've come to an issue. I'm working on an interpreter for a small stack based language I made. However, I can not see how one is to use IO inside the body of a function.
For example, I have a function:
Code: Select all
{- 'Just a structure that keeps a rough 'type' of a token and the actual token. Used for parsing and the stack. -}
data Token = Tn { tok_type, tok_data :: String }
{- The first arg is the command and the second is the stack. We return the modified stack. -}
eval_command :: String -> [Token] -> [Token]
eval_command "dup" (x:xs) = ([x] ++ [x] ++ xs)
Code: Select all
eval_command "print" (x:xs) = do
putStrLn x
return xs
Does anyone know how this is performed in Haskell? I don't feel that using [Token] IO everywhere would be a good idea, yet other Haskell code I've looked at does seem to do this.
I'm not 100% on monads yet, however, this seems to be a problem of a more 'coding paradigm' sort, so I don't think that should be a problem.
Thanks for any 'pure' enlightenment,
Alboin