gpt4 book ai didi

haskell - Haskell 中的运行时错误

转载 作者:行者123 更新时间:2023-12-03 09:03:24 25 4
gpt4 key购买 nike

我的程序是 在元组列表中搜索 我写成如下

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
这是什么意思?

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

最佳答案

函数定义中的模式

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 .

关于haskell - Haskell 中的运行时错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11511536/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com