【发布时间】:2018-10-27 09:44:22
【问题描述】:
您好,我已经完成了我的 JSon 类型,我正在尝试将其写入文件。
我可以从前奏中做到这一点,但在使用 IO Monad 时我不能这样做。我得到以下error:
Main.hs:13:24: error:
* Couldn't match type `Char' with `[Char]'
Expected type: String
Actual type: Char
* In the second argument of `writeFile', namely `val'
In a stmt of a 'do' block: writeFile out val
In the expression:
do val <- renderJValue sample
writeFile out val
|
13 | writeFile out val
| ^^^
主要
module Main where
import Jlib
import Put
import Data.Typeable
import System.Environment
out="data.txt"
main::IO()
main=do
val<-renderJValue sample
writeFile out val
为什么这在 IO Monad 中不起作用,因为前奏中的 renderJValue sample 可以正常工作。
Jlib.hs
data JValue=JString String
|JNumber Double
|JBool Bool
|JNull
|JObject [(String,JValue)]
|JArray [JValue]
deriving (Eq,Ord,Show)
Put.hs
sample=JArray[
JObject [("name",JString "adita"),("age",JNumber 13)],
JObject [("name",JString "dan"),("kids",JNumber 3)] ,
JNumber 3,
JBool False,
JString "Howdy"
]
P.S renderJValue 返回一个字符串
PS:如果我开始前奏,我会加载模块并渲染它的工作值:
Prelude System.Environment Put> :load Put
Ok, two modules loaded.
Prelude System.Environment Put> renderJValue sample
"[{name:adita,age:13.0},{name:dan,kids:3.0},3.0,False,Howdy]"
【问题讨论】:
标签: haskell monads do-notation