0

我一直被困在这个特定的阶段,致力于解决我遇到的问题,而我对 haskell 的体验仍处于初学者水平。

在尝试创建一个将节点(由字符串“键”和值组成)插入二叉搜索树的函数时,我尝试了一种使用保护括号的方法。我似乎误解了如何在这里做某事,我收到的错误对我来说没有意义。


module Ex03 where                                                                                                                                                                                                  
import Data.List ((\\))                                                                                                                                                                                            

-- Datatypes -------------------------------------------------------------------                                                                                                                                   

-- do not change anything in this section !                                                                                                                                                                        

-- Binary Tree                                                                                                                                                                                                     
data BT a b                                                                                                                                                                                                        
  = Leaf                                                                                                                                                                                                           
  | Branch (BT a b) a b (BT a b)                                                                                                                                                                                   
  deriving (Eq, Show)                                                                                                                                                                                              

-- association list                                                                                                                                                                                                
type Assoc a b = [(a,b)]                                                                                                                                                                                           

-- lookup binary (search) tree                                                                                                                                                                                     
lkpBST :: Ord a1 => BT a1 a -> a1 -> Maybe a                                                                                                                                                                       
lkpBST Leaf _  =  Nothing                                                                                                                                                                                          
lkpBST (Branch left k d right) k'                                                                                                                                                                                  
 | k < k'     =  lkpBST left k'                                                                                                                                                                                    
 | k > k'     =  lkpBST right k'                                                                                                                                                                                   
 | otherwise  =  Just d                                                                                                                                                                                            

-- Coding Part 1 (13 Marks)                                                                                                                                                                                        

-- insert into binary (search) tree                                                                                                                                                                                
insBST :: Ord a => a -> b -> BT a b -> BT a b                                                                                                                                                                      
insBST labelX valueX Leaf = Branch Leaf labelX valueX Leaf                                                                                                                                                         
{-                                                                                                                                                                                                                 
  insBST a 1 Branch Leaf b 1 Leaf                                                                                                                                                                                  
@?= Branch Branch Leaf a 1 Leaf  b 2 Leaf                                                                                                                                                                          
-}                                                                                                                                                                                                                 
insBST labelX valueX (Branch left labelRoot valueRoot right)                                                                                                                                                       
    | valueX < valueRoot = (Branch (insBST labelX valueX left) labelRoot valueRoot right)                                                                                                                          
    | valueX > valueRoot = (Branch left labelRoot valueRoot (insBST labelX valueX right))                                                                                                                          
    | otherwise = Branch left labelX valueX right                                                                                                                                                                  

-- Coding Part 2 (6 Marks)                                                                                                                                                                                         

-- convert an association list to a binary search tree                                                                                                                                                             
assoc2bst :: Ord a => Assoc a b -> BT a b                                                                                                                                                                          
assoc2bst _ = error "assoc2bst not yet implemented"                                                                                                                                                                

-- Coding Part 3 (6 Marks)                                                                                                                                                                                         

-- convert a binary search tree into an (ordered) association list                                                                                                                                                 
bst2assoc :: Ord c =>  BT c e -> Assoc c e                                                                                                                                                                         
bst2assoc _ = error "bst2assoc not yet   



我收到的错误如下

Hours of hacking await!
If I break, you can:
  1. Restart:           M-x haskell-process-restart
  2. Configure logging: C-h v haskell-process-log (useful for debugging)
  3. General config:    M-x customize-mode
  4. Hide these tips:   C-h v haskell-process-show-debug-tips
src/Ex03.hs:35:14: Could not deduce (Ord b) arising from a use of ‘&lt;’ …
    from the context (Ord a)
      bound by the type signature for
                 insBST :: Ord a => a -> b -> BT a b -> BT a b
      at /Users/eoindowling/college/3year2/michaelmas/CS3016-_Introduction_to_Functional_Programming/CSU34016-1920/Exercise03/src/Ex03.hs:28:11-45
    Possible fix:
      add (Ord b) to the context of
        the type signature for
          insBST :: Ord a => a -> b -> BT a b -> BT a b
    In the expression: valueX < valueRoot
    In a stmt of a pattern guard for
                   an equation for ‘insBST’:
      valueX < valueRoot
    In an equation for ‘insBST’:
        insBST labelX valueX (Branch left labelRoot valueRoot right)
          | valueX < valueRoot
          = (Branch (insBST labelX valueX left) labelRoot valueRoot right)
          | valueX > valueRoot
          = (Branch left labelRoot valueRoot (insBST labelX valueX right))
          | otherwise = Branch left labelX valueX right
Compilation failed.

我的守卫声明有什么问题?我见过类似的方法,但我不明白我做错了什么。

4

1 回答 1

2

您正在比较valueXvalueRoot类型b。错误消息说这样做需要一个约束Ord b,但你只有一个约束Ord a

也许您打算比较labelXand labelRoot(其中有 type a)?

于 2019-11-19T15:30:39.120 回答