Check if the item in list or sub-list

老子叫甜甜 提交于 2019-12-11 07:49:02

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!