Jump to content

Question: Setting multiple variables


Recommended Posts

Posted

Into Chapter two in my VisualLISP book, just had a quick question.... would this be a correct example of setting variables??

(setq nbr1 5 nbr2 12 str1 "text")

If this is correct, then wouldn't it be just a more efficient way of doing this:

(setq nbr1 5)
(setq nbr2 12)
(setq str1 "text")

Posted

1. Yes, that is a correct way

2. also correct... more efficient? more typing

I prefer something similar to this

(setq nbrl 5
     nbr2 12
     str1 "text"
);setq

easier to read than the first example (for me anyhow)

 

more than 1 way to skin a cat

Posted

Thanks for the approval. And I see your point regarding the syntax formatting. I used the Tools > Format Code in Editor tool and it set the code exactly how you did, minus the comment obviously. Thanks again.... more questions to come, hopefully I won't bombard the forum too much with a buncha beginner questions.

Posted

Actually it will add the comment if you want

Tools>Environment options>Visual Lisp Format Options

in this case it would read ;_ end of setq... that can be edited also

J1.jpg

Posted
hopefully I won't bombard the forum too much with a buncha beginner questions.

 

Bring them on... :thumbsup:

Posted

Definitely ask the questions Styk - about time we had someone asking about learning LISP rather than requesting one... o:)

 

As for your question...

 

Pretty much what Larry said, both are correct.

 

The "setq" function takes any number of pairs of arguments:

 

(setq [i]sym expr [sym expr][/i]...) 

^^ from Visual LISP Help files.

 

IMO, I prefer to group similar variable assignments together in one setq, and maybe use another statement for unlrelated variable assignments.

 

As for speed:

 

(defun All_in_one (/ a b c d e f g h i j)
 (setq a 1 b 2 c 3 d 4 e 5 f 6 g 7 h 8 i 9 j 10))

(defun Separate (/ a b c d e f g h i j)
 (setq a 1)
 (setq b 2)
 (setq c 3)
 (setq d 4)
 (setq e 5)
 (setq f 6)
 (setq g 7)
 (setq h 
 (setq i 9)
 (setq j 10))

(defun c:test nil

 (Benchmark
   '(
     (All_in_one)
     (Separate)
     )
   )

 (princ))

Elapsed milliseconds / relative speed for 65536 iteration(s):

   (SEPARATE).......1856 / 1.15 <fastest>
   (ALL_IN_ONE).....2137 / 1.00 <slowest>

There really isn't much in it.

 

Lee

Posted
The "setq" function takes any number of pairs of arguments:
It's logical to me that the setq function takes a pair of arguments, being that you have the function > name of the function > then the argument that the name calls for. So, if you have multiple pairs of arguments for a setq function, then can you have multiple arguments that each function name will call for? Example:

(setq nbr1 (3 (+ 2 5))
     nbr2 (4 (* 3 6))
     nbr3 (1 (- 4 7))
)

So would the posted code be correct? Thanks again for the explanation. I'm just trying to get all this to sink in.

Posted
It's logical to me that the setq function takes a pair of arguments, being that you have the function > name of the function > then the argument that the name calls for. So, if you have multiple pairs of arguments for a setq function, then can you have multiple arguments that each function name will call for? Example:

(setq nbr1 (3 (+ 2 5))
     nbr2 (4 (* 3 6))
     nbr3 (1 (- 4 7))
)

So would the posted code be correct? Thanks again for the explanation. I'm just trying to get all this to sink in.

 

Remember, lisp is always evaluated from the inside out...

So your code above would work IF you were doing something with those first integers like add/subtract/multiply/etc.

 


(setq nbr1 (* 3 (+ 2 5))
     nbr2 (+ 4 (* 3 6))
     nbr3 (- 1 (- 4 7))
)

Posted

ahhhhh, I see. I goofed on my example. But the (* 3 (+ 2 5)) would be considered two arguments for the calling symbol nbr1, correct?

Posted

1 argument - closing parenthesis

