试图制定一个计划程序

发布于 2024-10-24 16:49:32 字数 408 浏览 1 评论 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 是分开的......有人能给我一些合理的代码行来解决这个问题吗?如果有人想知道突变......并使用它来创建一个将过程作为其输出的过程,这将很有用......

trying to make a scheme procedure called make-odd-mapper! its supposed to be a procedure that takes one input, a procedure, and produces a procedure as an output

ex:

(define i4 (mlist 10 2 30 4))

(i4)

{10 2 30 4}

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

i4

{11 2 31 4}

I know the problem needs to mutate the input list and that set-mcar! and void are apart of it......could anyone give me some reasonable lines of code to solve this? It would be useful in case anyone was wondering about mutation.....and using it to create a procedure that makes a procedure as its output....

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

Smile简单爱 2024-10-31 16:49:32

好吧,简单地说,所提出的问题是不可能的。原因是,如果可能的话,那么您可以有一个表达式:

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

Well, the problem as posed, to put it shortly, is impossible. The reason is, if it were possible, then you could have an expression:

(make-odd-mapper! add-one)

and this would be a function of a function, created by partial application. However, this function would have to modify its operand, and this can only be done by a macro. Therefore, the result of that function would be a macro, which is impossible, because macros don't exist as values. However, by a slight change in the definition of make-odd-mapper! it is possible to do something slightly different. In this case, you would use it exactly as in the original question, except that instead of saying

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

You would say

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

Here is the code that does it this way:

;;; 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))))))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文