[plt-scheme] redefinition in module

From: Jos Koot (jos.koot at telefonica.net)
Date: Fri Feb 29 15:00:13 EST 2008

I noticed that for does not include a clause like
(var from last step), where from last and step are real numbers and the 
number of iterations is determined by (inexact->exact (floor (/ (past from) 
step))))
(as opposed to accumulated addition of the step to the start)

I can also imagine iteration over the entries in a hash-table.

I also noticed that a fender clause has peculiar influence on the nesting of 
iterations.

My loop is NOT consistent with the nesting of for, because it nests every 
clause that is not a fender.
But addition of a (var from past step) clause can be made in backward 
compatible fashion.

(module loop scheme ; not yet converted to #lang scheme
 (provide loop)
 (require (only-in (lib "etc.ss") begin-with-definitions))

 (define-syntax loop
  (syntax-rules ()
   ((loop () body-expr ...) (void (begin-with-definitions body-expr ...)))
   ((loop ((index from to step)) body-expr ...)
    (let ((f from) (t to) (s step))
     (let ((n (inexact->exact (ceiling (/ (- t f) s)))))
      (let loop ((i 0))
       (when (< i n)
        (let ((index (+ f (* i s))))
         (inhibit-assignment (index) body-expr ... (loop (add1 i)))))))))
   ((loop ((index from to)) body-expr ...)
    (loop ((index from to 1)) body-expr ...))
   ((loop ((index ctl)) body-expr ...)
    (loop-aux ctl (lambda (index) (void (begin-with-definitions body-expr 
...)))))
   ((loop (loop-control1 loop-control ...) body-expr ...)
    (loop (loop-control1) (loop (loop-control ...) body-expr ...)))))

 (define-syntax (inhibit-assignment stx)
  (syntax-case stx ()
   ((inhibit-assignment (var ...) . rest)
    #'(let-syntax
     ((var
       (make-set!-transformer
        (lambda (stx)
         (syntax-case stx (set!)
          ((set! id v) (raise-syntax-error #f "assignment to protected loop 
control index is prohibited" stx #'id))
          ((id x (... ...)) (raise-syntax-error #f "loop control index 
cannot be used in operator position" stx #'id))
          (id #'var))))) ...) . rest))))

 (define (loop-aux control proc)
  (cond
   ((number? control) (loop ((i 0 control 1)) (proc i)))
   ((vector? control) (loop ((i 0 vector-length control)) (proc (vector-ref 
control i))))
   ((list? control) (for-each proc control))
   ((hash-table? control) (hash-table-for-each control (lambda (x y) (proc 
(list x y)))))
   (else (error 'loop "number vector list or hash-table, given: ~s" 
control)))))

Jos

----- Original Message ----- 
From: "Noel Welsh" <noelwelsh at gmail.com>
To: "Jos Koot" <jos.koot at telefonica.net>
Cc: <plt-scheme at list.cs.brown.edu>
Sent: Friday, February 29, 2008 8:36 PM
Subject: Re: [plt-scheme] redefinition in module


> On Fri, Feb 29, 2008 at 7:04 PM, Jos Koot <jos.koot at telefonica.net> wrote:
>> For example, I had my own nice and more general version of syntax 'for'. 
>> I
>> no longer can import my own 'for' without renaming.
>
> If you have suggestions to improve the comprehension macros I, for
> one, would like to hear them.  Designing such a system is very tricky,
> and perhaps the existing system can be improved further.
>
> N. 



Posted on the users mailing list.