user-0 提问: 各位 一个问题 最长想了多久 ...

回复:
user: user-1
content: 其实停下来是最可怕的，可以找人问下，如果不是特别着急，可以先放着，继续学习，等过段时间回去看，可能就明白了

user: user-1
content: 引用 1 楼 user-10 的回复:  ;; A simple CPS transformer which does proper tail-call and does not ;; duplicate contexts for if-expressions.  ;; author: Yin Wang (yw21@cs.indiana.edu)   (load "pmatch.scm")   (define cps   (lambda (exp)     (letrec         ([trivial? (lambda (x) (memq x '(zero? add1 sub1)))]          [id (lambda (v) v)]          [ctx0 (lambda (v) `(k ,v))]      ; tail context          [fv (let ([n -1])                (lambda ()                  (set! n (+ 1 n))                  (string->symbol (string-append "v" (number->string n)))))]          [cps1           (lambda (exp ctx)             (pmatch exp               [,x (guard (not (pair? x))) (ctx x)]               [(if ,test ,conseq ,alt)                (cps1 test                      (lambda (t)                        (cond                         [(memq ctx (list ctx0 id))                          `(if ,t ,(cps1 conseq ctx) ,(cps1 alt ctx))]                         [else                          (let ([u (fv)])                            `(let ([k (lambda (,u) ,(ctx u))])                               (if ,t ,(cps1 conseq ctx0) ,(cps1 alt ctx0))))])))]               [(lambda (,x) ,body)                (ctx `(lambda (,x k) ,(cps1 body ctx0)))]               [(,op ,a ,b)                (cps1 a (lambda (v1)                          (cps1 b (lambda (v2)                                    (ctx `(,op ,v1 ,v2))))))]               [(,rator ,rand)                (cps1 rator                      (lambda (r)                        (cps1 rand                              (lambda (d)                                (cond                                 [(trivial? r) (ctx `(,r ,d))]                                 [(eq? ctx ctx0) `(,r ,d k)]  ; tail call                                 [else                                  (let ([u (fv)])                                    `(,r ,d (lambda (,u) ,(ctx u))))])))))]))])       (cps1 exp id))))     ;;; tests  ;; var (cps 'x) (cps '(lambda (x) x)) (cps '(lambda (x) (x 1)))   ;; no lambda (will generate identity functions to return to the toplevel) (cps '(if (f x) a b)) (cps '(if x (f a) b))   ;; if stand-alone (tail) (cps '(lambda (x) (if (f x) a b)))   ;; if inside if-test (non-tail) (cps '(lambda (x) (if (if x (f a) b) c d)))   ;; both branches are trivial, should do some more optimizations (cps '(lambda (x) (if (if x (zero? a) b) c d)))   ;; if inside if-branch (tail) (cps '(lambda (x) (if t (if x (f a) b) c)))   ;; if inside if-branch, but again inside another if-test (non-tail) (cps '(lambda (x) (if (if t (if x (f a) b) c) e w)))   ;; if as operand (non-tail) (cps '(lambda (x) (h (if x (f a) b))))   ;; if as operator (non-tail) (cps '(lambda (x) ((if x (f g) h) c)))   ;; why we need more than two names (cps '(((f a) (g b)) ((f c) (g d))))    ;; factorial (define fact-cps   (cps    '(lambda (n)       ((lambda (fact)          ((fact fact) n))        (lambda (fact)          (lambda (n)            (if (zero? n)                1                (* n ((fact fact) (sub1 n))))))))))  ;; print out CPSed function (pretty-print fact-cps) ;; => ;; '(lambda (n k) ;;    ((lambda (fact k) (fact fact (lambda (v0) (v0 n k)))) ;;     (lambda (fact k) ;;       (k ;;        (lambda (n k) ;;          (if (zero? n) ;;            (k 1) ;;            (fact ;;             fact ;;             (lambda (v1) (v1 (sub1 n) (lambda (v2) (k (* n v2)))))))))) ;;     k))   ((eval fact-cps) 5 (lambda (v) v)) ;; => 120    说实话这个代码就足够我看半个月了。   最开始学Java的时候，一个问题思考半个月都太正常了，具体什么问题就不记得了。

user: user-0
content: 引用 4 楼 lovesmiles 的回复:那说明基础的程度还未够看源码的程度。 每天 都能再看懂一些东西 ... 感觉还是有希望的 。。。龟速前进中 ，，

user: user-0
content: 引用 1 楼 user-10 的回复:  ;; A simple CPS transformer which does proper tail-call and does not ;; duplicate contexts for if-expressions.  ;; author: Yin Wang (yw21@cs.indiana.edu)   (load "pmatch.scm")   (define cps   (lambda (exp)     (letrec         ([trivial? (lambda (x) (memq x '(zero? add1 sub1)))]          [id (lambda (v) v)]          [ctx0 (lambda (v) `(k ,v))]      ; tail context          [fv (let ([n -1])                (lambda ()                  (set! n (+ 1 n))                  (string->symbol (string-append "v" (number->string n)))))]          [cps1           (lambda (exp ctx)             (pmatch exp               [,x (guard (not (pair? x))) (ctx x)]               [(if ,test ,conseq ,alt)                (cps1 test                      (lambda (t)                        (cond                         [(memq ctx (list ctx0 id))                          `(if ,t ,(cps1 conseq ctx) ,(cps1 alt ctx))]                         [else                          (let ([u (fv)])                            `(let ([k (lambda (,u) ,(ctx u))])                               (if ,t ,(cps1 conseq ctx0) ,(cps1 alt ctx0))))])))]               [(lambda (,x) ,body)                (ctx `(lambda (,x k) ,(cps1 body ctx0)))]               [(,op ,a ,b)                (cps1 a (lambda (v1)                          (cps1 b (lambda (v2)                                    (ctx `(,op ,v1 ,v2))))))]               [(,rator ,rand)                (cps1 rator                      (lambda (r)                        (cps1 rand                              (lambda (d)                                (cond                                 [(trivial? r) (ctx `(,r ,d))]                                 [(eq? ctx ctx0) `(,r ,d k)]  ; tail call                                 [else                                  (let ([u (fv)])                                    `(,r ,d (lambda (,u) ,(ctx u))))])))))]))])       (cps1 exp id))))     ;;; tests  ;; var (cps 'x) (cps '(lambda (x) x)) (cps '(lambda (x) (x 1)))   ;; no lambda (will generate identity functions to return to the toplevel) (cps '(if (f x) a b)) (cps '(if x (f a) b))   ;; if stand-alone (tail) (cps '(lambda (x) (if (f x) a b)))   ;; if inside if-test (non-tail) (cps '(lambda (x) (if (if x (f a) b) c d)))   ;; both branches are trivial, should do some more optimizations (cps '(lambda (x) (if (if x (zero? a) b) c d)))   ;; if inside if-branch (tail) (cps '(lambda (x) (if t (if x (f a) b) c)))   ;; if inside if-branch, but again inside another if-test (non-tail) (cps '(lambda (x) (if (if t (if x (f a) b) c) e w)))   ;; if as operand (non-tail) (cps '(lambda (x) (h (if x (f a) b))))   ;; if as operator (non-tail) (cps '(lambda (x) ((if x (f g) h) c)))   ;; why we need more than two names (cps '(((f a) (g b)) ((f c) (g d))))    ;; factorial (define fact-cps   (cps    '(lambda (n)       ((lambda (fact)          ((fact fact) n))        (lambda (fact)          (lambda (n)            (if (zero? n)                1                (* n ((fact fact) (sub1 n))))))))))  ;; print out CPSed function (pretty-print fact-cps) ;; => ;; '(lambda (n k) ;;    ((lambda (fact k) (fact fact (lambda (v0) (v0 n k)))) ;;     (lambda (fact k) ;;       (k ;;        (lambda (n k) ;;          (if (zero? n) ;;            (k 1) ;;            (fact ;;             fact ;;             (lambda (v1) (v1 (sub1 n) (lambda (v2) (k (* n v2)))))))))) ;;     k))   ((eval fact-cps) 5 (lambda (v) v)) ;; => 120  比较冷僻的区域 ... 基本上找不到人问

user: user-0
content: 引用 1 楼 user-10 的回复:  ;; A simple CPS transformer which does proper tail-call and does not ;; duplicate contexts for if-expressions.  ;; author: Yin Wang (yw21@cs.indiana.edu)   (load "pmatch.scm")   (define cps   (lambda (exp)     (letrec         ([trivial? (lambda (x) (memq x '(zero? add1 sub1)))]          [id (lambda (v) v)]          [ctx0 (lambda (v) `(k ,v))]      ; tail context          [fv (let ([n -1])                (lambda ()                  (set! n (+ 1 n))                  (string->symbol (string-append "v" (number->string n)))))]          [cps1           (lambda (exp ctx)             (pmatch exp               [,x (guard (not (pair? x))) (ctx x)]               [(if ,test ,conseq ,alt)                (cps1 test                      (lambda (t)                        (cond                         [(memq ctx (list ctx0 id))                          `(if ,t ,(cps1 conseq ctx) ,(cps1 alt ctx))]                         [else                          (let ([u (fv)])                            `(let ([k (lambda (,u) ,(ctx u))])                               (if ,t ,(cps1 conseq ctx0) ,(cps1 alt ctx0))))])))]               [(lambda (,x) ,body)                (ctx `(lambda (,x k) ,(cps1 body ctx0)))]               [(,op ,a ,b)                (cps1 a (lambda (v1)                          (cps1 b (lambda (v2)                                    (ctx `(,op ,v1 ,v2))))))]               [(,rator ,rand)                (cps1 rator                      (lambda (r)                        (cps1 rand                              (lambda (d)                                (cond                                 [(trivial? r) (ctx `(,r ,d))]                                 [(eq? ctx ctx0) `(,r ,d k)]  ; tail call                                 [else                                  (let ([u (fv)])                                    `(,r ,d (lambda (,u) ,(ctx u))))])))))]))])       (cps1 exp id))))     ;;; tests  ;; var (cps 'x) (cps '(lambda (x) x)) (cps '(lambda (x) (x 1)))   ;; no lambda (will generate identity functions to return to the toplevel) (cps '(if (f x) a b)) (cps '(if x (f a) b))   ;; if stand-alone (tail) (cps '(lambda (x) (if (f x) a b)))   ;; if inside if-test (non-tail) (cps '(lambda (x) (if (if x (f a) b) c d)))   ;; both branches are trivial, should do some more optimizations (cps '(lambda (x) (if (if x (zero? a) b) c d)))   ;; if inside if-branch (tail) (cps '(lambda (x) (if t (if x (f a) b) c)))   ;; if inside if-branch, but again inside another if-test (non-tail) (cps '(lambda (x) (if (if t (if x (f a) b) c) e w)))   ;; if as operand (non-tail) (cps '(lambda (x) (h (if x (f a) b))))   ;; if as operator (non-tail) (cps '(lambda (x) ((if x (f g) h) c)))   ;; why we need more than two names (cps '(((f a) (g b)) ((f c) (g d))))    ;; factorial (define fact-cps   (cps    '(lambda (n)       ((lambda (fact)          ((fact fact) n))        (lambda (fact)          (lambda (n)            (if (zero? n)                1                (* n ((fact fact) (sub1 n))))))))))  ;; print out CPSed function (pretty-print fact-cps) ;; => ;; '(lambda (n k) ;;    ((lambda (fact k) (fact fact (lambda (v0) (v0 n k)))) ;;     (lambda (fact k) ;;       (k ;;        (lambda (n k) ;;          (if (zero? n) ;;            (k 1) ;;            (fact ;;             fact ;;             (lambda (v1) (v1 (sub1 n) (lambda (v2) (k (* n v2)))))))))) ;;     k))   ((eval fact-cps) 5 (lambda (v) v)) ;; => 120  我也没看懂 = =

user: user-2
content: 难道没人觉得发错版了么？

user: user-3
content: 一周，我遇到代码比较少

user: user-4
content: 三个月

user: user-5
content: 想了6年，为什么当时选择当程序猿

user: user-6
content: 一口吃不成个胖子

user: user-7
content: 那说明基础的程度还未够看源码的程度。

user: user-8
content: 看这代码，头晕了。。。。

user: user-9
content: 想了十几天，感觉没必要，如果想了好几天想不出来，不如从网上找找别人的经验之谈(个人意见)

user: user-10
content: ;; A simple CPS transformer which does proper tail-call and does not ;; duplicate contexts for if-expressions.  ;; author: Yin Wang (yw21@cs.indiana.edu)   (load "pmatch.scm")   (define cps   (lambda (exp)     (letrec         ([trivial? (lambda (x) (memq x '(zero? add1 sub1)))]          [id (lambda (v) v)]          [ctx0 (lambda (v) `(k ,v))]      ; tail context          [fv (let ([n -1])                (lambda ()                  (set! n (+ 1 n))                  (string->symbol (string-append "v" (number->string n)))))]          [cps1           (lambda (exp ctx)             (pmatch exp               [,x (guard (not (pair? x))) (ctx x)]               [(if ,test ,conseq ,alt)                (cps1 test                      (lambda (t)                        (cond                         [(memq ctx (list ctx0 id))                          `(if ,t ,(cps1 conseq ctx) ,(cps1 alt ctx))]                         [else                          (let ([u (fv)])                            `(let ([k (lambda (,u) ,(ctx u))])                               (if ,t ,(cps1 conseq ctx0) ,(cps1 alt ctx0))))])))]               [(lambda (,x) ,body)                (ctx `(lambda (,x k) ,(cps1 body ctx0)))]               [(,op ,a ,b)                (cps1 a (lambda (v1)                          (cps1 b (lambda (v2)                                    (ctx `(,op ,v1 ,v2))))))]               [(,rator ,rand)                (cps1 rator                      (lambda (r)                        (cps1 rand                              (lambda (d)                                (cond                                 [(trivial? r) (ctx `(,r ,d))]                                 [(eq? ctx ctx0) `(,r ,d k)]  ; tail call                                 [else                                  (let ([u (fv)])                                    `(,r ,d (lambda (,u) ,(ctx u))))])))))]))])       (cps1 exp id))))     ;;; tests  ;; var (cps 'x) (cps '(lambda (x) x)) (cps '(lambda (x) (x 1)))   ;; no lambda (will generate identity functions to return to the toplevel) (cps '(if (f x) a b)) (cps '(if x (f a) b))   ;; if stand-alone (tail) (cps '(lambda (x) (if (f x) a b)))   ;; if inside if-test (non-tail) (cps '(lambda (x) (if (if x (f a) b) c d)))   ;; both branches are trivial, should do some more optimizations (cps '(lambda (x) (if (if x (zero? a) b) c d)))   ;; if inside if-branch (tail) (cps '(lambda (x) (if t (if x (f a) b) c)))   ;; if inside if-branch, but again inside another if-test (non-tail) (cps '(lambda (x) (if (if t (if x (f a) b) c) e w)))   ;; if as operand (non-tail) (cps '(lambda (x) (h (if x (f a) b))))   ;; if as operator (non-tail) (cps '(lambda (x) ((if x (f g) h) c)))   ;; why we need more than two names (cps '(((f a) (g b)) ((f c) (g d))))    ;; factorial (define fact-cps   (cps    '(lambda (n)       ((lambda (fact)          ((fact fact) n))        (lambda (fact)          (lambda (n)            (if (zero? n)                1                (* n ((fact fact) (sub1 n))))))))))  ;; print out CPSed function (pretty-print fact-cps) ;; => ;; '(lambda (n k) ;;    ((lambda (fact k) (fact fact (lambda (v0) (v0 n k)))) ;;     (lambda (fact k) ;;       (k ;;        (lambda (n k) ;;          (if (zero? n) ;;            (k 1) ;;            (fact ;;             fact ;;             (lambda (v1) (v1 (sub1 n) (lambda (v2) (k (* n v2)))))))))) ;;     k))   ((eval fact-cps) 5 (lambda (v) v)) ;; => 120