按名称拨打电话的示例 [英] Example of Call by name

查看:89
本文介绍了按名称拨打电话的示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的编程课堂原则中,我们谈论的是不同的调用方法.我们讨论了一些:

In my principles of programming class we are talking about different calling methods. Some we discussed were:

  • 按价值致电
  • 通过引用致电
  • 按价值/结果致电
  • 按姓名呼叫
  • call by value
  • call by reference
  • call by value/result
  • and call by name

我找不到有关按名称呼叫的工作方式的示例.有人在乎给我举个例子吗?我认为,当您将xml文件作为输入时,按名称调用相似.有人可以给我一个更传统的例子吗?

I can't find an example of how call by name works. Anyone care to give me an example? I think that when you take an xml file as input this is similar to call by name. Could someone give me a more traditional example?

推荐答案

我将使用一种假设的编程语言.假设我们有一个函数p(x),它打印出x然后返回它.现在让我们定义一个函数:

I'll work in a hypothetical programming language. Let's assume we have a function p(x) that prints out x and then returns it. Now let's define a function:

function foo(x, y) { return y+1; }

现在让我们用一些参数来调用它:

Now let's call it with some arguments:

foo(p(123),p(456))

xy将替换为参数,因此对上述foo的调用将导致:

x and y are going to be substituted for the parameters, so the call to foo above is going to result in:

return p(456)+1;

因此,我们将在屏幕上打印456并返回457.在另一种评估策略中,我们将首先评估功能参数(在此过程中将123和456打印到屏幕上),然后用456代替y在函数主体中,最终返回457.

So we're going to print 456 to the screen and return 457. In another evaluation strategy, we would evaluate the function parameters first (printing 123 and 456 to the screen in the process) and then substitute 456 for y in the function body, eventually returning 457.

它是人为的,但我希望您能理解.都是关于替换的.

It's contrived, but I hope you get the idea. It's all about substitution.

这篇关于按名称拨打电话的示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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