gpt4 book ai didi

function - 在 Scheme 中结合两个函数

转载 作者:太空宇宙 更新时间:2023-11-03 18:48:01 24 4
gpt4 key购买 nike

我在自己的代码中完成了过滤功能和反向功能

(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 即可工作,但我只想将两者结合起来。

在一般情况下和在这个特定情况下,我们将不胜感激

最佳答案

对于一般情况,我们可以使用 currycompose程序(希望在您的解释器中可用),它们允许我们操纵其他程序:

((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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com