TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: baitang36 on March 25, 2020, 01:03:30 AM

Title: The difference between set and setq
Post by: baitang36 on March 25, 2020, 01:03:30 AM
First look at the source code:
(defun aa nil
(setq a 88)
(set 'a 88)
(princ (read "bb"))
(set (read "bb") 88)


Through experiments, (setq a 88) and (set 'a 88) are completely equivalent. Setq is set quote.
After compiling to fas, the code looks like this:
14 00  00  00 00                                                              Instruction 20, the function starts
32 58 06 05 00                                                                (setq a 88)
09 05 00 32 58 35 02 04 00 03 0A                                    (set 'a 88)
09 03 00 35 01 02 00 03 35 01 01 00 03 0A                     (princ (read "bb"))
09 03 00 35 01 02 00 03 32 58 35 02 04 00 03                 (set (read "bb") 88)
16                                                                                  Instruction 22, the function ends
String used:
05 / A
04 / SET
03 / "bb"
02 / READ
01 / PRINC

It can be seen that (setq a 88) was compiled into an instruction, and the function was completed directly with instruction no. 6
(set 'a 88) is compiled to call function SET, which takes two arguments, one with the symbol a (05) and the other with 88
In conclusion, setq does not need to call functions, it is faster, and the compiled code is shorter and more concise.
Title: Re: The difference between set and setq
Post by: hanhphuc on March 25, 2020, 04:48:27 AM

Through experiments, (setq a 88) and (set 'a 88) are completely equivalent. Setq is set quote.


hi nice research but..
set only accepts single sym+expr whereas setq (set quote) accepts multiple symbols & expressions

Code - Auto/Visual Lisp: [Select]
  1.  
  2. (setq a 1 b "2"  ) ; (mapcar 'set '(a b) '(1 "2"))
  3.  
  4. (set a 1 b "2" )     ; too many arguments
  5. (set 'a "1" 'b "2" ) ; too many arguments
  6.  
  7.  





Title: Re: The difference between set and setq
Post by: MP on March 25, 2020, 08:38:50 AM
(mapcar 'set '(x y z) '(1.0 2.0 3.0))
Title: Re: The difference between set and setq
Post by: MP on March 25, 2020, 10:54:20 AM
Useful if one wants to swap var values in one go:

(setq a 1 b 2)

!a >> 1
!b >> 2

(mapcar 'set '(a b) (list b a))

!a >> 2
!b >> 1
Title: Re: The difference between set and setq
Post by: Marc'Antonio Alessi on March 25, 2020, 02:22:53 PM
Useful if one wants to swap var values in one go:

(setq a 1 b 2)

!a >> 1
!b >> 2

(mapcar 'set '(a b) (list b a))

!a >> 2
!b >> 1
8)
Title: Re: The difference between set and setq
Post by: MP on March 26, 2020, 09:39:23 PM
:)