Author Topic: Can't get the dotted list '(1 . nil) ?  (Read 2515 times)

0 Members and 1 Guest are viewing this topic.

highflyingbird

  • Bull Frog
  • Posts: 415
  • Later equals never.
Can't get the dotted list '(1 . nil) ?
« on: June 14, 2012, 08:25:12 PM »
When I use this ,  (list 1 . nil),actually, I want a dotted list, like this: '(1 . nil) but the result is:    (1).   even  I  use this way :  (cons 1 nil)    and  (quote( 1 nil))   ,I can't get I wanted.

So I see the vlisp help:
The value returned depends on the data type of list-or-atom. If list-or-atom is a list, cons returns that list with new-first-element added as the first item in the list. If list-or-atom is an atom, cons returns a dotted pair consisting of new-first-element and list-or-atom.
but:   
(atom nil) ---> T
(listp nil)---> T
So, I think : there is no way to the that kind of  dotted list,is that true?
I am a bilingualist,Chinese and Chinglish.

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: Can't get the dotted list '(1 . nil) ?
« Reply #1 on: June 14, 2012, 09:22:00 PM »
Perhaps you could explain why you need that construct?
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

highflyingbird

  • Bull Frog
  • Posts: 415
  • Later equals never.
Re: Can't get the dotted list '(1 . nil) ?
« Reply #2 on: June 14, 2012, 09:28:10 PM »
Perhaps you could explain why you need that construct?

I want to build a binomial tree model. when the right node is empty, I use  nil to express it.
I am a bilingualist,Chinese and Chinglish.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Can't get the dotted list '(1 . nil) ?
« Reply #3 on: June 14, 2012, 10:47:09 PM »

This is what I use.

