Iteration macro in scheme? 
Author Message
 Iteration macro in scheme?

I'm using MacGambit for a gigantic project that should really be
in  lisp and I hacked up -- perversely, no doubt -- the old unisys
for macro, which I've been using but it's so obscurely written
I find it impossible to extend or debug. By any odd chance, does someone
have a common lisp like loop macro in scheme around? It need not be
as complicated as loop, which I have no desire to hack. I need it for
the
most common sorts of iterations, such as:

for x on y by cddr do ....
for x in y by increment do ..
for x in y by increment as a on b by cdddr do ...
for x from a to b do ...

etc.

I know I'll probably wind up hacking this myself but it would be great
if someone's already done this as I can't really spare the
concentration.

In answer to the question ":why not lisp," I don't have the resources
and anyway I got to like scheme. I've got most of common lisp up in
macros anyway.

If anyone is interested in my hacked-up unisys for macro I'll pass it
on, but it really is an incomprehensible mess at this point. I find it
amazing that it works at all.

to reply remove one of the 'eliot's from my adress.



Mon, 24 Jun 2002 03:00:00 GMT  
 Iteration macro in scheme?

Quote:

>I'm using MacGambit for a gigantic project that should really be
>in  lisp and I hacked up -- perversely, no doubt -- the old unisys
>for macro, which I've been using but it's so obscurely written
>I find it impossible to extend or debug. By any odd chance, does someone
>have a common lisp like loop macro in scheme around? It need not be
>as complicated as loop, which I have no desire to hack.

Chris Riesbeck has a Lisp-based, extensible loop system at:

http://www.cs.nwu.edu/academics/courses/c25/programs/xlisp/loop.lsp



Tue, 25 Jun 2002 03:00:00 GMT  
 Iteration macro in scheme?

Quote:

>>I'm using MacGambit for a gigantic project that should really be
>>in  lisp and I hacked up -- perversely, no doubt -- the old unisys
>>for macro, which I've been using but it's so obscurely written
>>I find it impossible to extend or debug. By any odd chance, does someone
>>have a common lisp like loop macro in scheme around? It need not be
>>as complicated as loop, which I have no desire to hack.

>Chris Riesbeck has a Lisp-based, extensible loop system at:

>http://www.cs.nwu.edu/academics/courses/c25/programs/xlisp/loop.lsp

... Which is pretty directed to use with Common Lisp (or the subset
XLisp speaks).

Can you suggest something written for Scheme?
--
"If you want to travel around the world and be invited to speak at a
lot of different places, just write a Unix operating system."
-- Linus Torvalds



Wed, 26 Jun 2002 03:00:00 GMT  
 Iteration macro in scheme?

Quote:

>>I'm using MacGambit for a gigantic project that should really be
>>in  lisp and I hacked up -- perversely, no doubt -- the old unisys
>>for macro, which I've been using but it's so obscurely written
>>I find it impossible to extend or debug. By any odd chance, does someone
>>have a common lisp like loop macro in scheme around? It need not be
>>as complicated as loop, which I have no desire to hack.

>Chris Riesbeck has a Lisp-based, extensible loop system at:

>http://www.cs.nwu.edu/academics/courses/c25/programs/xlisp/loop.lsp

... Which is pretty directed to use with Common Lisp (or the subset
XLisp speaks).

Can you suggest something written for Scheme?
--
"If you want to travel around the world and be invited to speak at a
lot of different places, just write a Unix operating system."
-- Linus Torvalds



Wed, 26 Jun 2002 03:00:00 GMT  
 Iteration macro in scheme?

Quote:

> I need it for
> the
> most common sorts of iterations, such as:

> for x on y by cddr do ....
> for x in y by increment do ..
> for x in y by increment as a on b by cdddr do ...
> for x from a to b do ...

Why not use normal Scheme instead of a macro ?

(define iota
  (lambda (start finish step)
  (if (> start finish)                     ; doesn't work for step <= 0
      '()
      (cons start (iota (+ start step) finish step)))
  ))

(define iota1
  (lambda (start finish) (iota start finish 1)))

In MacGambit-C,

Quote:
> (for-each pp (iota 33 45 0.5))

