I need some help with a lisp function logic problem

I need some help with a lisp function logic problem

stanovb
Advocate Advocate
766 Views
7 Replies
Message 1 of 8

I need some help with a lisp function logic problem

stanovb
Advocate
Advocate

This is a problem from a Common Lisp book that I am going through, but it should apply to Autolisp as well. The book provides the answer, but I wish it went through exactly why the answer is what it is. It does state It will help to write down what value each variable is bound to and, of course, mind the quotes! 

 

Here is the the definition of the function:

(defun stooge (larry moe curly)
(list larry (list ’moe curly) curly ’larry))

 

Then the problem asks What does the following evaluate to?

(stooge ’moe ’curly ’larry)

 

The result is (MOE (MOE LARRY) LARRY LARRY) but I'm having trouble following the logic 100%. I know that the list function creates a new cons cell chain, and you usually work from inside out if there are parenthesis. A symbol has to be quoted & a variable does not, so in the inside parenthesis moe is quoted, but curly is not, which makes me think that moe is the symbol which doesnt get evaluated because it is quoted, but it is the global symbol moe, and curly is a local variable that evaluates to the value that is assigned to it. Somehow larry gets assigned to curly in the list function in the inside parenthesis. The book does state that it is a real head scratcher so hopefully I'm not the only person who has struggled with this problem.

 

If someone could walk me through the logic of this problem so I could understand it I would appreciate it. I know that a symbol evaluates to the value of the variable it refers to, but I'm having some trouble wrapping my head around this.

0 Likes
Accepted solutions (1)
767 Views
7 Replies
Replies (7)
Message 2 of 8

john.uhden
Mentor
Mentor

@stanovb ,

Boy, that was confusing.

(defun stooge (larry moe curly)
(list larry (list ’moe curly) curly ’larry))

The thing to recognize is that referenced quoted symbols get their value from outside the function, but input values are assigned in the order of specified input symbols.

Since 'moe and 'larry refer to outside values, and neither moe nor larry  has an outside value, their values are all nil.

Your example:

Command: (stooge 'larry 'moe 'curly)
(LARRY (nil CURLY) CURLY nil)

Result:

You see that LARRY and CURLY came through as the symbol names they are, but when combined as a list, moe has no value and neither does 'larry have a value.

Now if we substitute values (not just symbol name) for the input parameters we get a different result...

Command: (stooge "larry" "moe" "curly")
("larry" (nil "curly") "curly" nil)

Result:

That looks sorta the same as the previous evaluation, BUT this time the inputs had values (strings), which were treated as such, but 'moe and 'larry were referencing "outside" values that were both nil.

Now if you (setq moe "A" larry "B" curly "C") before calling the function, you get the following...

Command: (setq moe "A" larry "B" curly "C")
"C"

Command: (stooge 'larry 'moe 'curly)
(LARRY (nil CURLY) CURLY nil)

Hmm, that's the same result?!

Command: (stooge larry moe curly)
("B" (nil "C") "C" nil)

That's a little more expected except for Larry.

I know how quoted symbols make a difference, but now you have left me equally confused.

I can only suggest that you don't use quoted symbols.

 

 

 

John F. Uhden

0 Likes
Message 3 of 8

Sea-Haven
Mentor
Mentor

What sort of list are you trying to make ? Just post start values and what your looking for but leave out code in middle.

 

(setq lst (list 1 2 3))
do something
result( 1 3 6)
0 Likes
Message 4 of 8

stanovb
Advocate
Advocate

I'm sorry about the confusion. Was my explanation confusing or the function itself? If my explanation was the confusing part, I tried to explain what I understood to be the case such as a symbol is quoted to avoid it being evaluated since it refers to a global symbol that has no value or a value of nil as you put it.  Unless of course  it is  assigned to something within the local scope of the function. I was under the impression that if the value is unassigned you would get an error saying  "unassigned value" or something like that if you don't quote it & then you try to evaluate it. I guess maybe I didn't do a good job explaining that.

 

If the function was the  confusing part, This function is from a book so its not something I came up with. I was having trouble understanding it so thats why I reached out for some help. I guess they were just trying to teach you what  values certain variables are bound to. I guess that's why they stated that it was a real head scratcher. 

0 Likes
Message 5 of 8

komondormrex
Mentor
Mentor
Accepted solution

komondormrex_0-1689574559670.png

 

you also need to consider that autolisp and common lisp are somewhat different things.

0 Likes
Message 6 of 8

stanovb
Advocate
Advocate

Thank you. This is very helpful. I guess because curly is listed again in the body of the definition it gets assigned Larry from the 3rd argument again for the second time.

0 Likes
Message 7 of 8

Sea-Haven
Mentor
Mentor

Have a look at this link there is lots of examples of List manipulation, I think the examples will provide more clarity than the rather obsure example.

 

Challenges (theswamp.org)

0 Likes
Message 8 of 8

stanovb
Advocate
Advocate

Thanks. I'm a member at the swamp so thanks

0 Likes