I need help translating the concept of OO into Haskell.
Imagine class Vehicleand subclasses Car, and Truckby a method driveOneMile, which returns the binary representation the total fuel used. Each call driveOneMilealso changes the internal state of the vehicle.
This is what I have done so far in Haskell (since there are no instance variables in Haskell, it seems to me that I should create my own state types):
type CarState = (Double,Double)
initialCarState = (0,0)
driveCarOneMile :: CarState -> (Double,CarState) --Double: total fuel used
driveCarOneMile s = ...
--the internal state of trucks is more complex. needs three Doubles
type TruckState = (Double,Double,Double)
initialTruckState = (0,0,0)
driveTruckOneMile :: TruckState -> (Double,TruckState)
driveTruckOneMile s = ...
In a similar way, I could build other vehicles and their “drive” functions.
Here's how I drove a car twice:
fuelFor2Miles = fst $ driveCarOneMile $ snd $ driveCarOneMile initialCarState
- Am I doing it right?
- If not, how do you fix it?
- ( ), , , 10 [] ? ( OO , 10 .)