-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrocket.hs
More file actions
32 lines (24 loc) · 950 Bytes
/
rocket.hs
File metadata and controls
32 lines (24 loc) · 950 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import Data.Maybe (fromJust, fromMaybe)
data Rocket = Rocket {
rocketName :: String,
rocketFuel :: Int,
rocketLaunched :: Bool }
data LaunchError = LaunchError { launchErrorReason :: String }
type Launched a = Either LaunchError a
newRocket :: String -> Rocket
newRocket name = Rocket name 100 False
launchRocket :: Rocket -> Launched Rocket
launchRocket rocket
| rocketFuel rocket <= 25 = Left (LaunchError "Not enough fuel!")
| otherwise = Right (rocket { rocketLaunched = True })
showLaunchResult :: Launched Rocket -> String
showLaunchResult launch =
case launch of
Right successRocket -> "Rocket launched!"
Left (LaunchError errorReason) -> "Rocket failure: " ++ errorReason
doLaunch :: Rocket -> String
doLaunch rocket = showLaunchResult (launchRocket rocket)
badRocket :: Rocket
badRocket = (newRocket "BadRocket") { rocketFuel = 0 }
goodRocket :: Rocket
goodRocket = newRocket "CatRocket"