推断类型时错误的函数声明给出错误

时间:2011-07-07 13:06:38

标签: haskell

我已经开始从Learn You a Haskell学习Haskell了。在其中一个早期部分中有一个带有二叉树的例子,我开始考虑实现一个删除功能,因此对拉链产生了偏见,现在正在查看Zippers

作为拉链链接维基页面练习的一部分,我有以下方法声明的函数

get :: Node a -> a

put :: a -> Node a -> Node a

retrieveNode :: Thread -> Node a -> Node a

retrieve :: Thread -> Node a -> a

现在我尝试实现以下功能

update :: Thread -> Node a -> (a -> a) -> Node a
update t n f = put (f (get (retrieveNode t n)) retrieveNode t n) -- Line 29 referenced

在ghci中加载这个:

Prelude> :l theseus.hs 
[1 of 1] Compiling Main             ( theseus.hs, interpreted )

theseus.hs:29:15:
    Couldn't match expected type `Node a'
       against inferred type `Node a1 -> Node a1'
    In the expression:
        put (f (get (retrieveNode t n)) retrieveNode t n)
    In the definition of `update':
        update t n f = put (f (get (retrieveNode t n)) retrieveNode t n)
Failed, modules loaded: none.

我阅读了Monomorphism限制,但无法确定这是否与我的代码相关。

1 个答案:

答案 0 :(得分:3)

你错过了put的第二个参数。一眼就看出你的括号错了。试试这个。

update :: Thread -> Node a -> (a -> a) -> Node a
update t n f = put (f (get (retrieveNode t n))) (retrieveNode t n)