this is my first macro to do parallel assignment:
Code: Select all
(define-macro (psetq)
(let ((_var '()) (_ex '()))
; for each expression in (args 1) ...
(for (i 0 (- (length (args 1)) 1))
; expand the i-th expression with the value
; of each variable in (args 0)
(setq _ex (expand (args 1 i) (args 0 0)))
; loop that expands the i-th expression for each variable
(for (j 1 (- (length (args 0)) 1))
(setq _ex (expand _ex (args 0 j)))
(println _ex)
)
; adds the expanded expression to a list
(push _ex _var -1)
)
(println _var)
; assigns to each variable the evaluation
; of the relative expression of the created list
(dolist (el _var)
(set (args 0 $idx) (eval el))
)
)
)
Examples:
(setq x 2 y 3)
(psetq (x y) ((+ 1 y) (+ 1 x)))
;-> (+ 1 3)
;-> (+ 1 2)
;-> ((+ 1 3) (+ 1 2))
(list x y)
;-> (4 3)
(setq x 1)
(setq y 2)
(setq z 3)
(psetq (x y z) ((+ x y z) (- z y x) (- x y z)))
;-> (+ 1 2 z)
;-> (+ 1 2 3)
;-> (- z 2 1)
;-> (- 3 2 1)
;-> (- 1 2 z)
;-> (- 1 2 3)
;-> ((+ 1 2 3) (- 3 2 1) (- 1 2 3))
(list x y z)
;-> (6 0 -4)
cameyo
p.s. thank you all for reactivating the forum