Scheme中的参数传递 [英] Parameter Passing in Scheme

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

问题描述

谁能帮我理解Scheme中的各种参数传递模式?我知道 Scheme 实现了按值传递的参数.但是其他模式呢?

Can anyone help me out understanding the various parameter passing modes in Scheme? I know Scheme implements parameter passing by value. But how about other modes?

Scheme 中的参数传递有什么好的文档吗?

Is there any good documentation for parameter passing in Scheme?

推荐答案

Scheme 只有 call-by-value 函数调用.还有其他替代方案可以在该语言中实现,但如果您是初学者,那么此时最好不要尝试它们.如果您正在寻找一种通过引用"传递值的方法——那么一个可以实现的选项是使用宏,但您真的不应该去那里.相反,像 PLT Scheme 这样的一些 Scheme 实现提供了一个框值":这是一种容器,使用方式如下:

Scheme has only call-by-value function calls. There are other alternatives that can be implemented within the language, but if you're a beginner then it's best to not even try them at this point. If you're looking for a way to pass values "by reference" -- then one option that can sort of make it is to use macros, but you really shouldn't go there. Instead, some Scheme implementations like PLT Scheme provide a "box value": this is a kind of a container that is used like this:

  • 您使用 (box <something>)
  • 创建一个包含 的盒子
  • 您使用 (unbox <some-box>)
  • 获取存储在框中的值
  • 您可以使用 (set-box! <some-box> <new-value>)
  • 更改存储在框中的值
  • You create a box holding <something> with (box <something>)
  • You get the value that is stored in a box with (unbox <some-box>)
  • You change the value that is stored in a box with (set-box! <some-box> <new-value>)

鉴于这两个,你可以按值"使用这样的框对象,但它们的内容实际上是一个引用.这与 C 非常相似,其中所有值(实际上是大多数)都是按值传递的,但其中一些值可以是可以改变的指针.顺便说一句,最好避免这些:在 Scheme 中,函数式编程是更常见的选择,因此最好从它开始.

Given these two, you can use such box objects "by value", but their contents is actually a reference. This is very much like C, where all values (most, actually) are passed by-value, yet some of these values can be pointers that you can mutate. BTW, it's best to avoid even these: in Scheme, functional programming is the more common choice and it is therefore better to start with that.

(一旦您更熟练地使用 Scheme,并且如果您使用具有足够抽象的 Scheme,那么您也可以学习如何模仿许多替代方案.)

(Once you are more fluent with Scheme, and if you're using a Scheme with sufficient abstractions, then you can learn how to mimic lots of alternatives too.)

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

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