Defining point of functional programming

前端 未结 8 1636
无人及你
无人及你 2020-12-31 01:17

I can enumerate many features of functional programming, but when my friend asked me Could you define functional programming for me? I couldn\'t.

相关标签:
8条回答
  • 2020-12-31 01:44

    Being able to enumerate the features is more useful than trying to define the term itself, as people will use the term "functional programming" in a variety of contexts with many shades of meaning across a continuum, whereas the individual features have individually crisper definitions that are more universally agreed upon.

    Below are the features that come to mind. Most people use the term "functional programming" to refer to some subset of those features (the most common/important ones being "purity" and "higher-order functions").

    FP features:

    • Purity (a.k.a. immutability, eschewing side-effects, referential transparency)
    • Higher-order functions (e.g. pass a function as a parameter, return it as a result, define anonymous function on the fly as a lambda expression)
    • Laziness (a.k.a. non-strict evaluation, most useful/usable when coupled with purity)
    • Algebraic data types and pattern matching
    • Closures
    • Currying / partial application
    • Parametric polymorphism (a.k.a. generics)
    • Recursion (more prominent as a result of purity)
    • Programming with expressions rather than statements (again, from purity)
    • ...

    The more features from the above list you are using, the more likely someone will label what you are doing "functional programming" (and the first two features--purity and higher-order functions--are probably worth the most extra bonus points towards your "FP score").

    0 讨论(0)
  • 2020-12-31 01:48

    I would say that the defining point of pure functional programming is that all computation is done in functions with no side effects. That is, functions take inputs and return values, but do not change any hidden state, In this paradigm, functions more closely model their mathematical cousins.

    This was nailed down for me when I started playing with Erlang, a language with a write-once stack. However, it should be clarified that there is a difference between a programming paradigm, and a programming language. Languages that are generally referred to as functional provide a number of features that encourage or enforce the functional paradigm (e.g., Erlang with it's write-once stack, higher order functions, closures, etc.). However the functional programming paradigm can be applied in many languages (with varying degrees of pain).

    0 讨论(0)
提交回复
热议问题