[plt-scheme] for in-range (was redefinition in module)

From: Jos Koot (jos.koot at telefonica.net)
Date: Sat Mar 1 11:49:52 EST 2008

Browsing through the documentation of sequences, I am overwhelmed by the flexibility.
I could not refrain from trying out some things, of course. Very nice indeed.
Jos

(define-sequence-syntax ftn-range
 (lambda (stx) (raise-syntax-error 'ftn-range "can only be used in for clauses" stx))
 (lambda (ignore stx)
  (let loop ((stx stx))
   (syntax-case stx ()
    (((id) (ftn-range to))
     (loop #'((id) (ftn-range 0 to #f))))
    (((id) (ftn-range from to))
     (loop #'((id) (ftn-range from to #f))))
    (((id) (ftn-range from to step))
   #'((id)
      (:do-in
       (((n f s) ; bindings
         (let-values (((f t s) (values from to step)))
          (let ((s (or s (if (> t f) +1 -1))))
           (when (zero? s) (error 'ftn-range "zero step"))
           (values (inexact->exact (ceiling (/ (- t f) s))) f s)))))
       #t ; no outer-check
       ((k 0)) ; hidden loop var
       (< k n) ; pos-guard
       (((id) (+ f (* k s)))) ; users loop var
       #t ; no pre-guard
       #t ; no post-guard
       ((add1 k)))))))))

----- Original Message ----- 
From: "Jos Koot" <jos.koot at telefonica.net>
To: "Eli Barzilay" <eli at barzilay.org>
Cc: <plt-scheme at list.cs.brown.edu>
Sent: Saturday, March 01, 2008 4:34 PM
Subject: Re: [plt-scheme] for in-range (was redefinition in module)


> 
> ----- Original Message ----- 
> From: "Eli Barzilay" <eli at barzilay.org>
> To: "Jos Koot" <jos.koot at telefonica.net>
> Cc: "Noel Welsh" <noelwelsh at gmail.com>; <plt-scheme at list.cs.brown.edu>
> Sent: Saturday, March 01, 2008 3:18 PM
> Subject: Re: [plt-scheme] for in-range (was redefinition in module)
> 
> 
>> On Mar  1, Jos Koot wrote:
>>> Hi
>>> I had a look in scheme/private/for.ss. If I understand the code
>>> correctly in-range computes subsequent values of the variable by
>>> accumulated addition of the step to the start. When some decades ago
>>> do-loops with real variables were introduced in fortran, there was
>>> discussion about how to approach it:
>>>
>>> 1: accumulated addition of the step to the start.
>>>
>>> 2: compute the number of iterations in advance as
>>>    n=(inexact->exact (ceiling (/ (- finish start) step))))
>>>    and loop (for ((var (in-range 0 n 1))) (let ((var (+ start (* i 
>>> step)))) etc))
>>>
>>> The second method was choosen, because it was argued that
>>> accumulative addition also would accumulate error. The code below
>>> illustrates the difference:
>>
>> This is a *much* smaller problem in Scheme, since for the relatively
>> rare cases that you don't want to loop on just integers, you can just
>> use exact rationals.
>>
>> -- 
>>          ((lambda (x) (x x)) (lambda (x) (x x)))          Eli Barzilay:
>>                  http://www.barzilay.org/                 Maze is Life!
> 
> A rare case may involve limits that are inherently inexact, for example when 
> drawn from a measurement-device.
> Jos 
> 
> _________________________________________________
>  For list-related administrative tasks:
>  http://list.cs.brown.edu/mailman/listinfo/plt-scheme
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20080301/868452d2/attachment.html>

Posted on the users mailing list.