Author Topic: How to use code to construct ((2 . ("1" "2" "3")))  (Read 1611 times)

0 Members and 1 Guest are viewing this topic.

kozmos

  • Newt
  • Posts: 114
How to use code to construct ((2 . ("1" "2" "3")))
« on: October 27, 2021, 10:20:15 PM »
It is for sure  '((2  ("1" "2" "3"))) and ' ((2 . ("1" "2" "3"))) are not same. I can only generate  ((2 . ("1" "2" "3"))) by directly define it but now I need to define it by code and I found there is no function can construct it. Using (cons 2 '("1" "2" "3")) will only get (2 "1" "2" "3"). Does anyone know how to do it?
KozMos Inc.

BIGAL

  • Swamp Rat
  • Posts: 1417
  • 40 + years of using Autocad
Re: How to use code to construct ((2 . ("1" "2" "3")))
« Reply #1 on: October 27, 2021, 10:40:28 PM »
Do you really need dotted pair ?

Are you trying to do repeated (cons 2 "a")(cons 2 "b") etc
A man who never made a mistake never made anything

roy_043

  • Water Moccasin
  • Posts: 1895
  • BricsCAD 18
Re: How to use code to construct ((2 . ("1" "2" "3")))
« Reply #2 on: October 28, 2021, 03:51:05 AM »
Don't know about AC but in BC (BricsCAD) this applies:
Code: [Select]
(equal '(2 . ("1" "2" "3")) '(2 "1" "2" "3")) => T

mhupp

  • Bull Frog
  • Posts: 250
Re: How to use code to construct ((2 . ("1" "2" "3")))
« Reply #3 on: October 28, 2021, 07:38:18 AM »
Don't know about AC but in BC (BricsCAD) this

That because BC hasn't been around for decades and gotten lazy with coding. BC usually go above and beyond with their coding and features.
One of the main reasons why I switched over. they listen to their customers and try to fix errors/features brought to their attention.

Another thing BC does it will actually give the area of a hatch made from a self intersecting polyline.
AC does not compute area = 0


steve.carson

  • Newt
  • Posts: 108
Re: How to use code to construct ((2 . ("1" "2" "3")))
« Reply #4 on: October 28, 2021, 01:32:30 PM »
Don't know about AC but in BC (BricsCAD) this applies:
Code: [Select]
(equal '(2 . ("1" "2" "3")) '(2 "1" "2" "3")) => T

They're equal in AC as well. Tested in C3D 2020.

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: How to use code to construct ((2 . ("1" "2" "3")))
« Reply #5 on: October 28, 2021, 05:58:24 PM »
The expression (2 . ("1" "2" "3")) is identical to (2 "1" "2" "3") both semantically and in the manner in which the data is retrieved by the AutoLISP list functions. In AutoLISP, lists are stored as singly linked lists whereby the contents of the address register (or car) will return the first element of the list, and the content of the decrement register (or cdr) will return the linked list which results from traversing to the next node in the link.

In the case of the dotted pair (aka ordered pair), the two elements are stored in consecutive blocks of memory, i.e. the content of the decrement register will yield the second element of the pair, as opposed to a pointer to the remainder of the linked list. However, since the second element in this case is itself a list, the dotted pair is interpreted as a linked list, as may be observed at the console -
Code - Auto/Visual Lisp: [Select]
  1. _$ (setq l '(2 . ("1" "2" "3")))
  2. (2 "1" "2" "3")

kozmos

  • Newt
  • Posts: 114
Re: How to use code to construct ((2 . ("1" "2" "3")))
« Reply #6 on: October 30, 2021, 09:23:43 AM »
Lee, thanks for the explanation. I already know if for one level list, (2 . (1 2 3)) is same as (2 1 2 3). What I always use is the  (2 . (1 2 3)) in a parent list and the parent list contains many items, normally like
((0 . "type")(1 . "value")(2 . ("text1" "text2" "text3"))(3 . (("text1" "abc")("text2" "123"))) ...)
In most situations, I just directly define the list by typing them as the contents are constant. I now just need to construct it by program and have the difficulty to do it by code.

As I discovered, for the parent list:
(cdr (assoc 3 lst))=> (("text1" "abc")("text2" "123"))
if define the parent list without the dot (by codes)
(cdr (assoc 3 lst))=> ((("text1" "abc")("text2" "123")))

It is obviously that having the dot will auto "strip" one level from the result. same as (cdr (assoc 2 '(2 . "a")))="a",  (cdr (assoc 2 '(2  "a")))=("a")

The expression (2 . ("1" "2" "3")) is identical to (2 "1" "2" "3") both semantically and in the manner in which the data is retrieved by the AutoLISP list functions. In AutoLISP, lists are stored as singly linked lists whereby the contents of the address register (or car) will return the first element of the list, and the content of the decrement register (or cdr) will return the linked list which results from traversing to the next node in the link.

In the case of the dotted pair (aka ordered pair), the two elements are stored in consecutive blocks of memory, i.e. the content of the decrement register will yield the second element of the pair, as opposed to a pointer to the remainder of the linked list. However, since the second element in this case is itself a list, the dotted pair is interpreted as a linked list, as may be observed at the console -
Code - Auto/Visual Lisp: [Select]
  1. _$ (setq l '(2 . ("1" "2" "3")))
  2. (2 "1" "2" "3")
KozMos Inc.

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: How to use code to construct ((2 . ("1" "2" "3")))
« Reply #7 on: October 30, 2021, 11:08:52 AM »
The dotted pair notation:
Code - Auto/Visual Lisp: [Select]
  1. _$ '(2 . ("1" . ("2" . ("3" . ()))))
  2. (2 "1" "2" "3")
stands for:
Code - Auto/Visual Lisp: [Select]
  1. _$ (cons 2 ( cons "1" (cons "2" (cons "3" nil))))
  2. (2 "1" "2" "3")
Speaking English as a French Frog

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: How to use code to construct ((2 . ("1" "2" "3")))
« Reply #8 on: October 31, 2021, 07:28:46 AM »
((0 . "type")(1 . "value")(2 . ("text1" "text2" "text3"))(3 . (("text1" "abc")("text2" "123"))) ...)
As I discovered, for the parent list:
(cdr (assoc 3 lst))=> (("text1" "abc")("text2" "123"))
if define the parent list without the dot (by codes)
(cdr (assoc 3 lst))=> ((("text1" "abc")("text2" "123")))

Defining the literal list:
Code: [Select]
'(3 . (("text1" "abc")("text2" "123")))...is equivalent to evaluating the expression:
Code: [Select]
(cons 3 '(("text1" "abc")("text2" "123")))...which will yield:
Code: [Select]
(3 ("text1" "abc")("text2" "123"))Hence the reason that:
Code: [Select]
(cdr (assoc 3 lst))Returns:
Code: [Select]
(("text1" "abc")("text2" "123"))
Whereas when you define the literal list as:
Code: [Select]
'(3 (("text1" "abc")("text2" "123")))...it is equivalent to evaluating the expression:
Code: [Select]
(list 3 '(("text1" "abc")("text2" "123")))... and therefore in this case, the expression:
Code: [Select]
(cdr (assoc 3 lst))...will return:
Code: [Select]
((("text1" "abc")("text2" "123")))