问题:
我希望能够在运行时解释任何类型的 Haskell 代码——包括不受一行限制的代码。
现在我正在使用 hint在下面执行此操作,它适用于单行:
html :: String -> IO String
html code = do
r <- runInterpreter $ do
setImports ["Prelude"]
interpret code (as :: () -> String)
case r of
Left err -> return $ show err
Right func -> return $ func()
示例:
如果上面的 code
是 \() -> "Hello World"
就可以了。
但是如果代码
是下面这样的东西,我上面的代码不起作用:(更新:它起作用)。
\() -> let concatString :: String -> String -> String
concatString str1 str2 = str1 ++ str2
in concatString "Hello" "World"
问题:
如何使用提示或任何其他库在运行时解释多行 Haskell 字符串?
更新
这确实有效 - 我在原始表达式中犯了一个错误(我现在已更正)。
最佳答案
我没有使用提示,但我可以告诉你你的例子不是一个有效的 Haskell 表达式。 where
子句不附加到表达式,它们附加到定义。也就是说,您必须有一个 =
符号才能拥有一个 where
子句。
-- Correct
foo = bar
where
bar = baz
where
baz = 42
-- Incorrect
foo = (bar + 1 where bar = 41)
如果你想在表达式上下文中定义一些东西你必须使用let
let concatString :: String -> String -> String
concatString str1 str2 = str1 ++ str2
in concatString "Hello" "World"
关于haskell - 使用 Haskell 在运行时解释多行代码字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54155931/