parsing - Parse Error on if statement in Haskell
问题描述
This is for an academic assignment and I simply cannot fix it.
I have a list (database) of "tracks" (of music records) - tuples of (Title, Artist, SalesNumber) and my task is to increment a sale of a given track if it is already present in the database (same title and artist) and just increment by 1 to sales number, or if is not already present, to add it to the database. I have written - I think correctly - functions to perform either of these tasks however am struggling to write the function which determines whether adding a new record or simply incrementing it and to call either.
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
What is wrong with the if statement in my recordSale function to give "parse error (possibly incorrect indentation or mismatched brackets)" on the first character of the fourth line of the function?
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
I have been shifting things around and changing indents to no avail. I am sure it must be a very simple error. How can I identify the problem? Is there an alternative and possibly more elegant implementation of what I'm trying to do?
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
解决方案
你sameTrack
提出的论点太少了。应该是sameTrack testDatabase title artist
。
另请注意,这trackExists == True
是冗长的。可以只是trackExists
。
推荐阅读
- spring - 没有上下文根路径的spring-webflux安全中的白名单端点
- sql - 在雪花中使用嵌套窗口函数
- woocommerce - 如何在 WooCommerce 中显示带有货币详细信息的金额的拼写
- python - 根据用户在 python 中输入的内容,是否有一种方法可以拆分溢出的方法?
- python - 如何附加到 python subdict 数组?
- python - 有没有办法在异常中捕获特定错误?
- lighting - 如何在eevee中实现硬阴影效果?
- javascript - Node.js:将请求选项转换为最终 URL
- javascript - 在 JavaScript 中有一种方法可以对多维数组中的元素进行求和,并根据数组中的其他元素分组
- hadoop - 将 Hadoop `FileStatus` 映射回 URI 基本相对路径