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

re: simple swap func

54 views
Skip to first unread message

WJ

unread,
May 6, 2012, 9:12:42 PM5/6/12
to
Lyman S. Taylor wrote:

> In article <5j65el$...@helix.cs.uoregon.edu>,
> Juan Jose Flores Romero <j...@cs.uoregon.edu> wrote:
>
> >USER(8): (defmacro swap (x y)
> > `(let ((temp ,x))
> > (setq ,x ,y)
> > (setq ,y temp)))
> >SWAP
>
> This isn't hygienic. The following:
>
> (swap temp b )
> or
> (swap a temp)
>
> won't work... You'll need a unique temporary variable to
> solve this problem.
>
> (defmacro swap ( x y )
> (let (( temp (gensym)))
> `(let ((,temp ,x ))
> (setq ,x ,y)
> (setq ,y ,temp))))

Racket:

(define-syntax-rule (swap x y)
(let ((temp x))
(set! x y)
(set! y temp)))

(define temp 2)
(define a 22)
(swap temp a)
(list temp a)
=> '(22 2)

loopcm

unread,
May 13, 2012, 6:13:47 AM5/13/12
to
> => '(22 2)- 隐藏被引用文字 -
>
> - 显示引用的文字 -

it looks like c&&c++ other way to wirte swap in lisp?

Pascal Costanza

unread,
May 15, 2012, 1:29:13 PM5/15/12
to
(defmacro swap (x y)
`(rotatef ,x ,y))


Pascal

--
My website: http://p-cos.net
Common Lisp Document Repository: http://cdr.eurolisp.org
Closer to MOP & ContextL: http://common-lisp.net/project/closer/
The views expressed are my own, and not those of my employer.
0 new messages