[Haskell-cafe] Coroutines

Robin Green greenrd at greenrd.org
Thu Dec 18 06:01:42 EST 2008


In my opinion, in Haskell, you don't need coroutines because you have
lazy evaluation.

You example below is simply an example of a heterogenous list being
read. The simplest way to implement a heterogenous list in Haskell is
to use a tuple. Or you could use the HList package.
-- 
Robin

On Thu, 18 Dec 2008 02:26:26 -0800
"Ryan Ingram" <ryani.spam at gmail.com> 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