0

试图制作一个名为 make-odd-mapper 的方案过程!它应该是一个过程,它接受一个输入,一个过程,并产生一个过程作为输出

前任:

(定义 i4 (mlist 10 2 30 4))

(i4)

{10 2 30 4}

((make-odd-mapper!add-one) i4)

i4

{11 2 31 4}

我知道问题需要改变输入列表和 set-mcar!和 void 是其中的一部分......谁能给我一些合理的代码行来解决这个问题?如果有人想知道突变......并使用它来创建一个将过程作为其输出的过程......这将很有用。

4

1 回答 1

0

好吧,简而言之,所提出的问题是不可能的。原因是,如果可能的话,你可以有一个表达式:

(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))))))
于 2011-06-05T17:03:58.180 回答