List Struture Equality
List Struture Equality
Hi,
Ok I need some help with thinking through this conceputally. I need to check if a list and another list is structurally equal.
For example:
(a (bc) de)) is the same as (f (gh) ij)), because they have the same structure.
Now cleary the base case will be if both list are empty they are structurally equal.
The recursive case on the other hand I'm not sure where to start.
Some ideas:
Well we are not going to care if the elements are == to each other because that doesn't matter. We just care in the structure. I do know we will car down the list and recursively call the function with the cdr of the list.
The part that confuses me is how do you determine wheter an atom or sublist has the same structure?
Any help will be appreciated.
Ok I need some help with thinking through this conceputally. I need to check if a list and another list is structurally equal.
For example:
(a (bc) de)) is the same as (f (gh) ij)), because they have the same structure.
Now cleary the base case will be if both list are empty they are structurally equal.
The recursive case on the other hand I'm not sure where to start.
Some ideas:
Well we are not going to care if the elements are == to each other because that doesn't matter. We just care in the structure. I do know we will car down the list and recursively call the function with the cdr of the list.
The part that confuses me is how do you determine wheter an atom or sublist has the same structure?
Any help will be appreciated.
-
- Posts: 406
- Joined: Sat Mar 07, 2009 6:17 pm
- Location: Brazil
- Contact:
Re: List Struture Equality
Well, If I got the idea, any two atoms have the same structure. Therefore, the base case of your function should test also if both arguments are atoms and, if the test succeeds, return T.
To see if sublists match each other, you just need to call the function recursively on them. In other words, if the function finds cons cells, the function must be called recursively on their CARs and CDRs.
Let me know if this is not clear enough for you.
To see if sublists match each other, you just need to call the function recursively on them. In other words, if the function finds cons cells, the function must be called recursively on their CARs and CDRs.
Let me know if this is not clear enough for you.
Re: List Struture Equality
Sorry I couldn't understand the last part
You mean Cons((function(car list)) (function(cdr list))
or something like that?
You mean Cons((function(car list)) (function(cdr list))
or something like that?
-
- Posts: 406
- Joined: Sat Mar 07, 2009 6:17 pm
- Location: Brazil
- Contact:
Re: List Struture Equality
What I meant is this:
In other words, if the arguments a and b are both cons cells, the function structurally-equal-p tests if (car a) and (car b) have the same structure using recursion. Then, it tests if (cdr a) and (cdr b) also have the same structure. If that is true, then a and b have the same structure and the function returns T.
Code: Select all
(defun structurally-equal-p (a b)
;; <- place base cases here
(if (and (consp a) (consp b))
(and (structurally-equal-p (car a) (car b))
(structurally-equal-p (cdr a) (cdr b)))))
Re: List Struture Equality
Whats is the difference between cons and consp?
Re: List Struture Equality
Forget it, I found what it was.
Does consp work with Dr Racket? I'm not home, so I can't try.
Does consp work with Dr Racket? I'm not home, so I can't try.
-
- Posts: 406
- Joined: Sat Mar 07, 2009 6:17 pm
- Location: Brazil
- Contact:
Re: List Struture Equality
Oos, sorry, now I've realized you are talking about Scheme
Anyway, Scheme should also have the function consp, so I guess that is a yes, but, instead of
you should use
Anyway, Scheme should also have the function consp, so I guess that is a yes, but, instead of
Code: Select all
(defun structurally-equal-p (a b)
...)
Code: Select all
(define (structurally-equal-p a b)
...)
-
- Posts: 94
- Joined: Mon Jul 21, 2008 7:26 am
- Location: München, Germany
- Contact:
Re: List Struture Equality
Scheme calls it pair?. Other than that, it should work the same way.gugamilare wrote:Anyway, Scheme should also have the function consp, so I guess that is a yes
Re: List Struture Equality
Code: Select all
(defun list-structure-eq (l1 l2)
(cond ((and (null l1) (null l2)) t)
((or (null l1) (null l2)) nil)
((and (listp (car l1)) (listp (car l2)))
(and (list-structure-eq (car l1) (car l2)) (list-structure-eq (cdr l1) (cdr l2))))
((and (atom (car l1)) (atom (car l2)))
(and (and (atom (car l1)) (atom (car l2))) (list-structure-eq (cdr l1) (cdr l2))))
(t nil)))
(list-structure-eq '(a (b c) d) '(1 (2 3) 4))
t
(list-structure-eq '(a (b) d) '(1 (2 3) 4))
nil
Code: Select all
(defun list-eq (l1 l2)
(cond ((and (null l1) (null l2)) t)
((or (null l1) (null l2)) nil)
((and (listp (car l1)) (listp (car l2)))
(and (list-eq (car l1) (car l2)) (list-eq (cdr l1) (cdr l2))))
((and (atom (car l1)) (atom (car l2)))
(and (eq (car l1) (car l2)) (list-eq (cdr l1) (cdr l2))))
(t nil)))
(list-eq '(a b c) '())
nil
nil
t
Re: List Struture Equality
ok whoops this is the Scheme channel. This might work if you replace the t form with an else one
pepsi7up wrote:common-lisp might already have a list atom equals..Code: Select all
(defun list-structure-eq (l1 l2) (cond ((and (null l1) (null l2)) t) ((or (null l1) (null l2)) nil) ((and (listp (car l1)) (listp (car l2))) (and (list-structure-eq (car l1) (car l2)) (list-structure-eq (cdr l1) (cdr l2)))) ((and (atom (car l1)) (atom (car l2))) (and (and (atom (car l1)) (atom (car l2))) (list-structure-eq (cdr l1) (cdr l2)))) (t nil))) (list-structure-eq '(a (b c) d) '(1 (2 3) 4)) t (list-structure-eq '(a (b) d) '(1 (2 3) 4)) nil
Code: Select all
(defun list-eq (l1 l2) (cond ((and (null l1) (null l2)) t) ((or (null l1) (null l2)) nil) ((and (listp (car l1)) (listp (car l2))) (and (list-eq (car l1) (car l2)) (list-eq (cdr l1) (cdr l2)))) ((and (atom (car l1)) (atom (car l2))) (and (eq (car l1) (car l2)) (list-eq (cdr l1) (cdr l2)))) (t nil))) (list-eq '(a b c) '()) nil nil t