您好,我已经完成了我的 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)
看跌期权
sample=JArray[
JObject [("name",JString "adita"),("age",JNumber 13)],
JObject [("name",JString "dan"),("kids",JNumber 3)] ,
JNumber 3,
JBool False,
JString "Howdy"
]
PS 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]"