1

A bike race can be represented as a list of bike riders. Each rider is recorded with their ability class (High or Low), name, and the times in minutes that they finished in in each of their races, in the following types:

data Ability = High | Low data RiderResults = Rider Ability String [Int] type Championship = [RiderResults] 

How to solve totalTime :: RiderResults -> Int which returns the total time taken by that rider in all their races, including any penalty. High ability riders are penalised by having a time double their actual time (eg, pre-penalty total time 70mins becomes 140mins).

I've done something like

 totalTime :: RiderResults -> Int totalTime (x:xs) = if x == Low then sum x*2 else x : totalTime xs 

but it is wrong. I have practice questions to solving types definition and data declarations in Haskell and this is one of them. Can someone please explain how to approach such questions?

1
  • 4
    When I'm stuck on something I often find that verbally articulating the problem in detail helps me to understand what I'm doing wrong. Try editing your question to include a detailed description of what each piece of your current totalTime function does. Commented May 19, 2018 at 15:44

1 Answer 1

5

A RiderResults value is not a list; it's a triple of an ability, a name and a list of times. You need to unpack that value first before you can sum up the times stored in the value.

totalTime (Rider ab _ times) = sum (map penalty times) where penalty = if ab == Low then id else (*2) 
Sign up to request clarification or add additional context in comments.

5 Comments

How come its not a list where RiderResults is in the form of [RiderResults]?
@Anna: id is the identity function.
But RiderResults is NOT "in the form of [RiderResults]", your type shows that totalTime only takes one RiderResults, so only one ability, one string, and one list of ints. You can use penalty p = if ab==Low then p else 2*p, which should be simpler to understand for a beginner.
Im still getting error whether using the id function or the simplified p
We can't help you if you don't tell us what error you are getting?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.