我在学习Writer
单子和有以下几点:Writer monad的函数组合?
myFunction :: Int -> Int -> Writer String Int
myFunction e1 e2
| e1 > e2 = do
tell ("E1 greater")
return (e1)
| otherwise = do
tell ("E2 greater")
return (e2)
main = do
-- execWriter :: Writer w a -> w
print $ execWriter . myFunction 1 2
错误:
"Couldn't match type ‘WriterT String Data.Functor.Identity.Identity Int’with ‘a0 -> Writer c0 a1’
Expected type: a0 -> Writer c0 a1
Actual type: Writer String Int"
为什么用.
,而不是$
这个计算错误?也许我对函数组合的理解是不正确的?
好了' (。)::(b - > c) - >(a - > b) - > a - > c'运算符需要两个函数,每个函数带一个参数,右边的'myFunction'没有参数(或至少不是那个'execWriter'期待的。 –
你也可以定义'(。:) =(。)。 (。)'然后使用'execWriter。:myFunction $ 1 2'。它也被称为“猫头鹰运营商”。 ('(。:)'已经在'Data.Composition'中定义 - http://hackage.haskell.org/package/composition-1.0.2.1/docs/Data-Composition.html) – zeronone