33
33.5
34.
34.5
35.
35.5
36.
36.5
37.
37.5
38.
38.5
39.
39.5
40.
40.5
41.
41.5
42.
42.5
43.
43.5
44.
44.5
45.
Quote:
> (define add1

    (lambda (x) (+ x 1)))
Quote:
> (map add1 (iota 33 45 0.5))

(34 34.5 35. 35.5 36. 36.5 37. 37.5 38. 38.5 39. 39.5 40. 40.5 41. 41.5 42. 42.5 43. 43.5 44. 44.5 45. 45.5 46.)

If your loop spanned a great deal of items (for x = 1 to 100000) then
it might be a bad idea to create the list of all those numbers.  In
that case, you simply pass the procedure into an iota-like function,
have the function run the procedure for the current value and then
call itself for the next iteration.  You'd have to define one for the
'for-each' usage and another for the 'map' usage.

Simon.
--
http://www.hearsay.demon.co.uk | John Peel:
No junk email please.          |  [My daughter] has modelled herself on you.
                               | Courtney Love:
                               |  Oh, I'm so sorry.



Wed, 26 Jun 2002 03:00:00 GMT  
 Iteration macro in scheme?

Quote:



> > I need it for
> > the
> > most common sorts of iterations, such as:

> > for x on y by cddr do ....
> > for x in y by increment do ..
> > for x in y by increment as a on b by cdddr do ...
> > for x from a to b do ...

> Why not use normal Scheme instead of a macro ?

