typestar

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

  1. trailName :: String and trailMiles :: Double sit directly above the values they describe.
  2. restStops is an Int, so fromIntegral widens it before the division.
  3. A comment notes that GHC would infer the Double for milesPerLeg without any help.

Keywords and builtins used here

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.

Type this snippet

Step 1 of 3 in Values & types, step 1 of 27 in Language basics.

Next →