问题
I want to use the check function to check if the item is in the list or the sub-list. But the error really confuse me. Can someone tell me what's wrong with my code?
(define check
(lambda(item lis)
(cond((null? lis) #f)
(else(if(pair? (car lis))
(if(check item (car lis)) #t (check item (cdr lis)))
(if(equal? item (car list)) #t (check item (cdr lis))))))))
> (check 'a '(a b))
. . car: contract violation
expected: pair?
given: #<procedure:list>
回答1:
You have a typo in here:
(equal? item (car list))
It should be:
(equal? item (car lis))
Notice that list
is a procedure, and the parameter in your code is called lis
.
来源:https://stackoverflow.com/questions/19436282/check-if-the-item-in-list-or-sub-list