在Common Lisp中,为什么(if)语句的多表达式主体需要(progn)? [英] In Common Lisp, why do multi-expression bodies of (if) statements require (progn)?

查看:224
本文介绍了在Common Lisp中,为什么(if)语句的多表达式主体需要(progn)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这仅仅是1950年代遗留下来的历史遗留物吗?还是语法上有某种原因为什么(如果)形式的多表达式主体需要(progn)?为什么不能将多个表达式包装在一组括号中,如(let):

Is this just a bit of historical cruft left over from the 1950s or is there some reason syntactically why multi-expression bodies of (if) forms require (progn)? Why can't you wrap the multiple expressions in a set of parentheses like with (let):

   (if some-cond
     ((exp1) (exp2) (exp3)) ; multi exp "then"
     (exp4)) ; single exp "else"

似乎写一个宏来测试每个主体以首先查看它是否是列表,然后查看它,如果它的第一个元素也是列表(因此不是函数调用),然后再进行测试,这似乎是微不足道的.相应地将其子组件包装在(progn)中.

It appears it would be trivial to write a macro to test each body to see first if it is a list and then if it is, if its first element is also a list (and thus not a function invocation) and then to wrap its subcomponents inside a (progn) accordingly.

推荐答案

在Common Lisp中,此代码:

In Common Lisp, this code:

(if t
    ((lambda (x) (+ x 5)) 10)
   20)

将返回15.根据您的建议,我认为它会看到真实的列表,然后自动将其转换为:

will return 15. With your proposal, I think it would see that the true-clause is a list, and automatically convert it to:

(if t
    (progn (lambda (x) (+ x 5)) 10)
   20)

它将返回10.是吗?

我不确定在CL中区分列表"和函数调用"是否简单".您是否打算使此更改不向后兼容? (新的和有趣的Lisp方言总是很酷,但不是Common Lisp.)或者您可以举一个例子说明您的想法吗?

I'm not sure it's "trivial" to distinguish between "list" and "function invocation" in CL. Do you intend for this change to be non-backwards-compatible? (New and interesting Lisp dialects are always cool, but then it's not Common Lisp.) Or can you give an example of what you have in mind?

这篇关于在Common Lisp中,为什么(if)语句的多表达式主体需要(progn)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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