在Haskell中的if语句上解析错误

时间:2018-07-26 23:53:20

标签: parsing haskell if-statement

这是一项学术任务,我根本无法解决。

我有(音乐记录的)“曲目”的列表(数据库)-(标题,艺术家,SalesNumber)的元组,我的任务是如果数据库中已经存在给定曲目,则增加其销售量(相同的标题和艺术家),只需将销售数字加1,或者如果尚不存在,则将其添加到数据库中。我已经写了-我认为正确-执行这些任务之一的函数,但是正在努力编写该函数来确定是添加新记录还是简单地增加记录并调用其中一个。

addNewTrack :: [Sale] -> Title -> Artist -> [Sale]
addNewTrack testDatabase title artist = testDatabase

incrExistingTrack :: [Sale] -> Title -> Artist -> [Sale]
incrExistingTrack testDatabase testedTitle testedArtist = []
incrExistingTrack ((title, artist, salesNumber): xs) testedTitle    testedArtist
 | testedTitle == title && testedArtist == artist =
 [(title, artist, salesNumber + 1)]
 | otherwise = incrExistingTrack xs title artist

recordSale :: [Sale] -> Title -> Artist -> [Sale]
recordSale testDatabase title artist
let trackExists = sameTrack title artist
if trackExists == True
 then incrExistingTrack title artist
 else addNewTrack title artist

sameTrack :: Title -> Artist -> Sale -> Bool
sameTrack queriedTitle queriedArtist (title, artist, salesNumber)
 | (queriedTitle == title) && (queriedArtist == artist) = True
 | otherwise = False

我的recordSale函数中的if语句在函数第四行的第一个字符上给出“解析错误(可能是缩进不正确或括号不匹配)”是怎么回事?

recordSale :: [Sale] -> Title -> Artist -> [Sale]
recordSale testDatabase title artist
let trackExists = sameTrack title artist
if trackExists == True
  then incrExistingTrack title artist
  else addNewTrack title artist

我一直在转移东西,缩进都无济于事。我确信这一定是一个非常简单的错误。我如何识别问题?我正在尝试做的事情是否有替代方法,并且可能更优雅?

recordSale :: [Sale] -> Title -> Artist -> [Sale]
recordSale testDatabase title artist
  = let trackExists = sameTrack title artist
    in if trackExists == True
        then incrExistingTrack testDatabase title artist
        else addNewTrack testDatabase title artist

1 个答案:

答案 0 :(得分:0)

您给sameTrack输入的参数太少了。应该是sameTrack testDatabase title artist

还要注意,trackExists == True是冗长的。可以只是trackExists