过程式编程和函数式编程有什么区别? [英] What is the difference between procedural programming and functional programming?

查看:163
本文介绍了过程式编程和函数式编程有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已阅读程序性编程函数式编程,但我仍然有点困惑。有人可以将它归结为核心吗?

解决方案

功能语言(理想情况下)允许您编写数学函数,函数需要 n 参数并返回一个值。如果程序执行完毕,这个函数会根据需要进行逻辑评估。



另一方面,过程语言执行一系列顺序步骤。 (可以将顺序逻辑转换为功能逻辑,称为延续传球风格)。



因此,一个纯函数式程序总是会为输入产生相同的值,而评估的顺序并不明确;这意味着像用户输入或随机值这样的不确定值很难在纯粹的函数式语言中进行建模。




1 在这个答案中的其他所有内容,这是一个泛化。当计算结果被需要而不是顺序地被称为懒惰时,评估计算的特性被称为懒惰,并且并非所有的函数式语言实际上都是普遍懒惰的,懒惰也不局限于函数式编程。相反,这里给出的描述提供了一个思维框架来思考不同的编程风格,这些编程风格并不是截然不同的类别,而是流畅的想法。


I've read the Wikipedia articles for both procedural programming and functional programming, but I'm still slightly confused. Could someone boil it down to the core?

解决方案

A functional language (ideally) allows you to write a mathematical function, i.e. a function that takes n arguments and returns a value. If the program is executed, this function is logically evaluated as needed.1

A procedural language, on the other hand, performs a series of sequential steps. (There's a way of transforming sequential logic into functional logic called continuation passing style.)

As a consequence, a purely functional program always yields the same value for an input, and the order of evaluation is not well-defined; which means that uncertain values like user input or random values are hard to model in purely functional languages.


1 As everything else in this answer, that’s a generalisation. The property of evaluating a computation when its result is needed rather than sequentially where it’s called is known as "laziness", and not all functional languages are actually universally lazy, nor is laziness restricted to functional programming. Rather, the description given here provides a "mental framework" to think about different programming styles that are not distinct and opposite categories but rather fluid ideas.

这篇关于过程式编程和函数式编程有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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