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 parameter lst instead
  • you have lot of parentheses errors - procedure calls (procedure param1 param2 ...), conditions inside cond in 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

Popular posts from this blog

routing - AngularJS State management ->load multiple states in one page -

python - GRASS parser() error -

Swift game error message -