2

我的程序正在搜索 我写的元组列表如下

import List
data BookInfo = Book Int String [String]
  deriving(Show)

enter :: Int->String->[String]->BookInfo
enter id name subject=Book id name subject
bookId   (Book id _     _  ) = id

index :: BookInfo -> Int
index (Book id name subject) =  bookId  (Book id name subject) 

arrayentering ::BookInfo->[BookInfo]->[BookInfo]
arrayentering (Book id name subject) [] =[(Book id name subject)]
arrayentering (Book  _  "     " ["    "]) [] =[]
arrayentering (Book id name subject) [(Book it namr suject)]=                         
              (Book id name subject):[(Book it name suject)]
toList::[BookInfo]->[Int]
toList [(Book id name subject) ]=   map index [ (Book id name subject)]

bubbleSort::(Ord t) => [t]->[t]
bubbleSort[x,y,z,xs]=
                if x<y then x : [y,z,xs]
                       else y : [x,z,xs]

superBubble::(Ord t) => [[t]]->[[t]]
superBubble a=map bubbleSort a

combining::[BookInfo]->[[Int]]
combining [(Book id name subject)]=superBubble [toList [(Book id name subject)]]

并从任何语法错误中清除它,但是在我尝试输入一个元组列表之后,combining()它会给我运行时错误说

Exception:Not Exhaustive pattern in function Main.combining

这是什么意思?

请只给我指示。如果可能的话,我想自己修复它。

4

1 回答 1

3

函数定义中的模式

combining [(Book id name subject)]=superBubble [toList [(Book id name subject)]]

只匹配只有一个元素的列表。你有一个类似的问题bubbleSort,其中

bubbleSort[x,y,z,xs]=

只匹配恰好有四个元素的列表和其他地方。

我还没有弄清楚你打算如何工作,也许使用变量模式(匹配所有参数)

combining books = superBubble (toList books)

合适吗?


我猜测

arrayentering ::BookInfo->[BookInfo]->[BookInfo]
arrayentering (Book id name subject) [] =[(Book id name subject)]
arrayentering (Book  _  "     " ["    "]) [] =[]
arrayentering (Book id name subject) [(Book it namr suject)]=                         
              (Book id name subject):[(Book it name suject)]

真的应该

arrayentering book bookList
    | empty book = bookList
    | otherwise  = book : bookList
      where
        empty (Book _ name subject) = all isSpace name && all (all isSpace) subject

empty可能是错误的)。

并且toList应该只是map index.

于 2012-07-16T19:57:27.953 回答