Author Topic: Newbie to world of (Lost in Stupid Parentheses) errr..(List)  (Read 101267 times)

0 Members and 1 Guest are viewing this topic.

nivuahc

  • Guest
Newbie to world of (Lost in Stupid Parentheses) errr..(List)
« Reply #60 on: November 05, 2003, 12:52:00 PM »
Code: [Select]
(defun var-test ( / va vb vd vf)
(setq va "A"
      vb "B"
      vc "C"
      vd "D"
      ve "E"
      vf "F")
)




Copy and past that into AutoCAD  and run the function by typing this on the command line

(var-test)

Now, after it's been run type the following on the command line and watch what the results are:

!va

!vb

!vc

!vd

!ve

!vf


on a side note: typing an exclamation point followed by a variable name will tell you what the value of that variable is. This can be helpful when writing LISP

amgirard2003

  • Guest
Newbie to world of (Lost in Stupid Parentheses) errr..(List)
« Reply #61 on: November 05, 2003, 12:52:35 PM »
nivuahc,

I think i get it... I think as we get into it more that it will click in...

amgirard2003

  • Guest
Newbie to world of (Lost in Stupid Parentheses) errr..(List)
« Reply #62 on: November 05, 2003, 12:59:36 PM »
I see what happened... the variables after the / va vb vd vf were not assigned a value..
Variables vc ve were assigned a value..

I'm confused as to why that happened..

I was under the impression that when you used the
(setq) to give a value to a variable, the variable would then = that
value

 :?

daron

  • Guest
Newbie to world of (Lost in Stupid Parentheses) errr..(List)
« Reply #63 on: November 05, 2003, 01:02:59 PM »
Quote from: amgirard2003
Wouldn't c always = nil... not empty, not a flash in the pan, not destroyed


I'm not asking for the value of c. We supplied that. What is c that it works in append? Here's the help file on append. Maybe I'm a confusing teacher, but this should give up the answer.
Quote
append Function
 



Takes any number of lists and appends them together as one list

(append [list ...])

Arguments

list

A list.



Again, what is the empty object (container) of the variable of C?

nivuahc

  • Guest
Newbie to world of (Lost in Stupid Parentheses) errr..(List)
« Reply #64 on: November 05, 2003, 01:06:57 PM »
Quote from: amgirard2003
I see what happened... the variables after the / va vb vd vf were not assigned a value..
Variables vc ve were assigned a value..

I'm confused as to why that happened..

I was under the impression that when you used the
(setq) to give a value to a variable, the variable would then = that
value

 :?


Actually, all of those variables were assigned values.

The ones listed after the slash were 'emptied' after the function completed but the ones not included in that list retained their value thereby making them global variables.

amgirard2003

  • Guest
Newbie to world of (Lost in Stupid Parentheses) errr..(List)
« Reply #65 on: November 05, 2003, 01:14:51 PM »
From what i've read is it better to stay away from
global variables because you can make the mistake
and have other functions call on those variables?

JohnK

  • Administrator
  • Seagull
  • Posts: 10626
Newbie to world of (Lost in Stupid Parentheses) errr..(List)
« Reply #66 on: November 05, 2003, 01:19:26 PM »
As I see it Daron, 'c' is a name bound to a value of 'nil' nothing more. The space allocated for the Variable is reclaimed by the processor but the value of the name 'c' is left as 'nil'. So we have a "phantom" variable.  ...I think.  
(I mean this is kind of beyond me, theory wise. But, I can’t think of any other explanation.)
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

nivuahc

  • Guest
Newbie to world of (Lost in Stupid Parentheses) errr..(List)
« Reply #67 on: November 05, 2003, 01:26:31 PM »
Now this should really bake your noodle:

Code: [Select]
(defun local-test ( / va vb)
  (setq va "A")
  (setq vb "B")
  (princ (strcat "\nThe value of va is " va))
  (princ (strcat "\nThe value of vb is " vb))
  (princ)
)


Before you run that, set the values of va and vb to something else

(setq va "Z" vb "X")

Now test the values of each

!va

!vb

Now before we run our function above, what result do you think you will get?

Try it and let's see

(local-test)



Now we're going to test the values of each of our variables again. Again, before you do, what result do you think you will get?

!va

!vb



Care to take a shot at why that happens the way it does?

amgirard2003

  • Guest
Newbie to world of (Lost in Stupid Parentheses) errr..(List)
« Reply #68 on: November 05, 2003, 01:38:11 PM »
Quote

code:
--------------------------------------------------------------------------------
(defun local-test ( / va vb)
  (setq va "A")
  (setq vb "B")
  (princ (strcat "\nThe value of va is " va))
  (princ (strcat "\nThe value of vb is " vb))
  (princ)
)

Now we're going to test the values of each of our variables again. Again, before you do, what result do you think you will get?

!va

!vb



Care to take a shot at why that happens the way it does?
--------------------------------------------------------------------------------

