Reputation: 1
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.:
Upvotes: 0
Views: 121
Reputation: 18937
A few comments:
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(procedure param1 param2 ...)
, the conditions inside cond
are in parenthesesequal?
predicate has a trailing ?
#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)))))
Upvotes: 2
Reputation: 370455
(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.
Upvotes: 0