我开始学习模板haskell,所以我想创建一个接收String参数的函数,该参数定义要生成的函数的名称
build_p5 :: String -> [Dec]
build_p5 name =
[ FunD p1
[ Clause [TupP [VarP a,VarP b]] (NormalB (AppE (VarE c) (VarE a))) [FunD c [Clause [VarP a] (NormalB (VarE a)) []]]
]
] where
p1 = mkName name
a = mkName "a"
b = mkName "b"
c = mkName "ident"
运行定义:
$(build_p5 "hola")
但是当我运行该程序时会出现以下错误
Couldn't match type ‘[Dec]’ with ‘Q [Dec]’ Expected type: DecsQ Actual type: [Dec] In the expression: build_p5 "hola"
正确的代码
build_p2 :: Monad m => String -> m [Dec]
build_p2 name =
return [ FunD p1
[ Clause [TupP [VarP a,VarP b]] (NormalB (AppE (VarE c) (VarE a))) [FunD c [Clause [VarP a] (NormalB (VarE a)) []]]
]
] where
p1 = mkName name
a = mkName "a"
b = mkName "b"
c = mkName "ident"