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

Re: [Paul Graham <paulgraham@yahoo.com>] Re: What is a lightweight language




>> Paul writes:
>>
>>   As in many uses of continuations, you don't actually need
>>   call/cc-- you can fake continuations using carefully constructed
>>   closures-- but having call/cc makes it cleaner and easier.

>> Matthias Felleisen <matthias@ccs.neu.edu> writes:
>>
>> It's not just cleaner and easier, it maintains invariants automatically. If
>> your programmer abuses your patterns just a bit, all kind of things can
>> happen and you don't even know.

> At 12:14 PM -0500 12/13/01, jmarshall@mak.com wrote:
> 
>      
> I'm not sure I understand what you mean here.  I thought that Paul was
> suggesting using CPS in lieu of call-with-current-continuation.  I
> don't see CPS as being any `dirtier' that call-with-current-continuation.


John Clements <clements@brinckerhoff.org> writes:

> It's "dirtier" because programming in a CPS discipline requires that
> the whole program (libraries, etc.) obey the CPS invariants.  So,
> suppose I want to write a short-cutting 'andmap' that accepts a
> function and a list (uh, in scheme):
> 
> ; andmap : ('a -> boolean) (listof boolean) -> boolean
> 
> (define (andmap fn lst)
> 
>   (call/cc
>    (lambda (escape)
>      (letrec ([loop
>                (lambda (remaining)
>                  (cond [(null? remaining) #t]
>                        [(fn (car remaining)) (loop (cdr remaining))]
>                        [else (escape #f)]))])
>        (loop lst)))))
> ; test expression
> (and
>   (andmap (lambda (x) (= x 9)) '(9 9 9))
>   (not (andmap (lambda (x) (= x 9)) '(9 4 a))))

There is no need for the escape continuation.

(define (andmap fn list)
  (or (and (pair? list)
           (fn (car list))
           (andmap fn (cdr list)))
      (null? list)
      (error "Improper list.")))

I don't mean this as a nitpick.  I find it extremely rare that I need
to use call-with-current-continuation because CPS doesn't cut it. 

> ... unless you were just talking about using a CPS-ing compiler to implement call/cc.

No, that would be infeasible.