懒惰的评价问题 [英] Lazy evaluation question

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

问题描述

为什么C对逻辑布尔运算进行惰性求值而不是对b
进行逐位求值?


即:以下程序打印1 2 ,而不是11。在gcc下


main()

{

int a = 1;

int b = 1;

0&& ++ a;

0& ++ b;

printf("%d%d \ n",a,b);

}


当然,按位布尔操作适合做懒惰的

评估吗?


B2003

解决方案

1月5日16:26,Boltar< boltar2 ... @ yahoo.co.ukwrote:


当然是按位布尔值操作是否适合做懒惰的

评估?



那应该读取一些按位操作 - 即左边

手边为零时的AND和XOR。


B2003


Boltar说:


为什么C对逻辑布尔值进行延迟评估操作但不是

按位?



因为逻辑运算旨在确定表达式的真实性或虚假性b / b,所以在任何一种情况下都可以采取适当的措施。


例如,如果表达式是如果它正在下雨而且我要外出,我会

需要一把雨伞 ;一旦我们确定它没有下雨,我们就不需要知道我是否会外出 - 表达的虚假性

已经建立,所以不需要雨伞。


另一个例子:如果你足够高或者能找到一个脚凳,你可以

取下那个平底锅。一旦我们确定你足够高,

我们不需要寻找一个stepstool。


所以它很好感觉C做懒惰的评价&&和||。


但是按位运算符不用于建立真值或虚假。它们是用于计算数学运算结果的
。懒惰的评价

将毫无意义且毫无意义。


-

Richard Heathfield< http://www.cpax。 org.uk>

电子邮件:-http:// www。 + rjh @

谷歌用户:< http://www.cpax.org.uk/prg/writings/googly.php>

Usenet是一个奇怪的放置" - dmr 1999年7月29日


1月5日下午6:26,Boltar< boltar2 ... @ yahoo.co.ukwrote:


为什么C对逻辑布尔运算进行惰性求值而不是按位
按位计算?


即:以下程序打印1 2 ,而不是11。在gcc下


main()

{

int a = 1;

int b = 1;

0&& ++ a;

0& ++ b;

printf("%d%d \ n",a,b);


}

当然,按位布尔操作适合做懒惰的

评估吗?



No.

此外,C中没有懒惰评价的概念。也许在

Haskell或其他一些编程语言但肯定不在C语言中,而不仅仅是b $ b,但是我没有看到懒惰的评价与此有什么关系。


(a&& b)在这个表达式中,b仅在''a''获得后评估

评估它不等于0(或NULL)

(a& b)在这个表达式中,两个操作数被评估,然后&是

适用于他们。


另一个例子是操作员''sizeof''


示例剪辑:

-

#include< stdio.h>


int main(无效){

int i = 0;

printf(" sizeof(int)=%zu \ n",sizeof ++ i);

printf(" ; i =%d \ n,i); / *将打印''i = 0''* /

返回0;

}

-

如您所见,sizeof不会评估它的操作数/表达式。 (我听说

如果你通过''VLA,它可能会对它进行评估;我不确定,我会将b $ b留给常客)


Why does C do lazy evaluation for logical boolean operations but not
bitwise ones?

Ie: the following program prints "1 2" , not "1 1" under gcc

main()
{
int a = 1;
int b = 1;
0 && ++a;
0 & ++b;
printf("%d %d\n",a,b);
}

Surely the bitwise boolean ops are just as appropriate for doing lazy
evaluation with?

B2003

解决方案

On 5 Jan, 16:26, Boltar <boltar2...@yahoo.co.ukwrote:

Surely the bitwise boolean ops are just as appropriate for doing lazy
evaluation with?


That should have read SOME bitwise ops - ie AND and XOR when the left
hand side is zero.

B2003


Boltar said:

Why does C do lazy evaluation for logical boolean operations but not
bitwise ones?

Because logical operations are intended to establish the truth or falsity
of an expression, so that appropriate action can be taken in either case.

For example, if the expression is "if it''s raining and I''m going out, I''ll
need an umbrella", once we''ve established that it''s not raining, we don''t
need to know whether or not I''m going out - the falsity of the expression
is already established, so an umbrella is not required.

Another example: "if you''re tall enough or can find a stepstool, you can
fetch down that saucepan". Once we''ve established that you''re tall enough,
we don''t need to look for a stepstool.

So it makes good sense for C to do lazy evaluation of && and ||.

But bitwise operators are not used to establish truth or falsity. They are
used to calculate the result of a mathematical operation. Lazy evaluation
would be pointless and meaningless.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999


On Jan 5, 6:26 pm, Boltar <boltar2...@yahoo.co.ukwrote:

Why does C do lazy evaluation for logical boolean operations but not
bitwise ones?

Ie: the following program prints "1 2" , not "1 1" under gcc

main()
{
int a = 1;
int b = 1;
0 && ++a;
0 & ++b;
printf("%d %d\n",a,b);

}

Surely the bitwise boolean ops are just as appropriate for doing lazy
evaluation with?

No.
Furthermore, there is no concept of ''lazy evaluation'' in C. Perhaps in
Haskell or some other programming language but certainly not in C, not
only that, but I fail to see what lazy evaluation has to do with this.

(a && b) in this expression, b is evaluated ONLY if after ''a'' gets
evaluated it is not equal to 0 (or NULL)
(a & b) in this expression both operands are evaluated, then & is
applied to them.

Another example would be with the operator ''sizeof''

Example snip:
--
#include <stdio.h>

int main(void) {
int i = 0;
printf("sizeof(int) = %zu\n", sizeof ++i);
printf("i = %d\n", i); /* will print ''i = 0'' */
return 0;
}
--

As you see, sizeof does not evaluate it''s operand/expression. (I heard
it might evaluate it if you ''pass'' a VLA; I am not sure thought, I
will leave that to the regulars)


这篇关于懒惰的评价问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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