以下如何减少? [英] How does the following reduce?

查看:16
本文介绍了以下如何减少?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下过程来添加三个数字:

I have the following procedure to add three numbers:

(define add-3 (lambda (x y z) (+ x y z)))
(add-3 100 200 300)
; 600

当将其转换为柯里化函数时,它会简化为如下所示,

When converting it to a curried function, it reduces to something like the following,

(apply (lambda args1
         (apply add-3 (cons 100 args1)))
       '(200 300))
; 600

我的问题是上述情况如何进一步减少?例如,如果我在列表中替换 args1=(200 300) 我得到以下内容:

My question is how does the above further reduce? For example, if I substitute in the list args1=(200 300) I get the following:

(apply (lambda args1
         (apply add-3 (cons 100 args1)))
       '(200 300))

(apply
 (apply add-3 (cons 100 '(200 300))))

但这给了我 ar arity mismatch,所以我一定在减少/替换中做错了什么.这样做的正确方法是什么?

But that gives me ar arity mismatch, so I must be doing something wrong in the reduction/substitution. What would be the proper way to do that?

我想一个更直接的提问方式是下面的简化是如何发生的?

I suppose a more direct way to ask this is how does the following simplification occur?

(apply (lambda args (apply add-3 args)) '(100 200 300))
; 600

; ... how do we get here?
(apply add-3 '(100 200 300))
; 600


上下文:https://stackoverflow.com/a/68038585/651174.

推荐答案

以此为基准重新开始:

(define add-3 (lambda (x y z) (+ x y z)))
(add-3 100 200 300)
; 600

然后我们从我们想要减少的函数开始:

And then we start with the function we want to reduce:

(apply (lambda args1
         (apply add-3 (cons 100 args1)))
       '(200 300))

我们如何减少这种情况?apply 的工作原理如下:

How do we reduce this? The apply works as follows:

(apply + '(1 2))
(      +   1 2 )
3

也就是说,它需要一个过程并将其应用于提供的列表中的参数.因此,首先,我们可以采用内部 lambda 函数"并应用它:

That is, it takes a procedure and applies that to the arguments from the list supplied. So, to start off, we can take the 'inner lambda function' and apply that:

((lambda args1 (apply add-3 (cons 100 args1))) 200 300)

这不是什么简化,但我们可以设置 args1='(200 300) 来去掉那个组件,这给了我们:

This isn't much of a simplification, but we can set args1='(200 300) to get rid of that component, which gives us:

(let ((args1 '(200 300)))
  (apply add-3 (cons 100 args1)))

我们可以在这里走几条路线,但为了简化事情,让我们做 (cons 100 args1) 调用,它简化为 (100 200 300):

We can go a few routes here, but to simplify things, let's do the (cons 100 args1) call, which simplifies to (100 200 300):

(let ((args1 '(200 300)))
  (apply add-3 '(100 200 300)))

注意我们不再有 args1 在正文中,所以我们可以删除 let:

Notice we no longer have args1 in the body, so we can remove the let:

(apply add-3 '(100 200 300))

现在我们可以使用(apply func args)的应用定义->(func arg1 arg2 ...) 得到:

And now we can use the apply definition of (apply func args) -> (func arg1 arg2 ...) to get:

(add-3 100 200 300)

这就是我们想要的.请注意,在我们的缩减中,我们能够利用以下优势:

Which is what we wanted. Notice that in our reduction we were able to take advantage of the following:

(apply (lambda args BODY) LIST)

并将其转换为:

(let ((args LIST))
    BODY)

这里最简单的例子是:

(apply (lambda args (length args)) '(1 2 3 4))
;  {
;    BODY = (length args)
;    LIST = '(1 2 3 4)
;  }
;  ==>
(let ((args '(1 2 3 4)))
    (length args))

有关更多信息,此答案提供了额外的上下文:https://stackoverflow.com/a/68048663/651174.

For more information, this answer provides additional context: https://stackoverflow.com/a/68048663/651174.

这篇关于以下如何减少?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