Code - Auto/Visual Lisp: [Select]
  1.  ;|--------------------------------------------------------
  2. Function: (KDUB:Dotted-Pair-p arg )
  3. RETURN : T if arg is a dotted-pair list
  4. nil otherwise
  5. Notes:
  6. Anything that is not a list is considered an atom. NIL is an atom.
  7. ----------------------------------------------------------|;
  8.  
  9. (defun kdub:dotted-pair-p (arg) (and (not (atom arg)) (not (listp (cdr arg)))))
  10.  
  11.  ;|---------------------------------------------------------
  12. Function: (KDUB:normal-list-p   arg )
  13. RETURN : T if arg is a list
  14. nil if arg is atom or nil or dotted-pair list
  15. EXAMPLE:
  16. (KDUB:normal-list-p nil)       ==>> nil
  17. (KDUB:normal-list-p "abc")    ==>> nil
  18. (KDUB:normal-list-p '(1 . 3 )) ==>> nil
  19. (KDUB:normal-list-p '((1 2 3)(4 5 6)) ) ==>> T
  20. (KDUB:normal-list-p '((1 2 3)(4 . 5)(a b c)) ) =>> T
  21. (KDUB:normal-list-p '((1 . "first")(2 . "second")) ) ==>> T
  22. (KDUB:normal-list-p (car '((1 . "first")(2 . "second")) )) ==>> nil
  23. (KDUB:normal-list-p (car '(("a" "first")(2 . "second")) )) ==>> T
  24. ----------------------------------------------------------|;
  25. (defun kdub:normal-list-p (arg / return)
  26.   (setq return (if (not (atom arg))
  27.                  (vl-list-length arg)
  28.                  nil
  29.                )
  30.   )
  31.   (if (or (not return) (zerop return))
  32.     nil
  33.     t
  34.   )
  35. )
  36.  
  37.  ;|---------------------------------------------------------|;
  38.  


Code - Auto/Visual Lisp: [Select]
  1.  
  2. (setq result (cons 8 nil))
  3.  
  4. (kdub:dotted-pair-p result)  ;;-> nil
  5.  
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

highflyingbird

  • Bull Frog
  • Posts: 415
  • Later equals never.
Re: Can't get the dotted list '(1 . nil) ?
« Reply #4 on: June 14, 2012, 11:21:06 PM »

This is what I use.


Thanks ,Kerry. this is a good way.
I am a bilingualist,Chinese and Chinglish.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Can't get the dotted list '(1 . nil) ?
« Reply #5 on: June 14, 2012, 11:30:56 PM »

You're welcome :)
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

ribarm

  • Gator
  • Posts: 3297
  • Marko Ribar, architect
Re: Can't get the dotted list '(1 . nil) ?
« Reply #6 on: June 14, 2012, 11:51:57 PM »
nil is protected symbol, maybe you can use any other variable and eval them in calculations of dotted pair...

Code - Auto/Visual Lisp: [Select]
  1. (setq n T)
  2. (eval (cdr (cons 1 'n)))
  3. ---> T
  4.  
n - variable assigned value T

Code - Auto/Visual Lisp: [Select]
  1. (setq n nil) <--- if variable haven't previously been used, you can skip this line
  2. (eval (cdr (cons 1 'n)))
  3. ---> nil
  4.  
n - variable not defined

Code - Auto/Visual Lisp: [Select]
  1. (cons 1 'n)
  2. ---> (1 . N)
  3.  
constructed dotted pair with symbol-atom 1 and unprotected symbol-atom N

also consider this :
Code - Auto/Visual Lisp: [Select]
  1. (cons 1 nil)
  2. ---> (1)
  3. (eval (cdr (cons 1 nil)))
  4. ---> nil  just like  (cons 1 nil)->(1 . nil)
  5.  
  6. if (setq T nil)
  7. (cons 1 T)
  8. ---> (1 . T)
  9. (eval (cdr (cons 1 T)))
  10. ---> nil  just like (cons 1 T)->(1)->(1 . nil)
  11.  
  12. else if T undefined
  13. (cons 1 T)
  14. ---> (1 . T)
  15. (eval (cdr (cons 1 T)))
  16. ---> T
  17.  

as you wrote :
(atom nil) -> T
(listp nil) -> T
(eval nil) -> nil

but
(atom T) -> T  (atom 1) -> T  (atom N) -> T
(listp T) -> nil  (listp 1) ->nil  (listp N) -> nil
and
(eval T) -> T  (eval 1) -> 1  (eval N) -> nil
M.R.
« Last Edit: June 15, 2012, 12:13:50 AM by ribarm »
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

Faster

  • Guest
Re: Can't get the dotted list '(1 . nil) ?
« Reply #7 on: June 15, 2012, 12:13:31 AM »
(setq a '(1 . 2) b '(1 . nil) c '(1))

(car a) ==> 1  (cdr a) ==> 2

(car b) ==> 1 (cdr b) ==> nil

(car c) ==> 1 (cdr c) ==> nil

so

b = c   :  (equal '(1 . nil) '(1)) ==> t

irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Re: Can't get the dotted list '(1 . nil) ?
« Reply #8 on: June 15, 2012, 07:22:05 AM »
I think you're over-complicating the binary tree structure: Consider this:
Code - Auto/Visual Lisp: [Select]
  1. (defun BTree:GetVal (node /) (car node))
  2. (defun BTree:GetLeft (node /) (cadr node))
  3. (defun BTree:GetRight (node /) (caddr node))
  4. (defun BTree:SetVal (node val /) (list val (BTree:GetLeft node) (BTree:GetRight node)))
  5. (defun BTree:SetLeft (node left /) (list (BTree:GetVal node) left (BTree:GetRight node)))
  6. (defun BTree:SetRight (node right /) (list (BTree:GetVal node) (BTree:GetLeft node) right))
  7.  
  8. (setq TestTree1 '(1 (2 (4 (8)) (5)) (3 (6) (7)))
  9.       TestTree2 '(1 (2 (4 (8) (9)) (5 (10) (11))) (3 (6 (12) (13)) (7 (14) (15)))))
  10.  
  11. (defun BTree:Height  (node /)
  12.   (if node
  13.     (1+ (max (BTree:Height (BTree:GetLeft node)) (BTree:Height (BTree:GetRight node))))
  14.     0))
  15.  
  16. (defun BTree:GetLevel  (root level / GetLevel)
  17.   (defun GetLevel  (node count /)
  18.     (cond ((null node) nil)
  19.           ((zerop count) (list (BTree:GetVal node)))
  20.           (t (append (GetLevel (BTree:GetLeft node) (1- count)) (GetLevel (BTree:GetRight node) (1- count))))))
  21.   (GetLevel root level))
  22.  
  23. (defun BTree:Traverse-ByLevel (root func / n height)
  24.   (setq n -1
  25.         height (BTree:Height root))
  26.   (while (< (setq n (1+ n)) height)
  27.     (mapcar 'func (BTree:GetLevel root n))))
E.g.
Code: [Select]
_$ (BTree:Traverse-ByLevel TestTree1 print)

1
2
3
4
5
6
7
8 (8)
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.

Lee Mac

  • Seagull
  • Posts: 12922
  • London, England
Re: Can't get the dotted list '(1 . nil) ?
« Reply #9 on: June 15, 2012, 07:29:04 AM »
Regarding binary trees, unless I have misunderstood, maybe the following thread will help you, reading from this post onwards:

http://www.theswamp.org/index.php?topic=35191.msg404387#msg404387

irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Re: Can't get the dotted list '(1 . nil) ?
« Reply #10 on: June 15, 2012, 07:31:15 AM »
Or a more "proper" level traversal function and including blank nodes as nil values:
Code - Auto/Visual Lisp: [Select]
  1. (defun BTree:GetLevel  (root level / GetLevel)
  2.   (defun GetLevel  (node count /)
  3.     (cond ((null node) '(nil))
  4.           ((zerop count) (list (BTree:GetVal node)))
  5.           (t (append (GetLevel (BTree:GetLeft node) (1- count)) (GetLevel (BTree:GetRight node) (1- count))))))
  6.   (GetLevel root level))
  7.  
  8. (defun BTree:Traverse-ByLevel (root func-node func-level / n height)
  9.   (setq n -1
  10.         height (BTree:Height root))
  11.   (while (< (setq n (1+ n)) height)
  12.     (func-level n)
  13.     (mapcar 'func-node (BTree:GetLevel root n)))
  14.   (princ))
Then watch this  :laugh: :
Code: [Select]
_$ (BTree:Traverse-ByLevel TestTree1
(lambda (val) (prin1 val) (princ "\t"))
(lambda (n) (princ "\nLevel ") (princ n) (princ ":\t")))

Level 0:    1   
Level 1:    2    3   
Level 2:    4    5    6    7   
Level 3:    8    nil    nil    nil    nil    nil    nil    nil   
Note though, my level traversal is extremely inefficient. You'd probably be better off using some sort of queue structure and iteration.

Regarding binary trees, unless I have misunderstood, maybe the following thread will help you, reading from this post onwards:

http://www.theswamp.org/index.php?topic=35191.msg404387#msg404387
My point exactly, nested lists is exactly a tree structure in Lisp. Binary trees are simply nested lists which never have more than 2 items for each list (value left-list right-list).
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.

irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Re: Can't get the dotted list '(1 . nil) ?
« Reply #11 on: June 15, 2012, 07:55:52 AM »
Binary trees are simply nested lists which never have more than 2 items for each list (value left-list right-list).
Sorry, make that 3 items per list  :-[
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.