[英]How to combine a readProcess monad with where in Haskell?
以下脚本旨在作为已发布且有效的Pandoc Haskell过滤器脚本的扩展。 添加的是对shell命令curl
的调用。
#!/usr/bin/env runhaskell
-- svgtex.hs
import Text.Pandoc.JSON
import System.Process
curl latex = readProcess "curl" ["-d", "type=tex&q=" ++ latex, "http://localhost:16000"] ""
main = toJSONFilter svgtex
where svgtex (Math style latex) = do
svg <- curl latex
return (Math style (svg))
svgtex x = x
但是,对于Haskell函数式编程来说,它是全新的,因此我的脚本失败并不足为奇:
Couldn't match expected type `IO Inline' with actual type `Inline'
In the expression: x
In an equation for `svgtex': svgtex x = x
In an equation for `main':
...
尽管跳过了许多在线Haskell教程和StackExchange Q&A,但是monads的概念仍然没有完全出现在我身上。 因此,非常感谢您对上述脚本中所有错误的详细解释!
问题出在这里:
svgtex x = x
编译者抱怨说
Couldn't match expected type `IO Inline' with actual type `Inline'
因为x
是Inline
,而svgtex
必须返回IO Inline
。 要将x
注入IO
monad,我们可以简单地使用return
函数
svgtex x = return x
要完全了解发生了什么,请参阅任何monad教程(或LYAH)。 粗略地说, IO Inline
类型的值表示可以执行任意数量的I / O并最终返回Inline
类型的值的操作。 return
函数用于将纯值转换为虚拟IO
操作,该操作不执行任何I / O,而只是返回结果。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.