Jim Snavely on 19 Oct 2011 15:57:29 -0700


[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

Continuations example in haskell


import Control.Monad.Cont

add :: Int -> Int -> Int
add x y = x + y

square :: Int -> Int
square x = x * x

pythagoras :: Int -> Int -> Int
pythagoras x y = add (square x) (square y)

add_cont :: Int -> Int -> Cont r Int
add_cont x y = return (add x y)

square_cont :: Int -> Cont r Int
square_cont x = return (square x)

pythagoras_cont :: Int -> Int -> Cont r Int
pythagoras_cont x y =
    do x_squared <- square_cont x
       y_squared <- square_cont y
       sum_of_squares <- add_cont x_squared y_squared
       return sum_of_squares

main = runCont (pythagoras_cont 5 6) print

On Oct 19, 2011 3:34 PM, "Kyle R. Burton" <kyle.burton@gmail.com> wrote:
I took the liberty of writing a few examples in ruby (since it has
continuations) that I could walk through during the discussion if
people are interested:

 https://github.com/kyleburton/sandbox/tree/master/examples/ruby/continuations

I try to show a 'normal' linear program, then one that's run by a
controller (inversion of control), and finally one with a very small
framework using continuations that allow the program to 're-invert'
the IOC and look linear again.  The fun thing is with that example you
can 'go back' w/o breaking the linearity of the code.  Hopefully I can
explain it better in person.

Kyle

On Tue, Oct 18, 2011 at 9:57 AM, Trevor Lalish-Menagh
<trev@trevreport.org> wrote:
> Hi all,
>
> Don't forget that tomorrow night is Functional Fall. We will be
> reading "Continuation, functions and jumps."
> (http://www.cs.bham.ac.uk/~hxt/research/Logiccolumn8.pdf)
>
> As always there will be pizza and cola provided, and beer afterwards.
>
> Don't forget to RSVP at http://www.doodle.com/dvd85t6iw8hs8vqp
>
> I hope to see you there!
>
> Yours,
> Trevor
> --
> Trevor Lalish-Menagh
> (484) 868-6150
> trev@trevmex.com
> http://www.trevmex.com
>



--
Twitter: @kyleburton
Blog: http://asymmetrical-view.com/
Fun: http://snapclean.me/