typestar

Strings and chars in Haskell

Reshapes a gate announcement, since a String is just a list of characters.

-- A String is exactly [Char], so every list function already works.
module Announcement where

import Data.Char (isDigit, toUpper)

headline :: String
headline = "gate 7 boards at 1450"

-- words splits on whitespace; unwords glues the pieces back together.
pieces :: [String]
pieces = words headline

shouted :: String
shouted = unwords (map (map toUpper) pieces)

digitsOnly :: String
digitsOnly = filter isDigit headline

banner :: String
banner = shouted ++ " [" ++ digitsOnly ++ "]"

How it works

  1. words headline splits the line on whitespace and unwords glues the pieces back.
  2. map (map toUpper) pieces reaches through the list of strings into every Char.
  3. filter isDigit headline keeps only the digits, using an ordinary list function.

Keywords and builtins used here

The run, in numbers

Lines
20
Characters to type
488
Tokens
67
Three-star pace
75 tpm

At the three-star pace of 75 tokens a minute, this run takes about 54 seconds.

Type this snippet

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

← Previous Next →