我正在使用来自The Little schemer的这个脚本,以获得两组的交集。但我在“会员”中收到未绑定的标识符错误,有人可以告诉它有什么问题:
(define intersect
(lambda (set1 set2)
(cond ((null? set1) (quote ()))
((member? (car set1) set2)
(cons (car setl)
(intersect (cdr set1) set2)))
(else (intersect (cdr setl) set2)))))
我在上面遗漏了这个功能:
(define member?
(lambda (a lat)
(cond ((null? lat) #f)
(else (or (eq? (car lat) a)
(member? a (cdr lat)))))))
另外,我想要交叉两个列表,如:'((1 2)(2 7))'((1 3)(4 5))='((1 5)),有关如何去的任何建议关于它?我正在查看这篇文章的答案:How to write a scheme function that takes two lists and returns four lists
答案 0 :(得分:1)
你在intersect
中有一个拼写错误,你已经用1作为小写字母L切换了1。如果你确定你的intersect
如果你正在比较符号那么我很好。例如
(define intersect
(lambda (set1 set2)
(cond
((null? set1)(quote ()))
((member? (car set1) set2)
(cons (car set1)
(intersect (cdr set1) set2)))
(else (intersect (cdr set1) set2)))))
(intersect '(a b c d) '(c d e f)) ; ==> (c d)
要比较符号以外的其他内容,您需要更改member?
,以便它使用equal?
代替eq?
。它会是这样的:
(define member?
(lambda (a lat)
(cond
((null? lat) #f)
(else (or (equal? (car lat) a) ; changed eq? to equal?
(member? a (cdr lat)))))))
(intersect '((1 2)(3 4)(5 6)) '((9 10) (7 8) (5 6))) ; ==> ((5 6))
即使在此之后。上面的符号版本仍然有效。
在任何LISP(至少Common Lisp和Scheme)中,你有member
。它使用equal
并在未找到时评估为false
(在实现中为false),如果找到它,则从找到元素的位置开始计算其余的参数列表(这是被认为是真的):
(member 'a '(x y a c)) ; ==> (a c)
使用标准成员而不是您自己的谓词:
(define intersect
(lambda (set1 set2)
(cond
((null? set1)(quote ()))
((member (car set1) set2)
(cons (car set1)
(intersect (cdr set1) set2)))
(else (intersect (cdr set1) set2)))))
(intersect '((1 2)(3 4)(5 6)) '((9 10) (7 8) (5 6))) ; ==> ((5 6))
(intersect '(a b c d) '(c d e f)) ; ==> (c d)
编辑1
您似乎没有搜索intersection
,而是搜索特殊的alist
:
#!r6rs
(import (rnrs base)
(rnrs lists))
;; if you dont have r6rs remove the above and
;; uncomment this rnrs/lists-6 memp
#;(define (memp predicate? lst)
(cond ((null? lst) #f)
((predicate? lst) lst)
(else (memp predicate? (cdr lst)))))
(define (alist-merge merge-proc alist-1 alist-2)
(if (null? alist-1)
'()
(let* ((name (caar alist-1))
(found (memp (lambda (x) (equal? (car x) name)) alist-2)))
(if found
(cons (merge-proc (car alist-1) (car found))
(alist-merge merge-proc
(cdr alist-1)
alist-2))
(alist-merge merge-proc
(cdr alist-1)
alist-2)))))
(define (alist-merge-add alist-1 alist-2)
(alist-merge (lambda (x y)
(list (car x)
(+ (cadr x) (cadr y))))
alist-1
alist-2))
(alist-merge-add '((1 2)(2 7)) '((1 3)(4 5))) ; ==> ((1 5))
答案 1 :(得分:0)
我的路口解决方案:
#lang racket
(define (intersect set1 set2)
(cond [(empty? set1) '()]
[(empty? set2) '()]
[(= (caar set1) (caar set2)) (cons (list (caar set1)
(+ (cadar set1)
(cadar set2)))
(intersect (cdr set1) (cdr set2)))]
[(< (caar set1) (caar set2)) (intersect (cdr set1) set2)]
[else (intersect set1 (cdr set2))]))