Monday, October 7, 2013

Lesson 26 - Monadic composition

Today was a day of many firsts.

I had installd iTerm2 on her machine, so that we can easily split the terminal into two panes. It was great fun splitting and closing panes, and switching between them. We will use this skill for great good on the next lesson. This time it was just to get a fun start for a heavy lesson.

Because next we went straight into the world of Haskell and GHCI.

We started by recapping how to run a sequence of actions (I still haven't come up with a good Finnish word for action) using the predefined s function. Actually the s function is a synonym for sequence and part of a set of helpers I've defined for both Haskell and Turtle Roy to make programming a bit faster.

    s [say "mila",say "elena"]

She remembered the list syntax quite well, and autonomically closed the strings and the list with quotes/brackets.

Next, we tried a new function that's also part of my "Napero" predefs for Haskell.


Here i is a shorthand for getLine which reads a line of user input and returns it. The story was that now the computer is asking you a question and you should answer by writing your name. It doesn't actually do much yet. But when "piping" it to say, it's already quite cool. So

    i >>= say

Just like piping in bash, but using a monadic bind (I suppressed the urge to explain monads at this time). Then we celebrated her first Interactive Computer Program!

But that was not enough. We went for the next goal: the first parametric function. So, we wrote a new function "hello" which has a parameter "name":

    let h n = s [say "hello", say n]


    h "mila"

And the computer said "hello mila". And finally

    i >>= h


I kept on explaining how the parameter thing works but I'm quite sure it will take several recaps to nail it. Finally we had a little Parametericity Quiz, played a bit with iTerm splits and that was it.

1 comment:

  1. action = toiminto? Tai ehkä vähemmän jäykästi teko, tapahtuma, käsky, toimi (harvoin käytetty, ei mene sekaisin...), ...

    sequence of actions vois olla "sarja käskyjä" tai "monta toimintoa jonossa". En o pedagogi, mutta varmaan tekee silti hyvää kielelliselle kehitykselle käyttää suomennoksia vaikka (aikuisittain) puoliväkisin. Sanat assosioituu silloin tiiviimmin kuin jos kaikelle on oma puolienglanninkielinen sana...