0
votes

I am trying to complete a school problem using lists in Racket. It is a simple problem using recursion but I cannot understand why my code won't work. We are supposed to search the list and return true if the item provided matches one in the list. This is what I have so far:

(define (containsAnywhere test  list)
  (cond 
    ((null? list) '())
    (equal?((car list) test))
    (else(containsAnywhere (test (cdr list))))))

But I get the following error:

application: not a procedure;
 expected a procedure that can be applied to arguments
  given: 1
  arguments.:
2

2 Answers

2
votes

A few comments:

  • don't call your parameter list, there's a build-in procedure of that name which will be shadowed this way; in Scheme it's common to call the parameter lst instead
  • you have a lot of parentheses errors - procedure calls are like (procedure param1 param2 ...), the conditions inside cond are in parentheses
  • the equal? predicate has a trailing ?
  • and your code's indentation is off; use Dr Racket's "reindent all" feature
  • I assume the result of your procedure is supposed to be True or False, so the empty list should return #f, not '()

Here's a working version of your code; inside cond I put the condition and the expression to be evaluated on separate lines so that the code and the parentheses become clearer:

(define (containsAnywhere test lst)
  (cond 
    ((null? lst)
     #f)
    ((equal? (car lst) test)
     #t)
    (else
     (containsAnywhere test (cdr lst)))))

Alternatively, I would code this like:

(define (containsAnywhere tst lst)
  (and (not (null? lst))
       (or (equal? (car lst) tst)
           (containsAnywhere tst (cdr lst)))))
0
votes
(test (cdr list))

Here you're applying test to the argument (cdr list) as if test were a function, but it's not. Thus the error.

You presumably meant (containsAnywhere test (cdr list)), which passes test as the first argument to containsAnywhere and (cdr list) as the second.