好吧,简而言之,所提出的问题是不可能的。原因是,如果可能的话,你可以有一个表达式:
(make-odd-mapper! add-one)
这将是由部分应用创建的函数的函数。但是,此函数必须修改其操作数,而这只能通过宏来完成。因此,该函数的结果将是一个宏,这是不可能的,因为宏不作为值存在。但是,通过稍微改变定义,make-odd-mapper!
就有可能做一些稍微不同的事情。在这种情况下,您将完全按照原始问题中的方式使用它,除了不是说
((make-odd-mapper! add-one) i4)
你会说
(make-odd-mapper! add-one i4)
这是这样做的代码:
;;; Applies its argument to every other element of a list.
(define map-every-other
(lambda (function lis)
(let map-every-other ((function function) (lis lis) (acc '()) (other #t))
(if (null? lis)
acc
(map-every-other
function
(cdr lis)
(append acc (list (if other (function (car lis)) (car lis))))
(not other))))))
;;; This function does the odd mapping, but returns
;;; the new function instead of mutating the original.
(define make-odd-mapper
(lambda (function-to-apply)
(lambda (function)
(lambda ()
(map-every-other function-to-apply (function))))))
;;; This macro mutates the original function by using make-odd-mapper
(define-syntax make-odd-mapper!
(syntax-rules ()
((_ function-to-apply function)
(begin
(set! function
((make-odd-mapper function-to-apply) function))))))