> (define iota
>   (lambda (start finish step)
>   (if (> start finish)                     ; doesn't work for step <= 0
>       '()
>       (cons start (iota (+ start step) finish step)))

R5 states do

this seems to be R5rs conformant Scheme:
(define (fact-with-do n)
  (let ((prod 1))
        (do ((i 1 (+ 1 i)))
                ((> i n) prod)
          (set! prod (* i prod)))))

I cite from my manual:
Syntax:  (do ((<variable1> <init1> <step1>)
                        ...)
                       (<test> <expression> ...)
                     <command> ...)

Do is an iteration construct. It specifies a set of variables to be
bound, how they are to be initialized at the start, and how they are to
be updated on each iteration. When a termination condition is met, the
loop exits after evaluating the <expression>s.

Do expressions are evaluated as follows: The <init> expressions are
evaluated (in some unspecified order), the <variable>s are bound to
fresh locations, the results of the <init> expressions are stored in the
bindings of the <variable>s, and then the iteration phase begins.

Each iteration begins by evaluating <test>; if the result is false (see
section 6.3.1), then the <command> expressions are evaluated in order
for effect, the <step> expressions are evaluated in some unspecified
order, the <variable>s are bound to fresh locations, the results of the
<step>s are stored in the bindings of the <variable>s, and the next
iteration begins.

If <test> evaluates to a true value, then the <expression>s are
evaluated from left to right and the value(s) of the last <expression>
is(are) returned. If no <expression>s are present, then the value of the
do expression is unspecified.

The region of the binding of a <variable> consists of the entire do
expression except for the <init>s. It is an error for a <variable> to
appear more than once in the list of do variables.

A <step> may be omitted, in which case the effect is the same as if
(<variable> <init> <variable>) had been written instead of (<variable>
<init>).

  (do ((vec (make-vector 5))
       (i 0 (+ i 1)))
      ((= i 5) vec)
    (vector-set! vec i i))          =>  #(0 1 2 3 4)

  (let ((x '(1 3 5 7 9)))
    (do ((x x (cdr x))
         (sum 0 (+ sum (car x))))
        ((null? x) sum)))             =>  25

Isn't that what you're looking for?

Regards
Friedrich



Thu, 27 Jun 2002 03:00:00 GMT  
 Iteration macro in scheme?


;
;> I need it for
;> the
;> most common sorts of iterations, such as:
;>
;> for x on y by cddr do ....
;> for x in y by increment do ..
;> for x in y by increment as a on b by cdddr do ...
;> for x from a to b do ...
;
;Why not use normal Scheme instead of a macro ?

What's wrong with macros?  It's probable that I'm overlooking
many ideas in scheme that don't work in lisp, eg (as far as I remember)
coroutines using call/cc but I don't see the point of avoiding macros.

-- eliot



Thu, 27 Jun 2002 03:00:00 GMT  
 Iteration macro in scheme?

;>


;
;R5 states do
;
;this seems to be R5rs conformant Scheme:
;(define (fact-with-do n)
;  (let ((prod 1))
;        (do ((i 1 (+ 1 i)))
;                ((> i n) prod)
;          (set! prod (* i prod)))))
;
;
;
;Isn't that what you're looking for?
;
;Regards
;Friedrich

I personally don't like do, though I use dolist and dotimes, implemented
as macros, a lot. If I'm really just going to be looking at cars in do
I need

(do ((v l (cdr v)))
    ((null? l) #t)
   (let ((iitem (car v)))
      ....)

As opposed to

(dolist (item l #t)
    ....)

I find named lets much more useful in this regard than do.



Thu, 27 Jun 2002 03:00:00 GMT  
 Iteration macro in scheme?


Quote:


>>>I'm using MacGambit for a gigantic project that should really be
>>>in  lisp and I hacked up -- perversely, no doubt -- the old unisys
>>>for macro, which I've been using but it's so obscurely written
>>>I find it impossible to extend or debug. By any odd chance, does someone
>>>have a common lisp like loop macro in scheme around? It need not be
>>>as complicated as loop, which I have no desire to hack.

>>Chris Riesbeck has a Lisp-based, extensible loop system at:

>>http://www.cs.nwu.edu/academics/courses/c25/programs/xlisp/loop.lsp

>... Which is pretty directed to use with Common Lisp (or the subset
>XLisp speaks).

>Can you suggest something written for Scheme?
>--

If I had, I would have directed you there...

Riebeck's code is relatively short and transparent, and shouldn't be that
difficult to port to Scheme.



Fri, 28 Jun 2002 03:00:00 GMT  
 Iteration macro in scheme?
On Sat, 08 Jan 2000 22:57:18 +0000, Simon Slavin

Quote:



>> I need it for
>> the
>> most common sorts of iterations, such as:

>> for x on y by cddr do ....
>> for x in y by increment do ..
>> for x in y by increment as a on b by cdddr do ...
>> for x from a to b do ...

>Why not use normal Scheme instead of a macro ?

>(define iota
>  (lambda (start finish step)
>  (if (> start finish)                     ; doesn't work for step <= 0
>      '()
>      (cons start (iota (+ start step) finish step)))
>  ))

Pretty slick.

The following adaption copes with negative steps without any
diminishment of performance (e.g. - it only evaluates the sign of the
step *once*), and returns 'error if given a step of 0.

(define (iota start finish step)
  (if (= step 0)
      'error
      (letrec
          ((comparator
            (if (> 0 step)
                < >))
           (internal-iota
            (lambda (start finish)
              (if (comparator start finish)
                  '()
                  (cons start (internal-iota (+ start step) finish))))))
        (internal-iota start finish))))
--
The software isn't finished until the last user is dead.



Sat, 29 Jun 2002 03:00:00 GMT  
 Iteration macro in scheme?
On Sun, 09 Jan 2000 20:35:50 -0500, E. Handelman

Quote:

>I find named lets much more useful in this regard than do.

I like named let a whole lot more than do; is it me, or is it more
straightforward to follow it?

What troubles me a little bit about do is that it initiates a whole
lot of structure with barely any "punctuation."

Named let has:
(let name
   (definitions-to-apply)
  (body
    (name new values-for-defintions)))

and is pretty much just an extension of ordinary let, whilst do has a
hairy set of groups of parentheses without indicators to look like
portions of other Scheme control structures.
--
"In man-machine symbiosis, it is man who must adjust: The machines
can't." -- Alan Perlis



Sun, 30 Jun 2002 03:00:00 GMT  
 
 [ 11 post ] 

 Relevant Pages 

1. Scheme macro source: rewriter-widget, a widely applicable macro interface

2. Looking for Common Lisp iteration macros

3. A CL iteration macro, "while".

4. A CL iteration macro, FOR

5. FOR Iteration Scheme

6. A macro involving two sub-macros - where the 2nd macro needs results from the first

7. Metaprogramming: C++ templates vs Scheme macros?

8. Haskell functions as CL/Scheme macros

9. Macros in scheme-style syntax

10. Macros in Common Lisp, Scheme, and other languages

11. Scheme R4RS Macros

12. The woes of Scheme macros - HELP!

 

 
Powered by phpBB® Forum Software