Racket List Error Using Recursion -
i trying complete school problem using lists in racket. simple problem using recursion cannot understand why code won't work. supposed search list , return true if item provided matches 1 in list. have far:
(define (containsanywhere test list) (cond ((null? list) '()) (equal?((car list) test)) (else(containsanywhere (test (cdr list)))))) but following error:
application: not procedure; expected procedure can applied arguments given: 1 arguments.:
a few comments:
- don't call parameter
list, there's build-in procedure of name shadowed way; in scheme it's common call parameterlstinstead - you have lot of parentheses errors - procedure calls
(procedure param1 param2 ...), conditions insidecondin parentheses - the
equal?predicate has trailing? - and code's indentation off; use dr racket's "reindent all" feature
- i assume result of procedure supposed true or false, empty list should return
#f, not'()
here's working version of code; inside cond put condition , expression evaluated on separate lines code , parentheses become clearer:
(define (containsanywhere test lst) (cond ((null? lst) #f) ((equal? (car lst) test) #t) (else (containsanywhere test (cdr lst))))) alternatively, code like:
(define (containsanywhere tst lst) (and (not (null? lst)) (or (equal? (car lst) tst) (containsanywhere tst (cdr lst)))))
Comments
Post a Comment