O.k let me see if i get this one right for a change...

When the routine begins it sets Z to va and X to vb..
It then prints "The value of va is Z"
                  "The value of vb is X"
and then exits..
Why i think this happened is because it printed the values of va and vb to the screen before exiting and clearing the variables..

Correct??

nivuahc

  • Guest
Newbie to world of (Lost in Stupid Parentheses) errr..(List)
« Reply #69 on: November 05, 2003, 01:41:29 PM »
Did you try it out?

Because I think you'll be surprised when you do.


Also, I just wanted to mention... I'm loving this.

I remember what I was like when I started all of this and I love to see lightbulbs go off.

And, amgirard2003, don't feel bad if you don't fully grasp all of this right away.

Having no one to teach me, and relying solely on old books and old code to teach myself, I plugged along writing LISP for at least 4 years before I understood the difference between local and global variables.

amgirard2003

  • Guest
Newbie to world of (Lost in Stupid Parentheses) errr..(List)
« Reply #70 on: November 05, 2003, 01:44:38 PM »
nivuahc

You can call me by my first name which is Andre
amgirard is my full name... Andre, Michel, Girard

I know it will take some time to get this all to plug into my head
I'm here to learn and i want to learn all of this...

JohnK

  • Administrator
  • Seagull
  • Posts: 10626
Newbie to world of (Lost in Stupid Parentheses) errr..(List)
« Reply #71 on: November 05, 2003, 01:55:53 PM »
I get what your saying Daron. I see your point now. Your saying that when we assign 'c' to 'nil' we are creating an empty list.

That would make sense; Seeing as (setq c nil) is the same as saying (setq c '())  That's a cool lesson. Theres a lot to think about there.  *huh*
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

nivuahc

  • Guest
Newbie to world of (Lost in Stupid Parentheses) errr..(List)
« Reply #72 on: November 05, 2003, 01:57:01 PM »
From the Autocad command line (emphasis added):

Quote
Command: (defun local-test ( / va vb)
(_>   (setq va "A")
(_>   (setq vb "B")
(_>   (princ (strcat "\nThe value of va is " va))
(_>   (princ (strcat "\nThe value of vb is " vb))
(_>   (princ)
(_> )
LOCAL-TEST

Command: (setq va "Z" vb "X")
"X"

Command: !va
"Z"

Command: !vb
"X"

Command: (local-test)

The value of va is A
The value of vb is B

Command: !va
"Z"

Command: !vb
"X"


When you make a variable local, you make it local to the function that you used it in.

The variables va and vb are global variables that you set at the command line using

(setq va "Z" vb "X")

When you run the function (local-test) you set variables va and vb, local to the function, to A and B respectively. As local variables, they are 'emptied' when the function ends. But the variables that you set globally at the command line retained their value.

Doing this

Code: [Select]
(defun local-test ( / )
  (setq va "A")
  (setq vb "B")
  (princ (strcat "\nThe value of va is " va))
  (princ (strcat "\nThe value of vb is " vb))
  (princ)
)


would change the value of our global variables because they are not local to this function (notice that they are no longer following our slash). So when you test the variable values at the command line after running this function, you'll get a different result.

Quote
Command: !va
"A"

Command: !vb
"B"

daron

  • Guest
Newbie to world of (Lost in Stupid Parentheses) errr..(List)
« Reply #73 on: November 05, 2003, 01:57:35 PM »
When I started I had no book on lisp and still don't. I did have someone teach me things, but only if I'd ask questions. This left a lot in the air for me. I understood how things worked and when, but not why. Since April, under Se7en's tutilage, I've learned many new things. With that book and the tutilage you'll get here you should be able to avoid bad pratices as well as become a good lisp programmer at a rapid pace. It took me forever to understand functions and the power of the list and list iteration tools.

daron

  • Guest
Newbie to world of (Lost in Stupid Parentheses) errr..(List)
« Reply #74 on: November 05, 2003, 02:15:40 PM »
Quote from: Se7en
I get what your saying Daron. I see your point now. Your saying that when we assign 'c' to 'nil' we are creating an empty list.

That would make sense; Seeing as (setq c nil) is the same as saying (setq c '())  That's a cool lesson. Theres a lot to think about there.  *huh*

Yup, and what is LISP? LISt Processor. What else uses lists? foreach. Took me forever to understand how to use that one. I used repeat all the time. I'm learning in my vba book that there are three types of loops, determinate, indeterminate and infinate. Don't use infinate.
Types of determinate loops in lisp are repeat and while.
Types of indeterminate loops are foreach and vlax-for.
The difference in ability is the determinate loops run through a sequence a specified number of times. They also don't require lists to process.

indeterminate loops require lists or (vla-objects) to process and loop for as long as there is a list in the list collection. It doesn't matter how many items are in the collection, it just runs them until it's done.

The reason I bring this up is that I didn't understand what it took to get the indeterminate loops to work until Mark helped me understand that I wasn't supplying lists to the function.