我有这个清单
(define masterList(list redApple chickenLeg porkLoin milkD baguetteBread orangeJuice beanCan))
我正在尝试创建一个名为lookup的函数,该函数将获取一个整数并从列表中返回与该整数匹配的元素。列表包含这种格式的结构中的元素
(define-struct storeItem (id des cost))
因此,我将传递一个表示id的整数,并希望重新调整storeItem。
例如-
(define redApple (make-storeItem 0 "red delicious apple" 1.99))
如果我搜索masterList并将其传递为0,我希望会返回redApple。
对语法有帮助吗?
(define (contains masterList x)
(cond
((null? masterList) #f)
((eq? (car masterList) x) #t)
(else (contains (cdr masterList) x))))
这就是我要工作的地方。
它会根据我通过的内容正确返回true / false。
(包含materList redApple)返回true。
如果输入0,我该如何修改它以返回redApple?
;; These are the constructors to make the elements in our structure
(define redApple (make-storeItem 0 "red delicious apple" 1.99))
(define chickenLeg (make-storeItem 1 "boned chicken" 2.99))
(define porkLoin (make-storeItem 2 "processed pork" 4.99))
(define milkD (make-storeItem 3 "vitamin d milk" 3.99))
(define baguetteBread (make-storeItem 4 "french bread" 0.99))
(define orangeJuice (make-storeItem 5 "fruit juice drink)" 1.49))
(define beanCan (make-storeItem 6 "beans in a can" 2.49)
;; Creating a list that we will use as our master list which contains elements from our structure
(define masterList(list redApple chickenLeg porkLoin milkD baguetteBread orangeJuice beanCan))
答案 0 :(得分:0)
您的一般结构是正确的,但是您没有使用
测试正确的条件INSERT INTO language VALUES (1,'English');
INSERT INTO language VALUES (2,'Italian');
INSERT INTO FILM (film_id,title, description, language_id, original_language_id ) VALUES (100,'B Movie', 'Movie about wasps.', 1, 2);
SELECT * from film;
您不想将(eq? (car masterList) x)
与列表的整个元素进行比较,而仅将其与x
的组成部分进行比较。所以应该是:
id
此外,要比较数字,应使用(= (storeItem-id (car masterList)) x)
或=
而不是eqv?
。参见What is the difference between eq?, eqv?, equal?, and = in Scheme?
如果要获取具有ID的对象,而不仅仅是eq?
或#true
,请在ID匹配时返回#false
。
(car masterList)