nbr1 would be set to 21

Posted

Right, a statement like (+ 3 (abs (1+ (/ 21.2 16.8)))) is considered a single expression, because all of that will be evaluated from the inside out and return a single value to any calling function (like SETQ)

 

A good explanation can be found in the

Developers Guide>Using the Autolisp Language>Autolisp Basics>Autolisp Expressions

Posted

Styk,

 

When you say "function name" I assume you mean symbol/variable that you supply to the setq function as an argument.

 

When looking at LISP, what you must always keep in mind is the return values of the functions you are dealing with.

 

Lets look at your example:

 

(setq nbr1 (* 3 (+ 2 5))
     nbr2 (+ 4 (* 3 6))
     nbr3 (- 1 (- 4 7))
)

As Larry points out, LISP is evaluated "from the inside out", so in your example, the code is evaluated as follows:

 

[color=Blue][b](+ 2 5)[/b][/color]  returns [b][color=Red]7[/color][/b]
[color=Blue][b](* 3 (+ 2 5))[/b][/color]  becomes [b][color=Blue](* 3[/color][/b] [b][color=Red]7[/color][color=Blue])[/color][/b]  which returns [b][color=Red]21[/color][/b].

So in the setq:

 

[b][color=Blue](+ 2 5)[/color][/b]  returns [b][color=Red]7[/color][/b]
[b][color=Blue](* 3 (+ 2 5))[/color][/b]  becomes [color=Blue][b](* 3[/b][/color] [b][color=Red]7[/color][color=Blue])[/color] [/b] which returns [b][color=Red]21[/color][/b].
[color=Blue][b](setq nbr1 (* 3 (+ 2 5)))[/b][/color] becomes [b][color=Blue] (setq nbr1 (* 3[/color][/b] [color=Red][b]7[/b][/color][color=Blue][b]))[/b][/color] becomes  [color=Blue][b](setq nbr1[/b][/color] [b][color=Red]21[/color][color=Blue]) [/color][/b]

An hence you have the two arguments (symbol and value) as shown. After the setq is evaluated the symbol "nbr1" becomes a variable.

Posted
Right, a statement like (+ 3 (abs (1+ (/ 21.2 16.8)))) is considered a single expression, because all of that will be evaluated from the inside out and return a single value to any calling function (like SETQ)

 

A good explanation can be found in the

Developers Guide>Using the Autolisp Language>Autolisp Basics>Autolisp Expressions

Thanks, I'll look into this.

 

When you say "function name" I assume you mean symbol/variable that you supply to the setq function as an argument.
Yes, Lee I meant symbol or better yet variable. I'll get the lingo down in soon time. And thanks for the very detailed explanation. That helped a lot. I now understand it fully. Next question coming soon I'm sure....
Posted
I now understand it fully. Next question coming soon I'm sure....

 

You're welcome... bring on the next one :D

Posted
(setq nbr1 5 nbr2 12 str1 "text")

Another thing to keep in mind is that in this scenario, only the last value ( "text" ) is returned.

 

 [b][color=BLACK]([/color][/b]setq c 200
       i 0[b][color=BLACK])[/color][/b]
 [b][color=BLACK]([/color][/b]while [b][color=FUCHSIA]([/color][/b]< [b][color=NAVY]([/color][/b]setq c [b][color=MAROON]([/color][/b]1+ c[b][color=MAROON])[/color][/b] i [b][color=MAROON]([/color][/b]1+ i[b][color=MAROON])[/color][/b][b][color=NAVY])[/color][/b] 10[b][color=FUCHSIA])[/color][/b]
        [b][color=FUCHSIA]([/color][/b]prin1 i[b][color=FUCHSIA])[/color][/b][b][color=BLACK])[/color][/b]
 [b][color=BLACK]([/color][/b]prin1 c[b][color=BLACK])[/color][/b]

 

Even though c is increasing with each inteneration, the (1+ i) is the last returned value and is the test value for the while call

while i is less than 10

 

-David

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...