SICP/Scheme中的apply函数 [英] The apply function in SICP/Scheme

查看:28
本文介绍了SICP/Scheme中的apply函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里问了几个关于 Scheme/SICP 的问题,而且答案经常涉及使用 apply 过程,我在 SICP 和本书的索引中都没有看到它只列出了一次,结果是一个脚注.

一些用法的例子基本上是这个问题的每一个答案:Going从 Curry-0, 1, 2, 到 ...n.

我对 apply 的工作原理很感兴趣,我想知道是否有一些示例可用.怎么可能把apply过程改写成另一个函数,比如像这样改写map?

#lang sicp(定义(映射函数序列)(如果(空?序列)零(cons (func (car sequence)) (map func (cdr sequence)))))

它似乎只是使用第一个参数进行函数调用?类似的东西:

(应用列表 '(1 2 3 4 5)) ;-->(列表 1 2 3 4 5)(应用 + '(1 2 3)) ;-->(+ 1 2 3)

那么在 Python 中可能有类似的东西吗?

<预><代码>>>>参数=[1,2,3]>>>功能=最大">>>getattr(__builtins__, func)(*args)3

解决方案

您要求查看如何对 apply 进行编码,而不是如何使用它.

可以编码为

#lang sicp;(define (appl f xs) ; #lang球拍;(评估;(cons f (map (lambda (x) (list 'quote x)) xs))))(define (appl f xs) ; #lang r5rs, sicp(评估(cons f (map (lambda (x) (list 'quote x))xs))(空环境 5)))

#lang sicp 下的 Racket 中试用:

>(显示(应用列表'(1 2 3 4 5)))(1 2 3 4 5)>(显示(列表 1 2 3 4 5 ))(1 2 3 4 5)>(应用程序 + (列表 (+ 1 2) 3))6>( + (+ 1 2) 3 )6>(显示(应用程序地图(缺点列表'((1 2 3)(10 20 30)))))((1 10) (2 20) (3 30))>(显示(地图列表'(1 2 3)'(10 20 30)))((1 10) (2 20) (3 30))

这是关于 eval.

它需要一个 environment 作为第二个参数,所以我们为它提供了 (null-environment 5) ,它只返回一个空的环境,看起来像它.我们这里实际上不需要任何环境,因为此时已经完成了对参数的评估.

I've asked a few questions here about Scheme/SICP, and quite frequently the answers involve using the apply procedure, which I haven't seen in SICP, and in the book's Index, it only lists it one time, and it turns out to be a footnote.

Some examples of usage are basically every answer to this question: Going from Curry-0, 1, 2, to ...n.

I am interested in how apply works, and I wonder if some examples are available. How could the apply procedure be re-written into another function, such as rewriting map like this?

#lang sicp

(define (map func sequence)
    (if (null? sequence) nil
        (cons (func (car sequence)) (map func (cdr sequence)))))

It seems maybe it just does a function call with the first argument? Something like:

(apply list '(1 2 3 4 5)) ; --> (list 1 2 3 4 5)
(apply + '(1 2 3))        ; --> (+ 1 2 3)

So maybe something similar to this in Python?

>>> args=[1,2,3]
>>> func='max'
>>> getattr(__builtins__, func)(*args)
3

解决方案

You asked to see how apply could be coded, not how it can be used.

It can be coded as

#lang sicp

; (define (appl f xs)    ; #lang racket
;   (eval 
;     (cons f (map (lambda (x) (list 'quote x)) xs))))

(define (appl f xs)      ; #lang r5rs, sicp
  (eval
    (cons f (map (lambda (x) (list 'quote x)) 
                 xs))
    (null-environment 5)))

Trying it out in Racket under #lang sicp:

> (display (appl list '(1 2 3 4 5)))
(1 2 3 4 5)

> (display (     list   1 2 3 4 5 ))
(1 2 3 4 5)

> (appl + (list (+ 1 2) 3))
6

> (     +       (+ 1 2) 3 )
6

> (display (appl map (cons list '((1 2 3)  (10 20 30)))))
((1 10) (2 20) (3 30))

> (display (     map       list  '(1 2 3) '(10 20 30)  ))
((1 10) (2 20) (3 30))

Here's the link to the docs about eval.

It requires an environment as the second argument, so we supply it with (null-environment 5) which just returns an empty environment, it looks like it. We don't actually need any environment here, as the evaluation of the arguments has already been done at that point.

这篇关于SICP/Scheme中的apply函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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