Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

dolist and setf in scheme?

368 views
Skip to first unread message

John LeDuc

unread,
Aug 8, 2016, 8:12:04 AM8/8/16
to
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.

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)?

Thanks in advance!

Takashi Kato

unread,
Aug 8, 2016, 10:09:25 AM8/8/16
to
On Monday, August 8, 2016 at 2:12:04 PM UTC+2, John LeDuc wrote:
> 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)?
For DOLIST, can't you simply use FOR-EACH? If you need some syntax sugar,
then you can write it like this:

(define-syntax dolist
(syntax-rules ()
((_ (e lis) exprs ...)
(for-each (lambda (e) exprs ...) lis))))

For SETF, in short, no as far as I know. Scheme's SET! returns unspecified
value, so if you use the returned value, it won't be portable at all. If
you just need to get the set value, then you can write a macro like this
(not sure if you need old value or new value, so assumed new value):

(define-syntax setf
(syntax-rules ()
((_ var val)
(let ((v val))
(set! var v)
v))))

NOTE: this obviously doesn't work like this: (setf (car p) 'a)

_/_/
Takashi Kato

Pascal J. Bourguignon

unread,
Aug 8, 2016, 10:44:25 AM8/8/16
to
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

Robert L.

unread,
Nov 25, 2016, 2:20:44 AM11/25/16
to
On 8/8/2016, Takashi Kato wrote:

> (define-syntax setf
> (syntax-rules ()
> ((_ var val)
> (let ((v val))
> (set! var v)
> v))))
>
> NOTE: this obviously doesn't work like this: (setf (car p) 'a)

Gauche Scheme:

gosh> (define lst '(2 3 4))
lst
gosh> (set! (car lst) 88)
#<undef>
gosh> lst
(88 3 4)
0 new messages