Author Topic: Need help with dotted pairs "; error: bad list:"  (Read 304 times)

0 Members and 1 Guest are viewing this topic.

dubb

  • Swamp Rat
  • Posts: 1047
Need help with dotted pairs "; error: bad list:"
« 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?

Lee Mac

  • Seagull
  • Posts: 12195
  • London, England
Re: Need help with dotted pairs "; error: bad list:"
« Reply #1 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).

When two atoms form a cons cell, 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"
« Last Edit: January 31, 2019, 06:02:09 PM by Lee Mac »

dubb

  • Swamp Rat
  • Posts: 1047
Re: Need help with dotted pairs "; error: bad list:"
« Reply #2 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).

When two atoms form a cons cell, 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"

Lee Mac

  • Seagull
  • Posts: 12195
  • London, England
Re: Need help with dotted pairs "; error: bad list:"
« Reply #3 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.

Grrr1337

  • Swamp Rat
  • Posts: 679
Re: Need help with dotted pairs "; error: bad list:"
« Reply #4 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 before COM was implemented.
So back then LISP was the suitable language in order to manipulate/access the DXF data structure, 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:
(apply ''((a b c)(a b c))
  '(
    (( f L ) (apply 'strcat (f L)))
    (( L ) (if L (cons (chr (car L)) (f (cdr L)))))
    (72 101 108 108 111 32 87 111 114 108 100)
  )
)

ronjonp

  • Needs a day job
  • Posts: 6911
Re: Need help with dotted pairs "; error: bad list:"
« Reply #5 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.  :-)
« Last Edit: January 31, 2019, 10:39:32 PM by ronjonp »

Windows 10 x64 - AutoCAD /C3D 2020

Custom Build PC

Grrr1337

  • Swamp Rat
  • Posts: 679
Re: Need help with dotted pairs "; error: bad list:"
« Reply #6 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.
(apply ''((a b c)(a b c))
  '(
    (( f L ) (apply 'strcat (f L)))
    (( L ) (if L (cons (chr (car L)) (f (cdr L)))))
    (72 101 108 108 111 32 87 111 114 108 100)
  )
)

dubb

  • Swamp Rat
  • Posts: 1047
Re: Need help with dotted pairs "; error: bad list:"
« Reply #7 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 before COM was implemented.
So back then LISP was the suitable language in order to manipulate/access the DXF data structure, 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: