Author Topic: How to determine equality of nesting  (Read 612 times)

0 Members and 1 Guest are viewing this topic.

Jeremy Dunn

  • Newt
  • Posts: 24
How to determine equality of nesting
« on: May 19, 2019, 04:14:04 PM »
Let us say I want to determine if two expressions have the same form or not. I will call the function form= to write examples. Here are some examples to illustrate

(form= 3 (a b)) -> nil
(form= (a (b c)) (a b c)) -> nil
(form= (1 (2 3)) (5 (6 7))) -> T

So the idea is that expressions can be equal even if the individual elements are not and that only the number of elements and the nesting of the elements are important. Anyone have a function that can do this?

gile

  • Water Moccasin
  • Posts: 2231
  • Marseille, France
Re: How to determine equality of nesting
« Reply #1 on: May 19, 2019, 05:24:37 PM »
Hi,

If the order of items matters
(foo '(1 (2 3)) '(5 (6 7))) return T
(foo '(1 (2 3)) '((6 7) 5)) return nil
Code - Auto/Visual Lisp: [Select]
  1. (defun foo (a b)
  2.   (or
  3.     (and (atom a) (atom b))
  4.     (and (listp a)
  5.          (listp b)
  6.          (= (length a) (length b))
  7.          (vl-every 'foo a b)
  8.     )
  9.   )
  10. )

If the order does not matter
(bar '(1 (2 3) (b)) '(5 (6 7) (9))) returns T
(bar '(1 (2 3) (b)) '(5 (9) (6 7))) returns T
Code - Auto/Visual Lisp: [Select]
  1. (defun bar (a b)
  2.   (or
  3.     (and (atom a) (atom b))
  4.     (and (listp a)
  5.          (listp b)
  6.          (= (length a) (length b))
  7.          (vl-every 'bar
  8.                    (vl-sort (vl-remove-if 'atom a) '(lambda (x y) (< (length x) (length y))))
  9.                    (vl-sort (vl-remove-if 'atom b) '(lambda (x y) (< (length x) (length y))))
  10.          )
  11.     )
  12.   )
  13. )
Speaking English as a French Frog

myloveflyer

  • Newt
  • Posts: 128
Re: How to determine equality of nesting
« Reply #2 on: May 23, 2019, 11:26:59 PM »
Hi,

If the order of items matters
(foo '(1 (2 3)) '(5 (6 7))) return T
(foo '(1 (2 3)) '((6 7) 5)) return nil
Code - Auto/Visual Lisp: [Select]
  1. (defun foo (a b)
  2.   (or
  3.     (and (atom a) (atom b))
  4.     (and (listp a)
  5.          (listp b)
  6.          (= (length a) (length b))
  7.          (vl-every 'foo a b)
  8.     )
  9.   )
  10. )

If the order does not matter
(bar '(1 (2 3) (b)) '(5 (6 7) (9))) returns T
(bar '(1 (2 3) (b)) '(5 (9) (6 7))) returns T
Code - Auto/Visual Lisp: [Select]
  1. (defun bar (a b)
  2.   (or
  3.     (and (atom a) (atom b))
  4.     (and (listp a)
  5.          (listp b)
  6.          (= (length a) (length b))
  7.          (vl-every 'bar
  8.                    (vl-sort (vl-remove-if 'atom a) '(lambda (x y) (< (length x) (length y))))
  9.                    (vl-sort (vl-remove-if 'atom b) '(lambda (x y) (< (length x) (length y))))
  10.          )
  11.     )
  12.   )
  13. )
Gile,Nice Work!
Never give up !

Lee Mac

  • Seagull
  • Posts: 12265
  • London, England
Re: How to determine equality of nesting
« Reply #3 on: May 25, 2019, 12:06:40 PM »
Nice code gile  :-)

Here's an alternative to allow for dotted pairs -
Code - Auto/Visual Lisp: [Select]
  1. (defun foo ( a b )
  2.     (or (and (atom  a) (atom  b))
  3.         (and (listp a) (listp b)
  4.              (= (vl-list-length a) (vl-list-length b))
  5.              (foo (car a) (car b))
  6.              (foo (cdr a) (cdr b))
  7.         )
  8.     )
  9. )
Code - Auto/Visual Lisp: [Select]
  1. _$ (foo 1 1)
  2. T
  3. _$ (foo 1 '(1))
  4. nil
  5. _$ (foo 1 '(1 . 1))
  6. nil
  7. _$ (foo '(1) '(1))
  8. T
  9. _$ (foo '(1) '(1 . 1))
  10. nil
  11. _$ (foo '(1 . 1) '(1 . 1))
  12. T

bruno_vdh

  • Mosquito
  • Posts: 2
Re: How to determine equality of nesting
« Reply #4 on: June 14, 2019, 04:40:01 AM »
Hello,
Maybe I do not understand the subject, but why?
Quote
_$ (foo '(1) '(1 . 1))
nil

If we consider this:
Code: [Select]
_$ (car '(1.1))
1.1
_$ (car '(1))
1
_$ (cdr '(1.1))
nil
_$ (cdr '(1))
nil

So this definition seems to me preferable ...
Code: [Select]
(defun f (a b)
  (cond
    ((atom a) (atom b))
    ((atom b) nil)
    ((f (car a) (car b)) (f (cdr a) (cdr b)))
  )
)

Code: [Select]
_$ (f 1 1)
T
$ (f 1 '(1))
nil
_$ (f 1 '(1 . 1))
nil
_$ (f '(1) '(1 . 1))
T
_$ (f '(1 . 1) '(1 . 1))
T

Lee Mac

  • Seagull
  • Posts: 12265
  • London, England
Re: How to determine equality of nesting
« Reply #5 on: June 14, 2019, 08:10:08 AM »
Quote
_$ (foo '(1) '(1 . 1))
nil

If we consider this:
Code: [Select]
_$ (car '(1.1))
1.1
_$ (car '(1))
1
_$ (cdr '(1.1))
nil
_$ (cdr '(1))
nil

'(1 . 1) is very different to '(1.1) - the former represents a dotted pair, the latter a list containing a single item.

As such, the comparison would become:
Code - Auto/Visual Lisp: [Select]
  1. _$ (car '(1 . 1))
  2. 1
  3. _$ (car '(1))
  4. 1
  5. _$ (cdr '(1 . 1))
  6. 1
  7. _$ (cdr '(1))
  8. nil


bruno_vdh

  • Mosquito
  • Posts: 2
Re: How to determine equality of nesting
« Reply #6 on: June 14, 2019, 12:07:26 PM »
Yes I was too fast in my conclusions, thanks for correcting my mistake