Basically, i want to pattern match on the contents of a record, and then return a modification of said record. So i have this sort of situation cropping up a lot:
updateChr :: Database -> Database -> Database
updateChr db Database{mode=1, characters=chr} = db{characters=(map someFunc chr)}
updateChr db Database{mode=2, characters=chr} = db{characters=(map someOtherFunc chr)}
Where the two Database arguments should always be the same record. Is there a way i can do this while only passing the record once?
You can make use an as pattern [Haskell-report]:
updateChr :: Database -> Database
updateChr db@Database{mode=1, characters=chr} = db {characters=(map someFunc chr)}
updateChr db@Database{mode=2, characters=chr} = db {characters=(map someOtherFunc chr)}
If you however always want to map the characters
, you can here make use a guard:
updateChr :: Database -> Database
updateChr db@Database{mode=m, characters=chr} = db {characters=map f chr}
where f | m == 1 = someFunction
| otherwise = someOtherFunction