[plt-scheme] purely function call/cc example

From: Matthias Felleisen (matthias at ccs.neu.edu)
Date: Wed Feb 3 09:46:11 EST 2010

Functional programming with call/cc is just fine. But, as much as we understand the advantages of FP, we aren't ideologues; we use assignment statement when needed. 

Try to write a timer-based thread-like abstraction with call/cc and you will see that (1) state is your friend and (2) Python's yield has much less expressive power. 

-- Matthias





On Feb 3, 2010, at 9:27 AM, Francesco Abbate wrote:

> Hi all,
> 
> I'm new to scheme and I was trying to better understand the 'call-with-current-continuation' feature. I was surprised of the imperative style used in the examples with set!. I've actually written a purely functional example and I was willing to have your opinion about this style of programming.
> 
> ---------------
> (define (parcour test? lst return)
>   (define f-return (foldl
>      (lambda (element c-return)
>        (call/cc (lambda (here) 
>                   (if (test? element) 
>                       (c-return (cons element here)) 
>                       c-return))))
>      return lst))
>   (f-return 'end))
> 
> (define (add-odd-number lst)
>   (define (iter-rec p accu)
>     (match p
>       ['end accu]
>       [(cons v k-new) (let ([p-new (call/cc k-new)])
>                         (iter-rec p-new (+ accu v)))]))
>   (define p0 (call/cc (lambda(k) (parcour odd? lst k))))
>   (iter-rec p0 0)
> )
> 
> (add-odd-number '(0 1 2 3 4 5 6 7 8 9 13 27 98 101))
> -------------------
> 
> Othewise I have a question:
> Python feature the 'yield' keyword that allows to create very easily coroutines and use them in a for loop. I understood that scheme allows this kind of programming with call/cc but it seems to me that it does require a significant amout of boiler-plate code and I was wondering if there is a short direct syntax similar to the 'yield' of Python.
> 
> Best regards,
> Francesco
> _________________________________________________
>  For list-related administrative tasks:
>  http://list.cs.brown.edu/mailman/listinfo/plt-scheme



Posted on the users mailing list.