cogniblox

Code Room

Working with lists

Haskell treats a list as a stream of values to push through functions.

  • map applies a function to every item.
  • filter keeps only the items that pass a test.
map (*2) [1,2,3]
filter even [1..10]

The first line gives [2,4,6]. The second gives [2,4,6,8,10].

A FILTER Blox does the same job: items go in, and only the ones that pass come out.

Predict the output

filter (>3) [1,5,2,8]
Show answer
[5,8]

Predict the output

sum (map (^2) [1,2,3])
Show answer
14