2012-05-17 198 views
0

我有問題,在Haskell BST。 我想我已經問題定義的「鑰匙」變量節點(Uzel)是奧德。但我絕對沒有更多的想法。二叉搜索樹,comparsion

我不過,如果我一旦確定了「關鍵」的說法爲樹類型奧德,它是有效的,並在代碼中每次使用會太獲取此信息。

下面的代碼是不完整的,但我在談論的東西:

data (Ord key) => Tree key value = List key value |Uzel (Tree key value) key value (Tree key value) | Null deriving (Show) 

prazdny :: Ord key => Tree key value 
prazdny = Null 

najdi :: (Ord key,Ord k) => k -> Tree key value -> Maybe a 
najdi k Null = Nothing 
najdi k (Uzel levy klic hodnota pravy) = if k < klic 
       then najdi k levy 
       else najdi k pravy 

當試圖編譯我得到這個:

bvs.hs:9:49: 
Could not deduce (key ~ k) 
from the context (Ord key, Ord k) 
    bound by the type signature for 
      najdi :: (Ord key, Ord k) => k -> Tree key value -> Maybe a 
    at bvs.hs:(8,1)-(11,58) 
    `key' is a rigid type variable bound by 
     the type signature for 
      najdi :: (Ord key, Ord k) => k -> Tree key value -> Maybe a 
     at bvs.hs:8:1 
    `k' is a rigid type variable bound by 
     the type signature for 
     najdi :: (Ord key, Ord k) => k -> Tree key value -> Maybe a 
     at bvs.hs:8:1 
In the second argument of `(<)', namely `klic' 
In the expression: k < klic 
In the expression: if k < klic then najdi k levy else najdi k pravy 

地塊感謝您的想法! !

+3

你不能用變量'klic'類型的'key'比較變量'k'類型的'k'。嘗試'najdi :: Ord key =>鍵 - >樹鍵值 - >可能值'作爲類型(並記得有時返回一個值)。 – pigworker

+0

事實上,該錯誤消息表示,'key'和'k'必須是同一類型'(鍵〜K)'和GHC是無法證明。 – Vitus

+1

如果你在你的代碼中使用英文名,這個問題會對更多的人更清楚。 – yairchu

回答

0

評論了這條線,它會編譯:

--najdi :: (Ord key,Ord k) => k -> Tree key value -> Maybe a 
+1

請解釋爲什麼* *這就是OP需求去做;提供沒有上下文或解釋的代碼更改無人幫助。 – dimo414

2

的問題是,k和密鑰必須以比較它們相同的類型,但你聲明它們可能會有所不同,所以編譯器抱怨。如果你讓它們爲相同的類型,它會打字檢查。