功能编程和非功能编程 [英] Functional programming and non-functional programming

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

问题描述



在函数式编程中,什么是函数式编程,为什么?和/ xor我想在哪里使用它,而不是非函数式编程,我是否正确地认为C是一种非函数式编程语言? 解决方案

功能语言的一个关键特征是一流功能的概念。我们的想法是,您可以将函数作为参数传递给其他函数,并将它们作为值返回。



函数式编程涉及编写不会改变状态的代码。这样做的主要原因是,连续调用函数将产生相同的结果。你可以用任何支持一流函数的语言编写功能代码,但是有一些语言,比如Haskell,它们不允许你改变状态。事实上,你不应该做任何副作用(比如打印文本) - 这听起来像是完全没用。

Haskell改为使用IO的不同方法:monads。这些对象包含所需的IO操作,由解释器的顶层执行。在任何其他级别,它们只是系统中的对象。



函数式编程提供了什么优势?功能性编程允许编写更少的bug,因为每个组件都完全隔离。此外,使用递归和第一类函数允许简单的正确性证明,这通常反映了代码的结构。


In my second year of University we were "taught" Haskell, I know almost nothing about it and even less about functional programming.

What is functional programming, why and/xor where would I want to use it instead of non-functional programming and am I correct in thinking that C is a non-functional programming language?

解决方案

One key feature in a functional language is the concept of first-class functions. The idea is that you can pass functions as parameters to other functions and return them as values.

Functional programming involves writing code that does not change state. The primary reason for doing so is so that successive calls to a function will yield the same result. You can write functional code in any language that supports first-class functions, but there are some languages, like Haskell, which do not allow you to change state. In fact, you're not supposed to make any side effects (like printing out text) at all - which sounds like it could be completely useless.

Haskell instead employs a different approach to IO: monads. These are objects that contain the desired IO operation to be executed by your interpreter's toplevel. At any other level they are simply objects in the system.

What advantages does functional programming provide? Functional programming allows coding with fewer potentials for bugs because each component is completely isolated. Also, using recursion and first-class functions allows for simple proofs of correctness which typically mirror the structure of the code.

这篇关于功能编程和非功能编程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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