Defining point of functional programming

前端 未结 8 1639
无人及你
无人及你 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").

提交回复
热议问题