About Me

Friday, March 29, 2013

On CPS Part 1

"O, manyHave broke their backs with laying manors on 'emFor this great journey. What did this vanityBut minister communication ofA most poor issue?"
-- ShakespeareHenry VIII
Act I, Scene I
Lines 83-87

I've moved further along in my journey of functional programming. I find myself in a densely wooded forest of colossal pine trees. The smell of juniper fills the air, bring with it memories of winters gone by. This forest is where continuation lives.

Continuation-passing style is programming with no return, literally there is no return statement when you use continuation-passing style (CPS). Instead what you do is pass in the function which you would like the function you are calling to return its result to.

Simple CPS example

Say you have function called f and you want f to take what ever value you give it and pass it to another function you give it that well call k.

Now let's make the fizzBuzzer using these functions. We'll also use List.choose so we'll have either ["Fizz"], ["Buzz"], ["Fizz";"Buzz"], or [] as a result of applying fizzer and buzzer against the numerical input. We'll then use List.reduce to concat the possible ["Fizz";"Buzz"] result.

The great thing about CPS with TDD is that you can pass what you are testing its own test case (see Full FizzBuzz example below). I'll have more on CPS, once I wrap my head around it fully. I am working on understanding one of the best responses I've seen on StackOverflow.