typestar

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

  1. serviceHours = [6 .. 10] runs from 6 through 10, stopping at the upper bound.
  2. quarterPast = [0, 15 .. 60] takes its step of 15 from the gap between the first two elements.
  3. take 5 [7, 20 ..] slices five values off an endless range, and replicate 3 "--:--" repeats one value instead of counting.

Keywords and builtins used here

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.

Type this snippet

Step 2 of 3 in Lists, step 11 of 27 in Language basics.

← Previous Next →

Ranges in other languages