首页 > 解决方案 > 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

标签: parsinghaskellif-statement

解决方案


sameTrack提出的论点太少了。应该是sameTrack testDatabase title artist

另请注意,这trackExists == True是冗长的。可以只是trackExists


推荐阅读