2

in "The Scheme Programming Language 4th Edition", there is a example as below:


(define product
  (lambda (ls)
    (call/cc
      (lambda (break)
        (let f ([ls ls])
          (cond
            [(null? ls) 1]
            [(= (car ls) 0) (break 0)]
            [else (* (car ls) (f (cdr ls)))]))))))
(product '(1 2 3 4 5)) => 120

(product '(7 3 8 0 1 9 5)) => 0

later it is converted into CPS in 3.3 as below


(define product
  (lambda (ls k)
    (let ([break k])
      (let f ([ls ls] [k k])
        (cond
          [(null? ls) (k 1)]
          [(= (car ls) 0) (break 0)]
          [else (f (cdr ls)
                   (lambda (x)
                     (k (* (car ls) x))))])))))
(product '(1 2 3 4 5) (lambda (x) x)) => 120

(product '(7 3 8 0 1 9 5) (lambda (x) x)) => 0

I want to do it myself, The corresponding CPS is below


 (define (product ls prod break)
    (cond
      ((null? ls)
       (break prod))
      ((= (car ls) 0)
       (break 0))
      (else
        (product (cdr ls) (* prod (car ls)) break))))
(product '(1 2 3 4 5) 1 (lambda (x) x)) => 120

(product '(1 2 0 4 5) 1 (lambda (x) x)) => 0

I want to ask my CPS is right? T Thanks in advance!

BEST REGARDS

hammar
  • 138,522
  • 17
  • 304
  • 385
abelard2008
  • 1,984
  • 1
  • 20
  • 35
  • 1
    Where are your test cases? This is not meant to be a flippant question. If you don't know how to call a CPS'ed function, you've missed something important. – dyoo Jul 05 '12 at 03:00
  • Also, you should note that the function that you've CPSed is not the original "product" function, but one that's written with a standard accumulator. **(define (p l acc) (if (null? l) acc (p (cdr l) (* acc (car l))))** is not exactly the function you transformed, but it's close. (My definition of **f** just omits the early escape when we hit a zero.) In any event, the function that you transformed exhibited iterative recursion, and you can see this because your CPSed version of it doesn't need to construct new continuations. – dyoo Jul 05 '12 at 03:10
  • the original case is here :http://www.scheme.com/tspl4/further.html#./further:h4 – abelard2008 Jul 09 '12 at 02:35

1 Answers1

1

I think this is the correct implementation :

(define inside-product #f)  ;; to demonstrate the continuation

(define (product ls prod break)
  (cond
    ((null? ls)
     (begin
       (set! inside-product prod)
       (prod 1)))
    ((= (car ls) 0)
     (break 0))
    (else
     (product (cdr ls) (lambda (x) (prod (* (car ls) x))) break))))

(define identity (lambda (x) x))

The idea of CPS is to keep a track of the recursion.

> (product (list 1 2 3) identity identity)
6
> (inside-product 4)
24
dyoo
  • 11,795
  • 1
  • 34
  • 44
Rajesh Bhat
  • 980
  • 6
  • 8
  • Unfortunately, I have to -1 this: there's not a use of set! in the original code, and the use of mutation here confuses issues. – dyoo Jul 05 '12 at 02:59
  • I used the set! on purpose, in order to demonstrate the continuation. When the end of the list is reached, the value of inside-product is set to the continuation. So, once the product procedure is executed as shown in the example, the value of inside-product is set to (lambda (x) ((lambda (x) ((lambda (x) (identity (* 1 x))) (* 2 x))) (* 3 x))) – Rajesh Bhat Jul 05 '12 at 12:43
  • Hmmm... Ok. The question that the original questioner asked is still ambiguous, as he or she needs to say what function was transformed to CPS. From what I can tell, *prod* is supposed to be an accumulator in the original question. Yet there's no original source code pre-CPS-transform in the question that corresponds to a use of *prod* this way. So something is missing from the question. – dyoo Jul 06 '12 at 23:20
  • 1
    In my original question, prod is a number 1, so, I can call "product" like this : (product '(1 2 3 4 5) 1 (lambda (x) x)) => 120, maybe, my understanding about CPS is wrong, Where I can study CPS step by step, Thanks in advance! – abelard2008 Jul 09 '12 at 02:46
  • You can look at textbooks such as Programming Languages: Application and Interpretation, as well as Essentials of Programming Languages. Both have chapters on CPS, if I'm remembering right. Google for "PLAI" and you should find the first textbook online. – dyoo Jul 09 '12 at 13:57