2011-07-07 88 views
1

我已經開始從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限制,但無法確定這是否與我的代碼相關。

回答

3

您錯過了put的第二個參數。從一目瞭然看來,你只是讓你的括號錯誤。嘗試這個。

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

謝謝!我似乎有麻煩參數正確的功能,一般做G(exp1 exp2),我應該做G(exp1)(exp2)。 –

+2

@mb_shart如果您記住'G exp1 exp2'是正確的語法,括號僅用於對子表達式進行分組,則變得更容易。 – Rotsor