【发布时间】:2018-10-27 23:53:36
【问题描述】:
我正在尝试实现一个函数 composex,它给定函数列表 funcs 返回一个函数,该函数是 funcs 中所有函数的组合
所以,
输入 -> [f1 f2 f3 ...]
输出 -> f'(x) = f1(f2(f3( ... (x))))
我的代码sn-p是:
(define (reducex initial f arr)
(if (null? arr)
initial
(reducex (f initial (car arr))
f
(cdr arr)
)
)
)
(define (abst x)
(if (< x 0)
(- x)
x
)
)
(define (mult2 x)
(* x 2))
(define (composex funcs)
(lambda (x)
(reducex '()
(lambda (ini, f) (f x))
funcs)
)
)
(define absmult2 (composex (cons abst (cons mult2 '()))))
(absmult2 2)
(absmult2 -2)
我得到的错误是在composex
;The object (unquote f), passed as an argument to identifier->symbol, is not an identifier.
;To continue, call RESTART with an option number:
; (RESTART 1) => Return to read-eval-print level 1.
我正在使用 mit-scheme 执行。
【问题讨论】:
标签: lambda functional-programming scheme function-composition mit-scheme