Ranges in Haskell
[6 .. 10] counts up by one; naming the first two elements picks any other step.
module Ranges where
-- [a .. b] counts up one step at a time and stops at b
serviceHours :: [Int]
serviceHours = [6 .. 10]
-- naming the first two elements sets the step for the whole range
quarterPast :: [Int]
quarterPast = [0, 15 .. 60]
-- ranges are lazy, so an endless one is fine if you only take a slice
nextDepartures :: [Int]
nextDepartures = take 5 [7, 20 ..]
-- replicate repeats a single value a fixed number of times
placeholders :: [String]
placeholders = replicate 3 "--:--"
How it works
serviceHours = [6 .. 10]runs from 6 through 10, stopping at the upper bound.quarterPast = [0, 15 .. 60]takes its step of 15 from the gap between the first two elements.take 5 [7, 20 ..]slices five values off an endless range, andreplicate 3 "--:--"repeats one value instead of counting.
Keywords and builtins used here
IntStringmodulenextDeparturesplaceholdersquarterPastserviceHourswhere
The run, in numbers
- Lines
- 17
- Characters to type
- 492
- Tokens
- 60
- Three-star pace
- 80 tpm
At the three-star pace of 80 tokens a minute, this run takes about 45 seconds.
Step 2 of 3 in Lists, step 11 of 27 in Language basics.