### Author Topic: The difference between set and setq  (Read 281 times)

0 Members and 1 Guest are viewing this topic.

#### baitang36

• Newt
• Posts: 51
##### The difference between set and setq
« on: March 25, 2020, 01:03:30 AM »
First look at the source code:
(defun aa nil
(setq a 88)
(set 'a 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"
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.

#### hanhphuc

• Newt
• Posts: 62
##### Re: The difference between set and setq
« Reply #1 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.

( apply 'equal "hp" "happy" "hạnh phúc" "ハッピー" "幸福" "행복" ) ; error: too many arguments

#### MP

• Seagull
• Posts: 17577
##### Re: The difference between set and setq
« Reply #2 on: March 25, 2020, 08:38:50 AM »
(mapcar 'set '(x y z) '(1.0 2.0 3.0))
\|// Set goal. Experiment tirelessly until
|Oo| practice has become expertise.  Loop.

#### MP

• Seagull
• Posts: 17577
##### Re: The difference between set and setq
« Reply #3 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
\|// Set goal. Experiment tirelessly until
|Oo| practice has become expertise.  Loop.

#### Marc'Antonio Alessi

• Swamp Rat
• Posts: 1058
• Marco
##### Re: The difference between set and setq
« Reply #4 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

#### MP

• Seagull
• Posts: 17577
##### Re: The difference between set and setq
« Reply #5 on: March 26, 2020, 09:39:23 PM »
\|// Set goal. Experiment tirelessly until
|Oo| practice has become expertise.  Loop.