在具有越位规则的语言中,所有语言结构都可以是一流的吗?
在类似 LISP 的语言中,所有语言结构都是一等公民。
考虑以下 Dylan:
let x = if (c)
foo();
else
bar();
end;
和 LISP: 中的示例:
(setf x (if c (foo) (bar)))
在 Python 中,您必须编写:
if c:
x = foo();
else:
x = bar();
因为 Python 定义了语句和表达式。
遵守越位规则(具有基于缩进的语法)的语言中的所有语言构造都可以是表达式,以便您可以将它们分配给变量或将它们作为参数传递吗?
In LISP-like languages all language constructs are first-class citizens.
Consider the following example in Dylan:
let x = if (c)
foo();
else
bar();
end;
and in LISP:
(setf x (if c (foo) (bar)))
In Python you would have to write:
if c:
x = foo();
else:
x = bar();
Because Python destinguishes statements and expressions.
Can all language constructs in a language which adheres to the off-side rule (has an indention-based syntax) be expressions, so that you can assign them to variables or pass them as parameters?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Python 具有执行相同操作的以下语法:
Python has the following syntax that performs the same thing:
我在这里没有看到与一流性的关系 - 您没有将
if
语句 传递给函数,而是将其返回的对象传递给函数,该对象完全是一流的python 中的类与 lisp 中的类一样。 然而,就语句/表达式二分法而言,显然这是可能的:例如 Haskell 具有基于缩进的语法,但作为纯函数式语言显然没有语句。我认为 Python 的分离更多地与禁止危险的结构(如“if x=4:”等)有关,而不是与任何语法限制有关。 (尽管我认为这样做得不偿失——有时拥有足够的灵活性来射出你的脚是非常有价值的,即使你确实冒着时不时失去几个脚趾的风险。)
I don't see the relation with first-classness here - you're not passing the
if
statement to the function, but the object it returns, which is as fully first class in python as in lisp. However as far as having a statement/expression dichotomy, clearly it is possible: Haskell for instance has indentation-based syntax, yet as a purely functional language obviously has no statements.I think Python's separation here has more to do with forbidding dangerous constructs like "if x=4:" etc than any syntax limitation. (Though I think it loses more than it gains by this - sometimes having the flexibility sufficient to shoot off your foot is very valuable, even if you do risk losing a few toes now and again.)