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
words headlinesplits the line on whitespace andunwordsglues the pieces back.map (map toUpper) piecesreaches through the list of strings into every Char.filter isDigit headlinekeeps only the digits, using an ordinary list function.
Keywords and builtins used here
StringbannerdigitsOnlyheadlineimportisDigitmodulepiecesshoutedtoUpperwhere
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.
Step 3 of 3 in Values & types, step 3 of 27 in Language basics.