Search code examples
loopsvariable-assignmentsequentialchicken-scheme

A sequential Do for scheme (do*)


In Common Lisp, there's a sequential form of (do *vars* *test* *body*); just like let's sequential parallel let*, it works through *vars* one at a time, so that you can refer back to previously defined variables like so:

(do* ((a '(1 2 3) (cdr a))
      (b (mapcar (lambda (x) (1+ x)) a) (cdr b)))
     ((= 1 1) (list 'a a 'b b)))
; (a (1 2 3) b (2 3 4))

In Scheme, AFAIK, there is no correlating function or macro. There's do, but no do*. I've been trying to write an implementation of do* for Chicken Scheme, but so far I'm struggling to make any headway... I think, I can't really tell. I'm familiar with Chicken Scheme, but I've always found scheme macros confusing.

Here's what I've got so far:

(##sys#extend-macro-environment
 'do*
 '()
 (##sys#er-transformer
  (lambda (form r c)
(##sys#check-syntax 'do* form '(_ #((symbol _ . #(_)) 0) . #(_ 1)))
(let* ((bindings (cadr form))
       (test (caddr form))
       (body (cdddr form))
       (do*var (r 'doloop)))
  `(let*
       ,do*var
     ,(##sys#map (lambda (b) (list (car b) (car (cdr b)))) bindings)
     (##core#if ,(car test)
        ,(let ((tbody (cdr test)))
           (if (eq? tbody '())
               '(##core#undefined)
               `(##core#begin ,@tbody) ) )
        (##core#begin
         ,(if (eq? body '())
              '(##core#undefined)
              `(##core#let () ,@body) )
         (##core#app
          ,do*var ,@(##sys#map (lambda (b)
                     (if (eq? (cdr (cdr b)) '())
                         (car b)
                         (car (cdr (cdr b))) ) )
                       bindings) ) ) ) ) ) ) ) )

But I keep on getting errors like doloop not being a list--presently, I'm getting

Error: during expansion of (do* ...) - in `do*' - symbol expected: (do* ((a (quote (1 2 3)) (cdr a)) (b (map (lambda (x) (+ 1 x)) a) (cdr b)) ((= 1 1) (list (quote a) a (quote b) b))))

    Call history:

    <syntax>          (do* ((a (quote (1 2 3)) (cdr a)) (b (map           (lambda (x) (+ 1 x)) a) (cdr b)) ((= 1 1) (list (quote a) a...
    <eval>    (##sys#check-syntax (quote do*) form (quote (_ #((symbol _ . #(_)) 0) . #(_ 1))))     <--

Here's a rough do* in Common Lisp taken from HyerSpec:

  (block nil        
   (let ((var1 init1)
     (var2 init2)
     ...
     (varn initn))
     declarations
     (loop (when end-test (return (progn . result)))
       (tagbody . tagbody)
       (psetq var1 step1
          var2 step2
          ...
          varn stepn))))  

do* is similar, except that let* and setq replace the let and psetq, respectively.

And here's what do* expands to in CL:

(expand-form '(do* ((a '(1 2 3) (cdr a))
        (b (mapcar (lambda (x) (1+ x)) a) (cdr b)))
           ((= 1 1) (list 'a a 'b b))))
(block nil
   (let* ((a '(1 2 3)) (b (mapcar #'(lambda (x) (declare (system::source ((x) (1+ x)))) (1+ x)) a)))
     (tagbody #:loop-5382 (if (= 1 1) (go #:end-5383)) (setq a (cdr a) b (cdr b)) (go #:loop-5382) #:end-5383 (return-from nil (list 'a a 'b b))))) ;
;t

Solution

  • Please, do not use ##-prefixed identifiers. Those are unsupported and not part of the official API!

    Second, instead of trying to mess about with low-level macros, just use syntax-rules, which is much simpler, less error-prone and standardized. It should be used whenever you don't need to break hygiene.

    Here is an example of do* as it would work if I understand you correctly:

    (define-syntax do*
      (syntax-rules ()
        ((_ ((?var0 ?init0 ?inc0) ...)
             (?test ?result)
             ?body ...)
         (let* ((?var0 ?init0) ...)
           (let lp ()
             (cond (?test ?result)
                   (else ?body ...
                         (set! ?var0 ?inc0) ...
                         (lp))))))))
    

    Expressed as an implicitly renaming macro:

    (define-syntax do*
      (ir-macro-transformer
       (lambda (e i c)
         (let* ((vars (cadr e))
                (test&result (caddr e))
                (test (car test&result))
                (result (cadr test&result))
                (body-exprs (cdddr e)))
           `(let* (,@(map (lambda (v) (list (car v) (cadr v))) vars))
              (let lp ()
                (cond (,test ,result)
                      (else ,@body-exprs
                            ,@(map (lambda (v) `(set! ,(car v) ,(caddr v))) vars)
                            (lp)))))))))
    

    I hope you agree it is much more verbose that way.