runtime error in Haskell

跟風遠走 提交于 2019-12-11 03:12:28

问题


My program is searching in list of tuples I wrote it as the following

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)]]

and clean it from any syntax error but after i try to enter to a list of tuple to combining() it gve me runtime error say

Exception:Not Exhaustive pattern in function Main.combining

What does this mean?

Please, give me only instructions. If possible, I would like to fix it by my self.


回答1:


The pattern in the function definition

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

matches only lists with exactly one element. You have a similar problem in bubbleSort, where

bubbleSort[x,y,z,xs]=

matches only lists with exactly four elements and elsewhere.

I haven't figured out how you intend it to work, perhaps using a variable pattern (that matches all arguments)

combining books = superBubble (toList books)

would be appropriate?


I suspect

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)]

should really be

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

(empty may be wrong).

And toList should probably be just map index.



来源:https://stackoverflow.com/questions/11511536/runtime-error-in-haskell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!