Well I've been banging my head against hint but I give up for now. I know hint can do this but I'm not sure how. [edit] See TomMD's answer for how to set imports up for hint. [/edit]
import Language.Haskell.Interpreter (eval, runInterpreter, Interpreter, InterpreterError)
main = do let resIO = eval "3" :: Interpreter String
res <- runInterpreter resIO
print res
This uninterestingly produces Right "3"
as the result. I tried the following variants, only to run into baffling errors:
... eval "3 + 3" ....
-- yields --
Left (WontCompile [GhcError (errMsg = "Not in scope: `+'"])
The +
operator isn't in scope??? wtf...
import Language.Haskell.Interpreter (interpret, as, runInterpreter, Interpreter)
main = do let resIO = interpret "3" (as :: Int) :: Interpreter Int
res <- runInterpreter resIO
print res
-- yields --
Left (WontCompile [GhcError (errMsg = "Not in scope: type constructor or class 'Int'")])
The Int
class isn't in scope??? ugh...
I invite those more knowledgeable than me to expound on the finer details of hint.
eval "1+2" :: Int
? – HarleneText.Parsec
's part of the haskell platform and lets you do:runP (do { x <- many1 digit; spaces; string "+"; spaces; y <- many1 digit; return $ read x + read y }) () "-" "1 + 2"
returnsRight 3
. But you probably want more than just add. Do you want a calculator? A full fledged haskell interpreter? – Harlene