[plt-scheme] purely function call/cc example

From: Francesco Abbate (francesco.bbt at gmail.com)
Date: Wed Feb 3 09:27:55 EST 2010

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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20100203/f7efda5a/attachment.html>

Posted on the users mailing list.