Parallel assignment

Q&A's, tips, howto's
Post Reply
cameyo
Posts: 115
Joined: Sun Mar 27, 2011 3:07 pm
Location: Italy

Parallel assignment

Post by cameyo »

Hi all,
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)
Does anyone have any advice to improve it?
cameyo
p.s. thank you all for reactivating the forum

newBert
Posts: 149
Joined: Fri Oct 28, 2005 5:33 pm
Location: France

Re: Parallel assignment

Post by newBert »

Just another way to do it (with what comes to hand):

Code: Select all

> (setq x 2 y 3)
3
> (map set '(x y) (list (+ 1 y) (+ 1 x)))
(4 3)

> (setq x 1 y 2 z 3)
3
> (map set '(x y z) (list (+ x y z) (- z y x) (- x y z)))
(6 0 -4)
 
:)
Last edited by newBert on Mon May 25, 2020 8:55 am, edited 1 time in total.
BertrandnewLISP v.10.7.6 64-bit on Linux (Peppermint 9)

cameyo
Posts: 115
Joined: Sun Mar 27, 2011 3:07 pm
Location: Italy

Re: Parallel assignment

Post by cameyo »

Thanks !
I suspected that the map function could have been useful... :-)

newBert
Posts: 149
Joined: Fri Oct 28, 2005 5:33 pm
Location: France

Re: Parallel assignment

Post by newBert »

Yes, map is a very useful function which works well in parallel.
It is handy too to take the place of ‘list comprehensions’...
BertrandnewLISP v.10.7.6 64-bit on Linux (Peppermint 9)

Post Reply