//combinators
CruiseSpeed = int
CruiseSpeed getNextCruiseSpeed(bool cruiseControl, bool cruiseSpeedSet, int cruiseSpeed, int currentSpeed) {
if cruiseControl && cruiseSpeedSet
return cruiseSpeed - 1
elif cruisControl && !cruiseSpeedSet
return currentSpeed
else
return cruiseSpeed
}
bool getNextCruiseSpeedSet(bool cruiseControl, bool cruiseSpeedSet){
return cruiseControl && !cruiseSpeedSet ? true : cruiseSpeedSet
}
//IO functions
void getCurrentSpeed()
bool isCruiseSpeedSet()
void setCruisSpeedSet(bool cruiseSpeedSet)
void writeSpeedToEngineComputer(CruiseSpeed x)
void handleButtonPress(bool cruiseControl, bool cruiseSpeedSet, int cruiseSpeed, int currentSpeed){
speed = getNextCruiseSpeed(cruiseControl, cruiseSpeedSet, cruiseSpeed, currentSpeed)
cruiseSpeedSet = getNextCruiseSpeedSet(cruiseControl, cruiseSpeedSet)
writeSpeedToEngineComputer(speed)
setCruiseSpeedSet(cruiseSpeedSet)
}
Your example uses free variables extensively. It means that all your logic cannot move outside of the context of the free variable which means they cannot be reused. Even IO functions should not use external context.Combinators can be reused everywhere.