MondayMorningHaskell / haskellings

An automated tutorial to teach you about Haskell!
BSD 3-Clause "New" or "Revised" License
166 stars 25 forks source link

Typeclasses3 InterestRate deriving Read issue #38

Closed Rosuavio closed 3 years ago

Rosuavio commented 3 years ago

In the Typeclasses3

https://github.com/MondayMorningHaskell/haskellings/blob/6bf3510e1bfd15f190de2e899699113b04ac2954/exercises/typeclasses/Typeclasses3.hs#L56-L58

these directions say to simply "Derive" Read. I would think that means simply added Read to the list of derivings resulting in

 -- Derive both 'Ord' and 'Read' for this type. 
 newtype InterestRate = InterestRate Double 
   deriving (Eq, Show, Ord, Read) 

But when I do that I get this error.

Tests failed on exercise : Typeclasses3.hs

Typeclasses3
  Ordering Adults 1:      OK
  Ordering Adults 2:      OK
  Read Interest Rate:     FAIL
    Exception: Prelude.read: no parse
  Higher Interest Rate 1: OK
  Higher Interest Rate 1: OK

1 out of 5 tests failed (0.00s)

I am not sure if I am doing something wrong as the user, ~or if maybe it was intended to instruct the users to implement Read,~ or if this is supposed to work and is not for me for some reason.

Edit: Implementing Read seems too complicated for this exercise.

AR2202 commented 3 years ago

To my knowledge, the desired implementation for the Read typeclass can only be derived when the language extensions GeneralizedNewtypeDeriving and DerivingStrategies are enabled.

To fix this problem, I would suggest one of the following:

a.) adding {-# LANGUAGE GeneralizedNewtypeDeriving, DerivingStrategies #-} at the top of the file - but even with this modification, solving the exercise might be beyond the scope of this tutorial or: b.) changing the Unit test to: testCase "Read Interest Rate" $ map read ["InterestRate 0.5", "InterestRate 0.3", "InterestRate 0.788"] @?= [InterestRate 0.5, InterestRate 0.3, InterestRate 0.788]

(Just to be clear, these suggestions are for the maintainers, not for the user. I'm happy to make these changes if no one else does.)

Rosuavio commented 3 years ago

I think maybe the best solution is b, I don't think having exercises use language extensions without explaining them would allow the user to properly understand the problem when they find a solution and I don't think that this exercise is a good place to introduce language extensions.