[Haskell-cafe] Coroutines

Miguel Mitrofanov miguelimo38 at yandex.ru
Thu Dec 18 05:42:21 EST 2008


First thing I've tried when learning Ruby was something like that:

================
def a
   yield {puts 1}
end

a {yield}
================

It didn't work. Can Coroutine.hs do something like that?

On 18 Dec 2008, at 13:26, Ryan Ingram wrote:

> On Thu, Dec 18, 2008 at 2:00 AM, Nicolas Pouillard
> <nicolas.pouillard at gmail.com> wrote:
>> I don't see why one would need session types, channels... to  
>> express that.
>> I maybe need a more complicated coroutines (ruby) example that  
>> would require
>> using this system.
>
> OK, how would you type these routines in Haskell?
>
> def simple
>    yield "hello"
>    yield 1
>    yield (lambda { |x| x + 1 })
> end
>
> def useSimple
>    state = 0
>    result = nil
>    simple { |x|
>        if (state == 0) then result = x
>        else if (state == 1) then result += (x * 4).toString
>        else if (state == 2) then result += x.call(10).toString
>        state = state + 1
>    }
>    result
> end
>
> I know it's a bit contrived, but you get the idea.
>
> In Haskell using Control.Coroutine:
>
> simple :: forall rest. Session (String :!: Int :!: (Int -> Int) :!:
> rest) rest ()
> simple = do
>    put "hello"
>    put 1
>    put (\x -> x + 1)
>
> useSimple :: forall rest. Session (String :?: Int :?: (Int -> Int) :?:
> rest) rest String
> useSimple = do
>    string <- get
>    int <- get
>    func <- get
>    return (string ++ show (int * 4) ++ show (func 10))
>
> result :: String
> result = snd $ connects simple useSimple
> -- result = "hello411"
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe



More information about the Haskell-Cafe mailing list