检查列表或子列表中的项目 [英] Check if the item in list or sub-list

查看:113
本文介绍了检查列表或子列表中的项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用检查功能检查项目是否在列表或子列表中. 但是这个错误确实使我感到困惑.有人可以告诉我我的代码有什么问题吗?

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>

推荐答案

您在这里输入错字了:

(equal? item (car list))

应该是:

(equal? item (car lis))

请注意,list是一个过程,代码中的参数称为lis.

Notice that list is a procedure, and the parameter in your code is called lis.

这篇关于检查列表或子列表中的项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