Tuesday, September 17, 2013

Lesson 18 - IO Monad

I'm slipping. I forgot to post yesterday even though we had a lesson.

Yesterday we visited Haskell for the first time.

That was just because the "say" function in Turtle Roy didn't produce any speech though. Fortunately I had setup her GHCI so that it includes my special helper module Napero which provides easy single-letter shorthands to some common functions. For those not in the know, the Roy language used in Turtle Roy is syntactically very similar to Haskell, so we can switch between the two without struggling with different syntaxes.

The main thing of lesson 18 was the idea that you can define a list of actions and then sequence them into a single action that performs all of the actions on the list. So, in Haskell we did

    s [say "mila", say "ella"]

Where s is a shorthand for sequence and say is a wrapper for the command-line say command. Composition is a powerful tool! This is coding!

Next we started "teaching the computer". The idea was to teach the Turtle Roy to draw a rectangle, which consists of four equal parts. Each of the 4 parts proceeds 100 pixels and then turns right. We called this operation "askel" and taught the computer to do "askel" like this

    let askel = s [fd 100, rt 90]

When we had taught the computer, i.e. defined a function, we called askel 4 times and voilá, got the rectangle together!




Then we concluded that "askel" is almost like "haskell".

Next up: define the rectangle function.

The list syntax is something we'll be revisiting time after time until it feels natural to her.

2 comments: