TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: dubb on January 31, 2019, 05:27:57 PM

Title: Need help with dotted pairs "; error: bad list:"
Post by: dubb on January 31, 2019, 05:27:57 PM
Created a dotted list
Code: [Select]
(setq lst (cons 2 "test"))
Call the first element
Code: [Select]
Command: (nth 0 lst)
2

Call the second element  :idiot2:
Code: [Select]
Command: (nth 1 lst)
; error: bad list: "test"

What do I do?
Title: Re: Need help with dotted pairs "; error: bad list:"
Post by: Lee Mac on January 31, 2019, 05:38:24 PM
A dotted pair is not the same as an AutoLISP list (which is stored in memory as a linked list (https://en.wikipedia.org/wiki/Linked_list)).

When two atoms form a cons cell (https://en.wikipedia.org/wiki/Cons), the second atom occupies the decrement register and is returned by the cdr function, as opposed to the tail of the linked list. As such, when the nth function attempts to traverse the list as a linked list, accessing any element except the first will result in an error as the nth function is presented with an atom as opposed to the tail of the list.

Hence, to access the second element of a dotted pair, you would use the cdr function in the following way:
Code: [Select]
Command: (cdr lst)
"test"
Title: Re: Need help with dotted pairs "; error: bad list:"
Post by: dubb on January 31, 2019, 06:07:24 PM
Thank you!
Thank you!
Thank you!
Wow! How come I didn't try that?!

I have always thought CADR would give me the second element in a list.
Code: [Select]
(CADR TEST)
; error: bad argument type: consp "test"

From Autodesk website:
CADR
Returns the second element of a list
CDR
Returns a list containing all but the first element of the specified list


A dotted pair is not the same as an AutoLISP list (which is stored in memory as a linked list (https://en.wikipedia.org/wiki/Linked_list)).

When two atoms form a cons cell (https://en.wikipedia.org/wiki/Cons), the second atom occupies the decrement register and is returned by the cdr function, as opposed to the tail of the linked list. As such, when the nth function attempts to traverse the list as a linked list, accessing any element except the first will result in an error as the nth function is presented with an atom as opposed to the tail of the list.

Hence, to access the second element of a dotted pair, you would use the cdr function in the following way:
Code: [Select]
Command: (cdr lst)
"test"
Title: Re: Need help with dotted pairs "; error: bad list:"
Post by: Lee Mac on January 31, 2019, 06:15:33 PM
I have always thought CADR would give me the second element in a list.

From Autodesk website:
CADR
Returns the second element of a list
CDR
Returns a list containing all but the first element of the specified list

That is correct, however as noted in my post, you are not working with a list, you are working with a dotted pair - this distinction between the two included within the 'Return Values' section of the documentation (http://help.autodesk.com/view/ACD/2018/ENU/?guid=GUID-F9CD8FF3-022A-4323-BAE7-390174451537).
Title: Re: Need help with dotted pairs "; error: bad list:"
Post by: Grrr1337 on January 31, 2019, 07:07:01 PM
AFAIK Dotted pairs, also known as key-value pairs are used within DXF structure, where DXF was intended to provide an exact representation of the data in the AutoCAD native file format (https://en.wikipedia.org/wiki/AutoCAD_DXF) before COM (https://en.wikipedia.org/wiki/Component_Object_Model) was implemented.
So back then LISP was the suitable language in order to manipulate/access the DXF data structure (https://www.autodesk.com/techpubs/autocad/acadr14/dxf/general_file_structure_al_u05_b.htm), which is represented by DXF Lists (or list of dotted pairs).
Now the ACAD's Object model provides an alternative way of accessing/manipulating data, which seems to be more understandable (and even more comfortable in other languages like VBA).
Although with vanilla lisp one could dig a bit deeper into the object's data (for instance the parameters of a dynamic block).

Ofcourse, I might be wrong, cause I'm young!  :lol:
Title: Re: Need help with dotted pairs "; error: bad list:"
Post by: ronjonp on January 31, 2019, 10:36:27 PM
...

Ofcourse, I might be wrong, cause I'm young!  :lol:
We are all young at once .. the strength is strong in you my friend.  :-)
Title: Re: Need help with dotted pairs "; error: bad list:"
Post by: Grrr1337 on February 01, 2019, 06:31:35 AM
...

Ofcourse, I might be wrong, cause I'm young!  :lol:
We are all young at once .. the strength is strong in you my friend.  :-)

Thanks, although the reason I'm sharing it is not something to brag about, but to point out that my post/statement is not based on a personal experience.
So I will accept any corrections on it. :)

About aging... well physical aging is inevitable, but mental aging depends on one.. so the goal is to reach a good physical/mental age proportion
in order to become one day a valuable guy in his sixties with a 120 years of experience, and not some s2pid @ss grandpa villager.
Title: Re: Need help with dotted pairs "; error: bad list:"
Post by: dubb on February 05, 2019, 02:36:49 PM
Thanks for the explanation. I love you guys!
AFAIK Dotted pairs, also known as key-value pairs are used within DXF structure, where DXF was intended to provide an exact representation of the data in the AutoCAD native file format (https://en.wikipedia.org/wiki/AutoCAD_DXF) before COM (https://en.wikipedia.org/wiki/Component_Object_Model) was implemented.
So back then LISP was the suitable language in order to manipulate/access the DXF data structure (https://www.autodesk.com/techpubs/autocad/acadr14/dxf/general_file_structure_al_u05_b.htm), which is represented by DXF Lists (or list of dotted pairs).
Now the ACAD's Object model provides an alternative way of accessing/manipulating data, which seems to be more understandable (and even more comfortable in other languages like VBA).
Although with vanilla lisp one could dig a bit deeper into the object's data (for instance the parameters of a dynamic block).

Ofcourse, I might be wrong, cause I'm young!  :lol: