函数式编程和非函数式编程 [英] Functional programming and non-functional programming

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

问题描述

在我大学二年级时,我们教"了 Haskell,我对它几乎一无所知,对函数式编程更是一无所知.

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

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

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.

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

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 使用了一种不同的 IO 方法:monads.这些对象包含由解释器的顶层执行的所需 IO 操作.在任何其他级别,它们都只是系统中的对象.

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天全站免登陆