May 23th Wednesday (五月 二十三日 水曜日)

来源:互联网 发布:java语言和c语言的区别 编辑:程序博客网 时间:2024/05/01 01:23

The "(map proc list1 ...)" applies a procedure element-wise to the elements of the lists and
returns a list of the results, in order.  The dynamic order in which the procedure is applied
to the elements of the lists is unspecified.

  But the "(for-each proc list1 ...)" is guaranteed to call "proc" on the elements of the "lists"
in order from the first element(s) to the last, and the value returned by "for-each" is unspecified.

  The "(apply proc arg1 ...)", calls "proc" with the elements of the arguments list as the actual
arguments.

(define-syntax with-syntax
  (lambda (x)
    (syntax-case x ()
      ((_ ((p e0) e1 e2 ...))
       (syntax (syntax-case (list e0 ...) ()
                 ((p ...) (begin e1 e2 ...))))))))

(define-syntax loop
  (lambda (x)
    (syntax-case x ()
      ((k e ...)
       (with-syntax ((break (datum->syntax-object (syntax k) 'break)))
         (syntax (call/cc (lambda (break)
                            (let f () e ... (f))))))))))

  Let's come back to the "with-syntax" again.  Firstly, there is a discrepancy between the environment
of defining a macro "loop" and the enivronment of invoking the macro "loop".

(syntax (call/cc (lambda (break)
                          (let f () e ... (f)))))

  The above codes is a classic loop and break segment in scheme.  For escaping from a loop, there is
a current continutaion.  How to put a invoked current continuation into the "with-syntax" macro.

(break (datum->syntax-object (syntax k) 'break))

  This is a key code.  It binds a invoking environment to a 'break label.  Now the "with-syntax" can
matchs its pattern with it.

(_ ((p e0) e1 e2 ...))

  The "_" is "with-syntax" macro name.  The (p e0) matched (break (datum->syntax-object (syntax k) 'break)).
The "p" is 'break now.  So, in actual fact, (p ...) is (break ...).  First "p" matched from "with-syntax"'s
pattern matched; second "p" must matched pattern from (list e0 ...).  In (list e0 ...) there is a
invoking current continuation.  Okey, the rest codes can escape from a loop by using the current continuation. 

原创粉丝点击