50.5k views
0 votes
Write a Common Lisp predicate function that tests for the structural equality of two given lists. Two lists are structurally equal if they have the same list structure, although their atoms may be different. A script of defining and testing the function in Clisp on the empress system must be submitted.

User Lordjeb
by
4.6k points

1 Answer

4 votes

Answer:

Step-by-step explanation:

For the Program plan:

1. Two lists are structurally equal if they have the same list structure, although their atoms may be different.

2. So we are going to write a function that will check if elements in both lists at same position are atoms or not.

3. If yes then it should return true else false.

Program:

#lang scheme

( define (structurally-equal list1 list2)

(cond ((and (null? list1) (null? list2)) #t)

((or (null? list1) (null? list2)) #f)

((and (atom? (car list1)) (atom? (car list2)))

(structurally-equal (cdr list1) (cdr list2)))

((or (atom? (car list1)) (atom? (car list2))) #f)

(else (and (structurally-equal (car list1) (car list2))

(structurally-equal (cdr list1) (cdr list2) )))))

( define (atom? x) (not (or (pair? x) (null? x))))

User Kcwu
by
4.9k points