关于一、二、三等价值 [英] About first-,second- and third-class value

查看:52
本文介绍了关于一、二、三等价值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一流价值可以

  1. 作为参数传递
  2. 从子程序返回
  3. 赋值给一个变量.

Second-class 值只能作为参数传递.

Second-class value just can be passed as an argument.

第三类值甚至不能作为参数传递.

Third-class value even can't be passed as an argument.

为什么要这样定义这些东西?据我了解,可以作为参数传递"意味着它可以被推送到运行时堆栈中;可以分配到一个变量中"意味着它可以移动到内存的不同位置;可以从子程序返回"几乎与可以分配给变量"具有相同的含义,因为返回值总是放在已知地址中,因此第一类值完全是可移动的"或动态的",第二类值是可移动"的一半,而第三类值只是静态",例如 C/C++ 中的标签只能通过 goto 语句寻址,除了goto"之外,您不能对该地址做任何事情.理解有意义吗?或者这三种值到底是什么意思?

Why should these things defined like that? As I understand, "can be passed as an argument" means it can be pushed into the runtime stack;"can be assigned into a variable" means it can be moved into a different location of the memory; "can be returned from a subroutine" almost has the same meaning of "can be assigned into a variable" since the returned value always be put into a known address, so first class value is totally "movable" or "dynamic",second class value is half "movable" , and third class value is just "static", such as labels in C/C++ which just can be addressed by goto statement, and you can't do nothing with that address except "goto" .Does My understanding make any sense? or what do these three kinds of values mean exactly?

推荐答案

哦不,我可能不得不再次去编辑维基百科.

Oh no, I may have to go edit Wikipedia again.

实际上只有两个区别值得一提:一流和非一流.如果迈克尔斯科特谈论第三任何,我会非常沮丧.

There are really only two distinctions worth making: first-class and not first-class. If Michael Scott talks about a third-class anything, I'll be very depressed.

好的,那么什么是一流"?嗯,这是一个几乎没有技术意义的术语.意义,当出现时,通常是比较性的,它适用于比同类事物具有更多特权的语言中的事物(我在这里故意含糊不清).这就是人们的全部意思.

Ok, so what is "first-class," anyway? Well, it is a term that barely has a technical meaning. The meaning, when present, is usually comparative, and it applies to a thing in a language (I'm being deliberately vague here) that has more privileges than a comparable thing. That's all people mean by it.

让我们看一些例子:

  • C 中的函数指针是一等值,因为它们可以传递给函数、从函数返回以及存储在堆分配的数据结构中,就像任何其他值一样.Pascal 和 Ada 中的函数不是一等值,因为尽管它们可以作为参数传递,但它们不能作为结果返回或存储在堆分配的数据结构中.

  • Function pointers in C are first-class values because they can be passed to functions, returned from functions, and stored in heap-allocated data structures just like any other value. Functions in Pascal and Ada are not first-class values because although they can be passed as arguments, they cannot be returned as results or stored in heap-allocated data structures.

结构类型是 C 中的二等类型,因为没有结构类型的文字表达式.(从 C99 开始,有带有命名字段的文字 初始化器,但这仍然不如在任何可以使用表达式的地方使用文字那么通用.)

Struct types are second-class types in C, because there are no literal expressions of struct type. (Since C99 there are literal initializers with named fields, but this is still not as general as having a literal anywhere you can use an expression.)

多态值是机器学习中的二等值,因为尽管它们可以绑定到名称,但它们不能绑定到 lambda.因此它们不能作为参数传递.但是在 Haskell 中,因为 Haskell 支持更高阶的多态性,所以多态值是一流的.(它们甚至可以存储在数据结构中!)

Polymorphic values are second-class values in ML because although they can be let-bound to names, they cannot be lambda-bound. Therefore they cannot be passed as arguments. But in Haskell, because Haskell supports higher-rank polymorphism, polymorphic values are first-class. (They can even be stored in data structures!)

在 Java 中,类型 int 是第二类,因为您不能从它继承.类型 Integer 是第一类.

In Java, the type int is second class because you can't inherit from it. Type Integer is first class.

在 C 中,标签是第二类,因为它们没有值,您无法用它们进行计算.在 FORTRAN 中,行号有值,所以是第一类.有一个 GNU 对 C 的扩展,它允许您定义一流的标签,它非常有用.在这种情况下,一流是什么意思?这意味着标签有值,可以存储在数据结构中,并且可以在 goto 中使用.但这些值在另一种意义上是第二类,因为来自一个过程的标签不能有意义地用于属于另一个过程的 goto.

In C, labels are second class, because they don't have values and you can't compute with them. In FORTRAN, line numbers have values and so are first class. There is a GNU extension to C that allows you to define first-class labels, and it is jolly useful. What does first-class mean in this case? It means the labels have values, can be stored in data structures, and can be used in goto. But those values are second class in another sense, because a label from one procedure can't meaningfully be used in a goto that belongs to another procedure.

我们知道这个术语有多么无用吗?

Are we getting an idea how useless this terminology is?

我希望这些例子能让您相信一流"的想法在整体考虑编程语言时并不是一个非常有用的想法.当您谈论特定语言或语言家族的特定功能时,它可能是一种有用的简写(除非具有一流的嵌套函数,否则语言不是函数式的")但是总的来说,你最好直接说出你的意思,而不是谈论一流"或非一流"的事情.

I hope these examples convince you that the idea of "first-class" is not a very useful idea in thinking about programming languages overall. When you're talking about a particular feature of a particular language or language family, it can be a useful shorthand ("a language isn't functional unless it has first-class, nested functions") but by and large you're better off saying just what you mean instead of talking about "first-class" or "not first-class" things.

至于三等",直接说不.

As for "third class", just say no.

这篇关于关于一、二、三等价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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