您能使用过程式、函数式、逻辑式和面向对象编程语言来表示相同的示例吗?
任何人都可以为我提供一个示例,可以帮助我通过使用几乎相同的示例问题并排理解过程、函数、逻辑和面向对象的编程模型。
请给我使用过程、函数、逻辑和面向对象编程语言的相同问题的示例代码片段。
Can anyone please provide me with an example that can help me to understand Procedural, functional, Logic and Object Oriented programming models side by side by using nearly same example-problem.
Please give me example code-snippets of the somewhat same problem using Procedural, Functional, Logic and OO programming languages.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
让我们尝试更简单的例子 - 只计算第 n 个斐波那契数。
首先,过程式(Pascal):
这个例子展示了过程式语言的特征:
第二,面向对象(在Python中):
实际上这个问题不值得创建一个类,所以我添加了缓存已经计算出的结果。
这个例子显示:
不是已显示,但我们可以例如从返回某个序列的第 n 个成员的抽象类派生该类。通过子类化,我们得到定义斐波那契数列的类,序列 1,2,3...,序列 1,4,9,16,... 等。
第三,函数式风格 (Haskell):
函数式编程范式的以下特征是演示:
但函数式语言的主要特征是函数是第一类对象。
这可以通过
fib
的其他实现来证明:这里我们将
fibs
函数作为参数传递给zipWith
函数。此示例还演示了惰性求值:仅在其他函数需要时才计算“无限”列表。
顺便说一句,函数式并不一定意味着不是面向对象的。 Scala 是函数式和面向对象编程语言的一个例子。
Prolog:
可以看到逻辑编程风格的以下特征:
该程序还可用于找出斐波那契数 8 位于序列中的第 6 位:
Let's try simpler example - just calculating n-th Fibonacci number.
First, procedural (in Pascal):
This example shows features of procedural languages:
Second, object oriented (in Python):
Actually the problem is not worth creating a class, so I added caching of already calculated results.
This example shows:
Not shown but we can e.g. descend this class from abstract class returning n-th member of some sequence. By subslassing we get class defining Fibonacci sequence, sequence 1,2,3..., sequence 1,4,9,16,... etc.
Third, in functional style (Haskell):
Following features of a functional programming paradigm are demonstrated:
But the main feature of functional languages is that functions are first class objects.
This can be demonstrated by other implementation of
fib
:Here we are passing
fibs
function as parameter tozipWith
function.This example also demonstrates lazy evaluation: "infinite" list is computed only to extent it is needed for other functions.
By the way, functional does not necessary mean not object oriented. An example of programming language that is both functional and object oriented is Scala.
Prolog:
Following features of logic programming style can be seen:
This program could also be used to find out that Fibonacci number 8 is at 6th position in the sequence:
http://99-bottles-of-beer.net/
(它具有我自己的可怕的设计99 语言。)
http://99-bottles-of-beer.net/
(It features my own horribly contrived 99 language.)
欧拉项目问题 2:http://projecteuler.net/problem=2
Haskell(功能/逻辑) :
Python(OO):
C(过程/命令):
编写的非常低效的版本
这是用 MIT Scheme Prolog : 摘自此处,由 13tazer31 发布
Project Euler Problem number 2: http://projecteuler.net/problem=2
Haskell (functional/logic):
Python (OO):
C (procedural/imperative):
And here is a very inefficient version written in MIT Scheme
Prolog: Take from this here, posted by 13tazer31
嗯,它非常简单的
假设你想计算一些东西(这就是计算机所做的)
现在,如果您查看问题,您会发现它分为函数..但是必须有一个起点(从您开始计算的位置)和终点(您结束计算的位置),并且它们之间是函数(f1 ,f2,f3)。
因此,您真正所做的不是编写一个大函数(F)来完成所有事情并且很长,而是将其分解为更小的部分(这称为重构)。理解一个 150 行长的函数很无聊。当你读到行尾时,你会忘记你是从哪里开始的,因此我们将它们分开。
现在我们如何进行计算 --->
我们创建一个函数,例如compute()(这称为facade),它将按所需顺序调用其余函数(f1,f2,f3 ...)并返回结果。
现在明白了这一点:
如果我们编写一个函数,该函数大约需要 150 行(复杂性会增加)。
假设我们将其分解为 3 个函数,并假设每个函数有 50 行(可管理).. 由于 3 个函数的行数之和仍然是 150 行,我们如何降低复杂性:D 。函数名称降低了复杂性..它清楚地说明了函数的作用..这意味着查看名称您可以了解函数的作用。
OO编程逻辑:
现在功能分散在功能逻辑中..当我们将所有相关功能(行为)放在一个伞(类)中时,我们进一步降低了复杂性..如何..通过“类名称”。现在您可以说,我们不调用 f1,f2,f3..,而是调用 c1.f1(),c2.f2,c3.f3(),其中“c”表示类(域驱动设计)。
imp .. 无论您使用oops还是函数逻辑,总有一个计算的起点和终点...记住我谈到的compute()..问题是谁调用它..答案是你。一切OOP的逻辑或者过程逻辑都隐藏在它的背后(Service Facade)。
Well its very simple
lets say you want to compute something (that's what computers do)
now if you look at the problem , you see it divided into functions .. but there has to be a starting point (from where you start the computation) and ending point (where you end the computation) and between them are the functions (f1,f2,f3).
Thus what you have really done is instead of writing one big function (F) that does every thing and is very long you have break it into smaller parts (this is known as refactoring). Understanding a single function that is 150 line long is boring.. when you get to the end of line you will forget where you started thus we break things apart.
now how we do the computation --- >
We create a single function say compute() (this is known as facade ) that will call the remaining functions (f1,f2,f3...) in desired ordered and will return the result.
Now understand this:
If we had written a single function that would have been around 150 lines (complexity increases)..
assume we broke down it into 3 functions and say each function is of 50 lines (manageable) .. how did we reduce complexity since the sum of lines of 3 functions is still 150 :D . the complexity is reduced by function name .. which clearly states what the function does.. it means looking at the name you can have a idea what the function does.
OO Programming logic:
Now functions are scattered in functional logic .. when we bring all the related functions (behavior) inside a single umbrella (Class) we have further reduced the complexity .. how .. by "Class name". Now you can say that instead of calling f1,f2,f3.. we call c1.f1(),c2.f2,c3.f3() where "c" denotes a class (domain driven design).
imp .. no matter whether you use oops or functional logic there is always a starting and ending point of computation ... remember the compute() i talked about.. and the question is who call's it .. and the answer is you .. Everything OOP's logic or procedural logic is hidden behind it (Service Facade)