In computer science, functional programming is a programming paradigm, a style of building the structure and elements of computer programs, that treats computation as the evaluation of mathematical functions and avoids state and mutable data. It is a declarative programming paradigm, which means programming is done with expressions. In functional code, the output value of a function depends only on the arguments that are input to the function, so calling a function f twice with the same value for an argument x will produce the same result f(x) both times. Eliminating side effects, i.e. changes in state that do not depend on the function inputs, can make it much easier to understand and predict the behavior of a program, which is one of the key motivations for the development of functional programming.
Functional programming has its roots in lambda calculus, a formal system developed in the 1930s to investigate computability, the Entscheidungsproblem, function definition, function application, and recursion. Many functional programming languages can be viewed as elaborations on the lambda calculus. In the other well known declarative programming paradigm, logic programming, relations are at the base of respective languages.
In contrast, imperative programming changes state with commands in the source language, the most simple example is the assignment. Functions do exist, not in the mathematical sense, but in the sense of subroutine. They can have side effects that may change the value of program state. Functions without return value therefore make sense. Because of this, they lack referential transparency, i.e. the same language expression can result in different values at different times depending on the state of the executing program.

C# 3.0 and Java 8 added constructs to facilitate the functional style.

Functional Programming, as far as I understand it, seeks to describe what you want done rather than specify how you want something done. It's probably best understood in contrast to imperative programming. For instance, if you have a list in C and you want to pull out every Nth element, you have to point at the first element, set a counter at one, move to the next element, increment the counter, check to see if you're at the Nth element and so on. The functional equivalent would be to write a function that recognizes when the size of a list is a multiple of N, and then pass that function to the list, possibly with another snippet of code to hand back the head of the list if your N-recognizer evaluates to true and discarding it if it evaluates to false. The two functions recurse through the list, and finally hand back a list consisting of every Nth element.

The latter method might seem like the more confusing way to go about things, and that's because it is. Functional programming can be a mind-bender, which is one reason why Lisp, Scheme, and Haskell have never really surpassed C, C++, Java and COBOL in commercial popularity. But there are benefits to the functional way. For one, if you can get the logic correct, functional programming requires orders of magnitude less code than imperative programming. That means fewer points of failure, less code to test, and a more productive (and, many would say, happier) programming life. As systems get bigger, this has become more and more important.

Are there more exotic types? Not yet. There are hybrids between the two of them (like Scala), but these merely seek to leverage the strengths of both types. Then there's Object-oriented programming, which is really just a new way to organize data in an imperative program. And even with strange new technologies like quantum computing, the (planned-for) underlying languages fall somewhere in the declarative/imperative spectrum.

Functional programming的更多相关文章

  1. Beginning Scala study note(4) Functional Programming in Scala

    1. Functional programming treats computation as the evaluation of mathematical and avoids state and ...

  2. Functional Programming without Lambda - Part 2 Lifting, Functor, Monad

    Lifting Now, let's review map from another perspective. map :: (T -> R) -> [T] -> [R] accep ...

  3. Functional Programming without Lambda - Part 1 Functional Composition

    Functions in Java Prior to the introduction of Lambda Expressions feature in version 8, Java had lon ...

  4. a primary example for Functional programming in javascript

    background In pursuit of a real-world application, let’s say we need an e-commerce web applicationfo ...

  5. Java 中的函数式编程(Functional Programming):Lambda 初识

    Java 8 发布带来的一个主要特性就是对函数式编程的支持. 而 Lambda 表达式就是一个新的并且很重要的一个概念. 它提供了一个简单并且很简洁的编码方式. 首先从几个简单的 Lambda 表达式 ...

  6. Functional programming idiom

    A functional programming function is like a mathematical function, which produces an output that typ ...

  7. 关于函数式编程(Functional Programming)

    初学函数式编程,相信很多程序员兄弟们对于这个名字熟悉又陌生.函数,对于程序员来说并不陌生,编程对于程序员来说也并不陌生,但是函数式编程语言(Functional Programming languag ...

  8. Functional Programming 资料收集

    书籍: Functional Programming for Java Developers SICP(Structure and Interpretation of Computer Program ...

  9. BETTER SUPPORT FOR FUNCTIONAL PROGRAMMING IN ANGULAR 2

    In this blog post I will talk about the changes coming in Angular 2 that will improve its support fo ...

随机推荐

  1. Java for LeetCode 200 Number of Islands

    Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surro ...

  2. 【JAVA、C++】LeetCode 019 Remove Nth Node From End of List

    Given a linked list, remove the nth node from the end of list and return its head. For example, Give ...

  3. codeforces B.Maximum Absurdity 解题报告

    题目链接:http://codeforces.com/contest/332/problem/B 题意:在一个序列中,在所有长度为k的区间里找出两个不重叠的最大和,输出这两个最大和所对应的开头的位置a ...

  4. July 27th, Week 31st Wednesday, 2016

    Don't let yesterday take up too much of today. 别让昨天的事情占据今天太多时间. Learn from yesterday, but don't let ...

  5. Hadoop学习:

    文件名称 格式 描述 hadoop-env.sh BaSh 脚本 记 录 脚 本 要 用 的 环 境 变 , 以 运 行Hadoop core-site.xml HadooP配XML Hadoop C ...

  6. Linux底下的第一个C程序

    首先保证你的Linux底下安装了GCC,假如没有安装GCC的话请参考: http://www.cnblogs.com/aspirant/p/3544398.html 假如输入 gcc -v 不再是co ...

  7. 在iOS开发中,给项目添加新的.framework

    首先需要了解一下iOS中静态库和动态库.framework的概念 静态库与动态库的区别 首先来看什么是库,库(Library)说白了就是一段编译好的二进制代码,加上头文件就可以供别人使用. 什么时候我 ...

  8. PHP“Cannot use object of type stdClass as array” (php在调用json_decode从字符串对象生成json对象时的报错)

    php再调用json_decode从字符串对象生成json对象时,如果使用[]操作符取数据,会得到下面的错误 错误:Cannot use object of type stdClass as arra ...

  9. wp8 json2csharp

    string jsonData = "{\"result\":\"600\",\"data\":{\"items\&qu ...

  10. C调Lua

    转自:http://foredoomed.org/blog/2013/12/07/integrate-c-with-lua/ 我们在用C写程序的时候,很多情况下需要用到List,Map等集合,但是C是 ...