Defining point of functional programming

前端 未结 8 1769
无人及你
无人及你 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:29

    A lot of the definitions so far have emphasized purity, but there are many languages that are considered functional that are not at all pure (e.g., ML, Scheme). I think the key properties that make a language "functional" are:

    1. Higher-order functions. Functions are a built-in datatype no different from integers and booleans. Anonymous functions are easy to create and idiomatic (e.g., lambdas).
    2. Everything is an expression. In imperative languages, a distinction is made between statements, which mutate state and affect control flow, and expressions, which yield values. In functional languages (even impure functional languages), expression evaluation is the fundamental unit of execution.

    Given these two properties, you naturally get the behavior we think of as functional (e.g., expressing computations in terms of folds and maps). Eliminating mutable state is a way to make things even more functional.

提交回复
热议问题