您可以在没有if语句的情况下编写任何算法吗? [英] Can you write any algorithm without an if statement?

查看:102
本文介绍了您可以在没有if语句的情况下编写任何算法吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个网站让我很幽默- http://www.antiifcampaign.com/但可以多态在每种情况下都可以使用if语句的情况下工作?

This site tickled my sense of humour - http://www.antiifcampaign.com/ but can polymorphism work in every case where you would use an if statement?

推荐答案

Smalltalk被认为是真正的"面向对象的语言,没有"if"语句,也没有"for"语句,没有"while" "陈述.还有其他示例(例如Haskell),但这是一个很好的例子.

Smalltalk, which is considered as a "truly" object oriented language, has no "if" statement, and it has no "for" statement, no "while" statement. There are other examples (like Haskell) but this is a good one.

引用 Smalltalk没有"if"语句:

有些观众可能在想 这是证实他们的证据 怀疑Smalltalk很奇怪, 但是我要告诉你的是 这个:

Some of the audience may be thinking that this is evidence confirming their suspicions that Smalltalk is weird, but what I’m going to tell you is this:

"if"语句是面向对象语言中的可憎之物.

An "if" statement is an abomination in an Object Oriented language.

为什么?好吧,OO语言是由 类,对象和方法,以及 如果"陈述不可避免地没有 那些.您不能在 OO方式.它不应该存在. 有条件的执行,就像一切 否则,应该是一种方法.一种方法 什么?布尔值.

Why? Well, an OO language is composed of classes, objects and methods, and an "if" statement is inescapably none of those. You can’t write "if" in an OO way. It shouldn’t exist. Conditional execution, like everything else, should be a method. A method of what? Boolean.

现在,很有趣的是,在Smalltalk中, 布尔有一个称为 ifTrue:ifFalse:(该名称看起来 现在很奇怪,但可以跳过 现在).它是布尔型的抽象形式,但是 布尔值有两个子类:True和 错误的.该方法传递两个块 代码.实际上,该方法只是 为真实情况运行代码.在 错误,它将运行错误的代码 案子.这是一个希望的例子 解释:

Now, funnily enough, in Smalltalk, Boolean has a method called ifTrue:ifFalse: (that name will look pretty odd now, but pass over it for now). It’s abstract in Boolean, but Boolean has two subclasses: True and False. The method is passed two blocks of code. In True, the method simply runs the code for the true case. In False, it runs the code for the false case. Here’s an example that hopefully explains:

(x >= 0) ifTrue: [
'Positive'
] ifFalse: [
'Negative'
]

您应该能够看到ifTrue:和 ifFalse:在那儿.不用担心 他们不在一起.

You should be able to see ifTrue: and ifFalse: in there. Don’t worry that they’re not together.

表达式(x> = 0)的计算结果为 对或错.说的是真的,那么我们 有:

The expression (x >= 0) evaluates to true or false. Say it’s true, then we have:

true ifTrue: [
'Positive'
] ifFalse: [
'Negative'
]

我希望这很明显 会产生正数".

I hope that it’s fairly obvious that that will produce ‘Positive’.

如果它是假的,我们将:

If it was false, we’d have:

false ifTrue: [
'Positive'
] ifFalse: [
'Negative'
]

产生负数".

好的,就是这样.怎么了 很好吗?好吧,还有什么 语言你能做到吗?更多的 认真地,答案是那里 在这方面没有什么特殊情况 语言.一切都可以在一个 OO方式,一切都在 面向对象的方式.

OK, that’s how it’s done. What’s so great about it? Well, in what other language can you do this? More seriously, the answer is that there aren’t any special cases in this language. Everything can be done in an OO way, and everything is done in an OO way.

我绝对建议阅读全文,并代码也是来自同一作者的对象.

I definitely recommend reading the whole post and Code is an object from the same author as well.

这篇关于您可以在没有if语句的情况下编写任何算法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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