Code Red > AutoLISP (Vanilla / Visual)

How to determine equality of nesting

(1/2) > >>

Jeremy Dunn:
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:
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: ---(defun foo (a b)  (or    (and (atom a) (atom b))    (and (listp a)         (listp b)         (= (length a) (length b))         (vl-every 'foo a b)    )  ))
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: ---(defun bar (a b)  (or    (and (atom a) (atom b))    (and (listp a)         (listp b)         (= (length a) (length b))         (vl-every 'bar                   (vl-sort (vl-remove-if 'atom a) '(lambda (x y) (< (length x) (length y))))                   (vl-sort (vl-remove-if 'atom b) '(lambda (x y) (< (length x) (length y))))         )    )  ))

myloveflyer:

--- Quote from: gile 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: ---(defun foo (a b)  (or    (and (atom a) (atom b))    (and (listp a)         (listp b)         (= (length a) (length b))         (vl-every 'foo a b)    )  ))
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: ---(defun bar (a b)  (or    (and (atom a) (atom b))    (and (listp a)         (listp b)         (= (length a) (length b))         (vl-every 'bar                   (vl-sort (vl-remove-if 'atom a) '(lambda (x y) (< (length x) (length y))))                   (vl-sort (vl-remove-if 'atom b) '(lambda (x y) (< (length x) (length y))))         )    )  ))
--- End quote ---
Gile,Nice Work!

Lee Mac:
Nice code gile  :-)

Here's an alternative to allow for dotted pairs -

--- Code - Auto/Visual Lisp: ---(defun foo ( a b )    (or (and (atom  a) (atom  b))        (and (listp a) (listp b)             (= (vl-list-length a) (vl-list-length b))             (foo (car a) (car b))             (foo (cdr a) (cdr b))        )    ))
--- Code - Auto/Visual Lisp: ---_$ (foo 1 1)T_$ (foo 1 '(1))nil_$ (foo 1 '(1 . 1))nil_$ (foo '(1) '(1))T_$ (foo '(1) '(1 . 1))nil_$ (foo '(1 . 1) '(1 . 1))T

bruno_vdh:
Hello,
Maybe I do not understand the subject, but why?

--- Quote ---_$ (foo '(1) '(1 . 1))
nil

--- End quote ---

If we consider this:

--- Code: ---_$ (car '(1.1))
1.1
_$ (car '(1))
1
_$ (cdr '(1.1))
nil
_$ (cdr '(1))
nil

--- End code ---

So this definition seems to me preferable ...

--- Code: ---(defun f (a b)
  (cond
    ((atom a) (atom b))
    ((atom b) nil)
    ((f (car a) (car b)) (f (cdr a) (cdr b)))
  )
)

--- End code ---


--- Code: ---_$ (f 1 1)
T
$ (f 1 '(1))
nil
_$ (f 1 '(1 . 1))
nil
_$ (f '(1) '(1 . 1))
T
_$ (f '(1 . 1) '(1 . 1))
T

--- End code ---

Navigation

[0] Message Index

[#] Next page

Go to full version