0
votes
totalPrice :: Product -> Integer -> Float    
totalPrice product x = case product of  

    Ramen    
    | x <= 10 -> 2 * x    
    | x <= 30 -> 1.8 * x   
    | x <= 100 -> 1.5 * x   
    | x <= 200 -> 1.4 * x   
    | x > 200 -> 1.35 * x   
    | otherwise -> 0   

Chips
    | x <= 2 -> 3 * x
    | x <= 5 -> 2.95 * x
    | x <= 10 -> 2.7 * x
    | x <= 20 -> 2.5 * x
    | x > 20 -> 2.35 * x
    | otherwise -> 0

When I compile this code it get error "Couldn't match expected type ‘Double’ with actual type ‘Integer’"

Any suggestions?

1
paste the code as text, and make sure you indent the code.ymonad
As a rule of thumb to help you decide what to look at when debugging problems in future: if you get a type error ("cannot match expected type ... with actual type ..."), it usually means that there is no issue with your syntax. In this example the problem had noting to do with guards or cases, you just used an operation that wasn't supported by your type. If you had a problem with the guard or case syntax Haskell wouldn't be able to figure out enough of what you were trying to do to even begin checking types.Ben

1 Answers

3
votes

x is an Integer, and you are multiplying it with decimal values to produce a Float. The operands and result of arithmetic operators like (*) must be of the same type, and you must convert between types explicitly.

totalPrice product y = case product of
  Ramen
    | x <= 10 -> 2 * x
    ...
  where
    x = fromInteger y