cogniblox

Code Room

Functions are the building blocks

A function takes an input and gives back an output. It changes nothing else, which makes it a Blox you can trust.

Save this in a file called lesson1.hs, then run ghci lesson1.hs in a terminal.

double x = x * 2

Now ask GHCi to run it with double 21. It answers 42.

Snapping functions together

The . operator joins two functions. The output of the right-hand function becomes the input of the left-hand one.

addThree x = x + 3
pipeline = addThree . double

Read pipeline from right to left: double first, then add three.

Predict the output

pipeline 5
Show answer
13

Predict the output

double (double 3)
Show answer
12

Try the same idea with coloured blocks in the Blox Lab. It writes the Haskell for you as you build.