Bindings & signatures in Haskell
A trail profile written as top-level bindings, each with its own type signature.
-- Every top-level binding gets its own type signature above it.
module TrailProfile where
trailName :: String
trailName = "Ridge Loop"
trailMiles :: Double
trailMiles = 8.4
restStops :: Int
restStops = 3
-- GHC would infer Double here on its own; the signature pins it down
-- and documents the binding for the next reader.
milesPerLeg :: Double
milesPerLeg = trailMiles / fromIntegral (restStops + 1)
summary :: String
summary = trailName ++ " runs " ++ show trailMiles ++ " miles"
How it works
trailName :: StringandtrailMiles :: Doublesit directly above the values they describe.restStopsis anInt, sofromIntegralwidens it before the division.- A comment notes that GHC would infer the
DoubleformilesPerLegwithout any help.
Keywords and builtins used here
DoubleIntStringmilesPerLegmodulerestStopssummarytrailMilestrailNamewhere
The run, in numbers
- Lines
- 19
- Characters to type
- 488
- Tokens
- 56
- Three-star pace
- 80 tpm
At the three-star pace of 80 tokens a minute, this run takes about 42 seconds.
Step 1 of 3 in Values & types, step 1 of 27 in Language basics.