gpt4 book ai didi

database - 不在范围 : data constructor `Song' - Haskell 内

转载 作者:行者123 更新时间:2023-12-02 02:43:49 26 4
gpt4 key购买 nike

type Song = (String, String, Int) --(title, artist, sales)

database :: [Song]
database = [("Amon Amarth","Ravens flight", 1),
("Amon Amarth","Shield wall", 11),
("Amon Amarth","The way of vikings", 105),
("Elijah Nang","Journey to the west", 1000),
("Elijah Nang","Tea house", 7),
("Pink Floyd","Wish you were here", 123),
("Amon Amarth","Raise your horns", 9001),
("NLE Choppa","Walk 'em down'", 69420),
("Elijah Nang","Kumite", 1337),
("NLE Choppa","Shotta flow 6", 511),
("Pink Floyd","Comfortably numb", 9),
("Pink Floyd","Shotta flow 6", 711), -- changed to match the name of an nle choppa song as requested
("Johannes Chrysostomus Wolfgangus Theophilus Mozart","Requiem", 10203948),
("Elijah Nang","Kenjutsu water style", 1),
("NLE Choppa","Shotta flow 5", 1),
("Pink Floyd","High hopes", 1),
("Amon Amarth","Deceiver of the gods", 1),
("Johannes Chrysostomus Wolfgangus Theophilus Mozart","Turkish march", 1),
("Chance The Rapper","Cocoa butter kisses", 1),
("Chance The Rapper","Favourite song", 1),
("Chance The Rapper","Hot shower", 1),
("Chance The Rapper","High hopes", 1)]


getTrackSale :: Int -> String -> String -> String --(index, artist, track, sales)
getTrackSale index artist track
| ((getArtist(database!!index) == artist) && (getTrack(database!!index) == track)) = getTrackSale(database!!index)
| otherwise = getTrackSale(index + 1 artist track)


task2 = getTrackSale(0 "Chance The Rapper" "Hot Shower")

getArtist :: Song -> String
getArtist (Song y _ _) = y

getTrack :: Song -> String
getTrack (Song _ z _) = z

getSale :: Song -> Int
getSale (Song _ _ x) = x

我无法弄清楚这意味着什么或如何解决它,我之前已经为三个“get”函数编写了相同的函数,并且它们没有遇到任何问题,但我之前确实使用了“type”声明,所以我想就是那个。我有使用相同类型声明的示例代码,它工作得很好,所以我在这里有点迷失。

最佳答案

你写

type Song = (String, String, Int)

这意味着Song(String, String, Int)类型同义词。也就是说,Song 实际上只是一个有序三元组,使用有序三元组构造函数 (,,) 构造。但后来你写了

getArtist :: Song -> String
getArtist (Song y _ _) = y

getTrack :: Song -> String
getTrack (Song _ z _) = z

getSale :: Song -> Int
getSale (Song _ _ x) = x

(不存在的)Song 数据构造函数上的模式匹配。

您可以通过编写来坚持使用类型同义词

getArtist :: Song -> String
getArtist (y, _, _) = y

等等。或者您可以将 Song 设为自己的数据类型:

data Song = Song
{ getArtist :: String
, getTrack :: String
, getSale :: Int }

在这种情况下,您必须修改database 定义才能使用Song 构造函数。使用自定义数据类型声明更加惯用并且更加自文档化,并且它使 Haskell 的类型系统可以做更多的工作来帮助您发现代码中的错误。作为一般规则,我建议 Haskell 初学者完全避免类型同义词,而更有经验的 Haskell 程序员在某些特殊情况下很少使用它们。特别是,类型同义词与 TypeFamilies 扩展结合使用非常有用,与 ConstraintKindsRankNTypes 扩展结合使用也有些用处,但除此之外根据我的经验,这些内容往往令人困惑而不是有帮助。

关于database - 不在范围 : data constructor `Song' - Haskell 内,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63125243/

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