什么是“Pure Function”

下列文字摘自The Definition of “Pure Function”

A pure function is a function that depends only on its declared inputs and its internal algorithm to produce its output. It does not read any other values from “the outside world” — the world outside of the function’s scope — and it does not modify any values in the outside world.

1-Pure-Function-Equation

什么是“lambda”

下列文字摘自What is This “Lambda” You Speak Of?

lambda means “anonymous function,” and calculus means “a formal system”

Therefore, the term lambda calculus refers to “a formal way to think about functions.”

That same Wikipedia link states this:

“Lambda calculus provides a theoretical framework for describing functions and their evaluation. Although it is a mathematical abstraction rather than a programming language, it forms the basis of almost all functional programming languages today.”

When I first started learning about functional programming, I found these terms to be a little intimidating, but as with most FP terms, they’re just uncommon words for talking about “functions and their evaluation.”

什么是“functional programming”

下列文字摘自What is “Functional Programming”?

In this lesson, I defined functional programming like this:

Functional programming is a way of writing software applications using only pure functions and immutable values.

To support that, I also defined pure function like this:

The output of a pure function depends only on (a) its input parameters and (b) its internal algorithm.
A pure function has no side effects, meaning that it does not read anything from the outside world or write anything to the outside world.
As a result of those first two statements, if a pure function is called with an input parameter x an infinite number of times, it will always return the same result y.

I noted that higher-order functions (HOFs) are a terrific FP language feature, and also stated that recursion is a by-product of the definition of FP.

I also briefly discussed some of the benefits of immutable values (and FP in general):

The best FP code is like algebra
Pure functions and immutable values are easier to reason about
Without much support (yet), I stated that immutable values make parallel/concurrent programming easier

 

从函数式编程角度思考awk

昨天读到这篇文章:Awk, Unix, and functional programming,作者从函数式编程角度考虑awk,把awk总结为一个函数式程序:

Awk(action) =
    for each file
      for each input line
        for each pattern
          if pattern matches input line
            do action(fields)

即把awk程序看做一个函数,action作为awk的参数。对符合pattern的输入行,调用action处理这一行的每个field。上面这段伪代码可以帮助我更好地理解awk

 

推荐一篇介绍函数式编程(functional programming)的文章

说实话,对于函数式编程(functional programming),我一直云里雾里,不大明白。今天读到一篇文章:An introduction to functional programming,感觉写的不错,读完之后,至少觉得自己入门了。我把其中最核心的一段话摘录出来,并做了翻译:

When people talk about functional programming, they mention a dizzying number of “functional” characteristics. They mention immutable data, first class functions and tail call optimisation. These are language features that aid functional programming. They mention mapping, reducing, pipelining, recursing, currying and the use of higher order functions. These are programming techniques used to write functional code. They mention parallelization, lazy evaluation and determinism. These are advantageous properties of functional programs.  

Ignore all that. Functional code is characterised by one thing: the absence of side effects. It doesn’t rely on data outside the current function, and it doesn’t change data that exists outside the current function. Every other “functional” thing can be derived from this property. Use it as a guide rope as you learn.

译文:

当人们谈论起函数式编程,他们总会提到一堆令人眼花缭乱的“函数式”特性。他们提到“不可变数据”,“`first class functions`”和“尾调用优化”。这些是辅助函数式编程的语言特性。他们提到“`map/reduce`”,“流水线”,“递归”,“`currying`”和“高阶函数”。这些是写函数式代码的编程技术。他们提到“并行”,“惰性求值”和“确定性”。这些是函数式程序的优良特性。  

忽略上面提到的这些,函数式代码被一个特性所决定:没有副作用,即它不依赖于也不会改变当前函数以外的数据状态。其它所有“函数式特性”均由此特性衍生出来。