0

This assignment is to check if both lists are equal by creating my own function to do so, with this i am getting it to define the function. The issue i am having is when i try to call the function using (listEquals list1 list2) it is returning an error of

application: not a procedure;
expected a procedure that can be applied to arguments
given: #f

I am giving a input of two lists list1 = (1 2 3 4), list2 = (2 3 4 5).

Here is my current implimentation

(define listEquals (lambda (list1 list2)
     (if (eq? list1 null) 

            (if (eq? list2 null)
                (true)
            (false)) 
     (if (eq? list2 null) 

            (false) 
            (if (eq? (first list1) (first list2))

                (listEquals (rest  list1) (rest list2))
            (false))))))
Felauras
  • 71
  • 9

2 Answers2

1

You must not surround true, false with brackets, they're not procedures, hence they can not be applied. Try this:

(define listEquals
  (lambda (list1 list2)
    (if (eq? list1 null)
        (if (eq? list2 null)
            true
            false)
        (if (eq? list2 null) 
            false
            (if (eq? (first list1) (first list2))
                (listEquals (rest  list1) (rest list2))
                false)))))
Óscar López
  • 232,561
  • 37
  • 312
  • 386
0

You've already accepted an answer, but here's a different approach that doesn't involve conditionals.

Two lists are equal if an only if

  • Either both are empty or both are non-empty, and
    • they are empty, or
    • their first elements are equal, and their tails are equal

In scheme:

(define (list-equals ls1 ls2)
    (and (equal? (empty? ls1) (empty? ls2))
         (or (empty? ls1) ; Only need to check one since they're equal
             (and (eq? (first ls1) (first ls2))
                  (list-equals (rest ls1) (rest ls2))))))
molbdnilo
  • 64,751
  • 3
  • 43
  • 82