John LeDuc <hier@daar> writes:
> I'm porting a CL application to scheme (preferably R5RS).
>
> I found this definition for dolist:
> (define-macro dolist (lambda (args . s)
> (let ((lstvar (gensym 'dolist-ptr)))
> (list 'do
> (list (list lstvar (cadr args) (list 'cdr lstvar))
> (list (car args) #f) )
> (list (list 'null? lstvar)
> (if (null? (cddr args)) #f (caddr args)))
> (cons 'begin (cons (list 'set! (car args) (list 'car lstvar))
> s)) ) )))
> but it doesn't work and to me it looks overly complicated.
I don't see why it shouldn't work, it looks perfectly good to me.
You might discuss the choice of using #f instead of 'nil as specified by
CL, but a choice has to be made in Scheme anyways, so…
[pjb@kuiper :0.0 ~]$ bigloo
------------------------------------------------------------------------------
Bigloo (4.1a) ,--^,
`a practical Scheme compiler' _ ___/ /|/
Tue Feb 18 19:35:13 CET 2014 ,;'( )__, ) '
Inria -- Sophia Antipolis ;; // L__.
email:
big...@lists-sop.inria.fr ' \ / '
url:
http://www-sop.inria.fr/indes/fp/Bigloo ^ ^
------------------------------------------------------------------------------
1:=> (define-macro dolist (lambda (args . s)
(let ((lstvar (gensym 'dolist-ptr)))
(list 'do
(list (list lstvar (cadr args) (list 'cdr lstvar))
(list (car args) #f) )
(list (list 'null? lstvar)
(if (null? (cddr args)) #f (caddr args)))
(cons 'begin (cons (list 'set! (car args) (list 'car lstvar))
s)) ) )))
#unspecified
1:=> (dolist (v '(1 2 3)) (display v) (newline))
1
2
3
#f
1:=> (dolist (v '(1 2 3)) (display v) (newline) (set! v 0))
1
2
3
#f
1:=> (dolist (v '(1 2 3) 42) (display v) (newline) (set! v 0))
1
2
3
42
1:=>
> Also, SETF in CL apparently returns the value as a result and set! in
> scheme returns nothing (#<unspecified>). Any general solutions
> (preferably without macro's or syntax-rules)?
Well if you're asking how to fight the dragon with both hands tied in
your back and blindfolded, yes, there's always something you can do.
Even after your legs are cut off, you can still shout "Ni!".
No, just joking. It's actually easier to do it in scheme: just go
metalinguistical. Treat all CL macros as special operators in your
implementation of CL written in Scheme. (If that sounds easier than
writing scheme macros, then have fun).
--
__Pascal Bourguignon__
http://www.informatimago.com/
“The factory of the future will have only two employees, a man and a
dog. The man will be there to feed the dog. The dog will be there to
keep the man from touching the equipment.” -- Carl Bass CEO Autodesk