<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=Content-Type content="text/html; charset=iso-8859-1">
<META content="MSHTML 6.00.6000.16608" name=GENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY>
<DIV><FONT face="Courier New" size=2>Browsing through the documentation of
sequences, I am overwhelmed by the flexibility.</FONT></DIV>
<DIV><FONT face="Courier New" size=2>I could not refrain from trying out
some things, of course. Very nice indeed.</FONT></DIV>
<DIV><FONT face="Courier New" size=2>Jos</FONT></DIV>
<DIV><FONT face="Courier New" size=2></FONT> </DIV>
<DIV><FONT face="Courier New" size=2>(define-sequence-syntax
ftn-range<BR> (lambda (stx) (raise-syntax-error 'ftn-range "can only be
used in for clauses" stx))<BR> (lambda (ignore stx)<BR> (let loop
((stx stx))<BR> (syntax-case stx ()<BR> (((id)
(ftn-range to))<BR> (loop #'((id) (ftn-range 0 to
#f))))<BR> (((id) (ftn-range from
to))<BR> (loop #'((id) (ftn-range from to
#f))))<BR> (((id) (ftn-range from to step))<BR>
#'((id)<BR>
(:do-in<BR> (((n f s) ;
bindings<BR> (let-values (((f t
s) (values from to
step)))<BR> (let ((s (or s
(if (> t f) +1
-1))))<BR> (when
(zero? s) (error 'ftn-range "zero
step"))<BR> (values
(inexact->exact (ceiling (/ (- t f) s))) f
s)))))<BR> #t ; no
outer-check<BR> ((k 0)) ; hidden loop
var<BR> (< k n)
; pos-guard<BR> (((id) (+ f (* k s))))
; users loop var<BR> #t ; no
pre-guard<BR> #t ; no
post-guard<BR> ((add1
k)))))))))</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><FONT face=Arial size=2>----- Original Message ----- </FONT>
<DIV><FONT face=Arial size=2>From: "Jos Koot" <</FONT><A
href="mailto:jos.koot@telefonica.net"><FONT face=Arial
size=2>jos.koot@telefonica.net</FONT></A><FONT face=Arial
size=2>></FONT></DIV>
<DIV><FONT face=Arial size=2>To: "Eli Barzilay" <</FONT><A
href="mailto:eli@barzilay.org"><FONT face=Arial
size=2>eli@barzilay.org</FONT></A><FONT face=Arial size=2>></FONT></DIV>
<DIV><FONT face=Arial size=2>Cc: <</FONT><A
href="mailto:plt-scheme@list.cs.brown.edu"><FONT face=Arial
size=2>plt-scheme@list.cs.brown.edu</FONT></A><FONT face=Arial
size=2>></FONT></DIV>
<DIV><FONT face=Arial size=2>Sent: Saturday, March 01, 2008 4:34 PM</FONT></DIV>
<DIV><FONT face=Arial size=2>Subject: Re: [plt-scheme] for in-range (was
redefinition in module)</FONT></DIV></DIV>
<DIV><FONT face=Arial><BR><FONT size=2></FONT></FONT></DIV><FONT face=Arial
size=2>> <BR>> ----- Original Message ----- <BR>> From: "Eli Barzilay"
<</FONT><A href="mailto:eli@barzilay.org"><FONT face=Arial
size=2>eli@barzilay.org</FONT></A><FONT face=Arial size=2>><BR>> To: "Jos
Koot" <</FONT><A href="mailto:jos.koot@telefonica.net"><FONT face=Arial
size=2>jos.koot@telefonica.net</FONT></A><FONT face=Arial size=2>><BR>>
Cc: "Noel Welsh" <</FONT><A href="mailto:noelwelsh@gmail.com"><FONT
face=Arial size=2>noelwelsh@gmail.com</FONT></A><FONT face=Arial size=2>>;
<</FONT><A href="mailto:plt-scheme@list.cs.brown.edu"><FONT face=Arial
size=2>plt-scheme@list.cs.brown.edu</FONT></A><FONT face=Arial
size=2>><BR>> Sent: Saturday, March 01, 2008 3:18 PM<BR>> Subject: Re:
[plt-scheme] for in-range (was redefinition in module)<BR>> <BR>>
<BR>>> On Mar 1, Jos Koot wrote:<BR>>>> Hi<BR>>>>
I had a look in scheme/private/for.ss. If I understand the code<BR>>>>
correctly in-range computes subsequent values of the variable by<BR>>>>
accumulated addition of the step to the start. When some decades
ago<BR>>>> do-loops with real variables were introduced in fortran,
there was<BR>>>> discussion about how to approach
it:<BR>>>><BR>>>> 1: accumulated addition of the step to the
start.<BR>>>><BR>>>> 2: compute the number of iterations in
advance as<BR>>>> n=(inexact->exact (ceiling (/ (-
finish start) step))))<BR>>>> and loop (for ((var
(in-range 0 n 1))) (let ((var (+ start (* i <BR>>>> step))))
etc))<BR>>>><BR>>>> The second method was choosen, because it
was argued that<BR>>>> accumulative addition also would accumulate
error. The code below<BR>>>> illustrates the
difference:<BR>>><BR>>> This is a *much* smaller problem in Scheme,
since for the relatively<BR>>> rare cases that you don't want to loop on
just integers, you can just<BR>>> use exact
rationals.<BR>>><BR>>> --
<BR>>> ((lambda (x)
(x x)) (lambda (x) (x x)))
Eli
Barzilay:<BR>>>
</FONT><A href="http://www.barzilay.org/"><FONT face=Arial
size=2>http://www.barzilay.org/</FONT></A><FONT face=Arial
size=2>
Maze is Life!<BR>> <BR>> A rare case may involve limits that are
inherently inexact, for example when <BR>> drawn from a
measurement-device.<BR>> Jos <BR>> <BR>>
_________________________________________________<BR>> For list-related
administrative tasks:<BR>> </FONT><A
href="http://list.cs.brown.edu/mailman/listinfo/plt-scheme"><FONT face=Arial
size=2>http://list.cs.brown.edu/mailman/listinfo/plt-scheme</FONT></A></BODY></HTML>