Scheme - 如果 'map/filter' 中有多个参数,我该如何使用该函数?
Scheme - How do I use a function in 'map/filter' if it has multiple arguments?
我一直在尝试创建一个子集?,会员?和交集函数,使用预定义的过滤器函数和映射函数的一些变体(ormap、andmap)。
"member?" 函数应该检查元素是否是列表的成员:
(define member?
(lambda (e s)
(ormap (curry same-elem? e) s)))
它正在使用一个名为 "same-elem?" 的函数,如下所示:
(define same-elem?
(lambda (e1 e2)
(cond ((and (set? e1) (set? e2)) (same-set? e1 e2))
((and (number? e1) (number? e2)) (= e1 e2))
(else (equal? e1 e2)))))
"subset?" 检查一组中的元素是否属于另一组:
(define subset?
(lambda (s2 s1)
(andmap (curry member? s2) s1)))
'intersection' 函数应该return 两个集合的交集:
(define intersection
(lambda (s2 s1)
(filter (curry member? s2) s1)))
我使用 'curry' 函数的原因是因为我试图遵循此 Whosebug 线程中的解决方案: 试图将第二个参数传递到我调用的函数中。不过没用。
每当我调用这些函数中的任何一个时,它都会输出一个错误,表明它希望我的成员在我的 ormap 调用中有一个列表?功能,但有一个元素。奇怪的是我的会员?直到几分钟前功能都运行良好......我什至没有碰过任何东西!
我是编程新手,对 Scheme 完全陌生。如果 map/filter 中的被调用函数只有一个参数,我知道如何使用 map/filter 函数......但是如果我有多个参数,我该如何处理呢?请帮忙。
编辑:我在下面选择的解决方案不适用于 curry。但是,如果您删除 curry 函数并改为编写匿名函数,它确实有效。
像这样:
(define member?
(lambda (e s)
(ormap (lambda (x)
(same-elem? e x)) s)))
您的方法是正确的,您只需要注意参数的顺序 - 例如,member?
应该接收一个集合作为其 first 参数,如果您打算咖喱它,因为 andmap
会将 单个元素 传递给生成的 lambda
表单。另外,请注意,使用中的高阶过程期望 lists 作为参数,而不是内置 set
数据的实际 sets类型。这就是我的意思:
; tests if `e` is a member of `s`
(define member?
(lambda (s e)
(ormap (curry same-elem? e) s)))
; tests if `s1` is a subset of `s2`
(define subset?
(lambda (s1 s2)
(andmap (curry member? s2) s1)))
; finds the intersection of `s1` and `s2`
(define intersection
(lambda (s1 s2)
(filter (curry member? s1) s2)))
它们按预期工作:
(member? '(a b c d e) 'c)
=> #t
(subset? '(c d) '(a b c d e))
=> #t
(intersection '(c d b a) '(a x y b))
=> '(a b)
我一直在尝试创建一个子集?,会员?和交集函数,使用预定义的过滤器函数和映射函数的一些变体(ormap、andmap)。
"member?" 函数应该检查元素是否是列表的成员:
(define member?
(lambda (e s)
(ormap (curry same-elem? e) s)))
它正在使用一个名为 "same-elem?" 的函数,如下所示:
(define same-elem?
(lambda (e1 e2)
(cond ((and (set? e1) (set? e2)) (same-set? e1 e2))
((and (number? e1) (number? e2)) (= e1 e2))
(else (equal? e1 e2)))))
"subset?" 检查一组中的元素是否属于另一组:
(define subset?
(lambda (s2 s1)
(andmap (curry member? s2) s1)))
'intersection' 函数应该return 两个集合的交集:
(define intersection
(lambda (s2 s1)
(filter (curry member? s2) s1)))
我使用 'curry' 函数的原因是因为我试图遵循此 Whosebug 线程中的解决方案:
每当我调用这些函数中的任何一个时,它都会输出一个错误,表明它希望我的成员在我的 ormap 调用中有一个列表?功能,但有一个元素。奇怪的是我的会员?直到几分钟前功能都运行良好......我什至没有碰过任何东西!
我是编程新手,对 Scheme 完全陌生。如果 map/filter 中的被调用函数只有一个参数,我知道如何使用 map/filter 函数......但是如果我有多个参数,我该如何处理呢?请帮忙。
编辑:我在下面选择的解决方案不适用于 curry。但是,如果您删除 curry 函数并改为编写匿名函数,它确实有效。
像这样:
(define member?
(lambda (e s)
(ormap (lambda (x)
(same-elem? e x)) s)))
您的方法是正确的,您只需要注意参数的顺序 - 例如,member?
应该接收一个集合作为其 first 参数,如果您打算咖喱它,因为 andmap
会将 单个元素 传递给生成的 lambda
表单。另外,请注意,使用中的高阶过程期望 lists 作为参数,而不是内置 set
数据的实际 sets类型。这就是我的意思:
; tests if `e` is a member of `s`
(define member?
(lambda (s e)
(ormap (curry same-elem? e) s)))
; tests if `s1` is a subset of `s2`
(define subset?
(lambda (s1 s2)
(andmap (curry member? s2) s1)))
; finds the intersection of `s1` and `s2`
(define intersection
(lambda (s1 s2)
(filter (curry member? s1) s2)))
它们按预期工作:
(member? '(a b c d e) 'c)
=> #t
(subset? '(c d) '(a b c d e))
=> #t
(intersection '(c d b a) '(a x y b))
=> '(a b)