简体   繁体   English

错误的 function 声明在推断类型时给出错误

[英]Wrong function declaration giving error when inferring on type

I have started learning Haskell from Learn You a Haskell .我已经从Learn You a Haskell 开始学习 Haskell In one of the early parts there is an example with binary trees and I started thinking about implementing a remove function and hence got sidetracked to zippers and am now looking at Zippers在早期的一个部分中,有一个二叉树的例子,我开始考虑实现一个删除 function ,因此被转移到了 zippers 上,现在我正在研究Zippers

As part of the exercises on the linked wiki page on zippers, I have functions with the following method declarations作为 zippers 链接 wiki 页面上练习的一部分,我有具有以下方法声明的函数

get :: Node a -> a

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

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

retrieve :: Thread -> Node a -> a

Now I try to implement the following function现在我尝试实现以下 function

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

Loading this in ghci gives:在 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.

I read up on the Monomorphism restriction, but couldn't decide if this is relevant for my code.我阅读了 Monomorphism 限制,但无法确定这是否与我的代码相关。

You're missing the second argument to put .您缺少put的第二个参数。 From a glance it looks like you just got your parenthesis wrong.乍一看,您似乎只是把括号弄错了。 Try this.尝试这个。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM