开发者

Wrong function declaration giving error when inferring on type

开发者 https://www.devze.com 2023-03-17 19:01 出处:网络
I have started learning Haskell from Learn You a 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 side

I have started learning Haskell from Learn You a 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 sidetrack开发者_运维技巧ed to zippers and am now looking at Zippers

As part of the exercises on the linked wiki page on zippers, I have functions with the following method declarations

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

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:

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.


You're missing the second argument to 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)
0

精彩评论

暂无评论...
验证码 换一张
取 消