我在自己的代码中完成了过滤功能和反向功能
(define reverse_
(lambda (xs)
(if (null? xs)
xs
(append (reverse_ (cdr xs))
(list (car xs))))))
和
(define filter_
(lambda (p? xs)
(if (null? xs)
xs
(append (if (p? (car xs))
(list (car xs))
(list))
(filter_ p? (cdr xs))))))
我想将这两个函数合并到 (reverse-filter)
函数中,即您可以输入 (reverse-filter symbol? '(1 2 3 a b c))
它将返回 -> c b a
。
现在只需输入 (reverse_ (filter_ symbol? '(1 2 3 a b c))) -> c b a
但我只想将两者结合起来。
在一般情况下和在这个特定情况下执行此操作的任何帮助将不胜感激
最佳答案
对于一般情况,我们可以使用 curry
和 compose
过程(希望在您的解释器中可用),它们允许我们操纵其他过程:
((compose (curry filter_ symbol?) reverse_)
'(1 2 3 a b c))
=> '(c b a)
出于说明目的,这里是两个过程的简单实现,以了解它们在幕后所做的事情:
(define (curry f x)
(lambda (y) (f x y)))
(define (compose f g)
(lambda (x) (f (g x))))
关于function - 在Scheme中组合两个函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41021481/