函数式编程

a programming paradigm where programs are constructed by applying and composing functions, evolving from the lambda calculus.

First-class functions (higher-order functions)

  • enable currying

Pure functions

  • no side-effects: the result is constant with respect to that argument list (referential transarency)
  • no data dependency between two pure expressions, so that they can be performed in parallel and they cannot interfere with one another (thread-safe)
  • 纯函数是模块化的、可组合的、可复用的,因为它从“对结果做什么(返回值)”和“如何获取输入(通过参数传递获得输入)”中分离了计算本身的逻辑,因此不必担心对上下文的副作用
  • equational reasoning[等式推理]: 由于函数是确定的、没有副作用的,所以可用结果替换函数,便于证明和优化(性质的证明和理解,程序的调试和优化),不必跟踪函数执行前后状态的变化

Recursion

  • iteration in functional languages is usually accomplished via recursion

Data Structures

  • random access lists maps: 
  • logartimic access and update times
  • persistence: keeping previous versions of the data structure unmodified

Type System

  • the presence of strong compile-time type checking makes programs more reliable in absence of other reliability techniques like test-driven development
  • type inference frees the programmer from the need to manually declare types to the compiler in most cases

Notes

lambda calculus:λ演算是一个表达和计算函数的形式化系统,整个系统基于λ-expression(也叫λ-term / λ项),lambda演算包括构建λ项和对λ项执行归约 。一个λ项可以是一个变量(variable),一个抽象(abstraction)或一个应用(application);而归约操作包括α-变换、β-归约和η-变换。(归约是将某个计算问题转换为另一个问题的过程

lambda演算和图灵机都是计算模型且等价 (可以encode any computation / any Turing Machine program可以被转换成等价的Lambda Calculus program and vice versa);

lambda演算基于函数,而图灵机基于状态机+存储,不同点在于lambda演算的function是pure的(不涉及状态的改变)而图灵机有internal state。

lambda演算中的函数都是匿名函数。

algebraic data type: an algebraic data type is a kind of composite type (i.e., a type formed by combining other types) created by "algebraic" operations. The "algebra" here is "sums" and "products":

  • "sum" is alternation (A | B, meaning A or B but not both)
  • "product" is combination (A B, meaning A and B together)

side-effect: 修改一个变量 / 直接修改数据结构 / 抛出一个异常或以一个错误终止 / 打印到终端或读取用户的输入 / 读取或写入一个文件。

referential transparency: 函数的返回值只依赖于其输入值 (Functional programs do not have assignment statements, that is, the value of a variable in a functional program never changes once defined. This eliminates any chances of side effects because any variable can be replaced with its actual value at any point of execution)(引用透明是说函数依赖的所有东西都在参数中,不会依赖全局变量里的(可能会变的)东西;无副作用是说函数不会去改变全局状态,即每次调用该函数时,该函数所产生的所有副作用都会在其返回值中)

Y-combinator: 使lambda演算可以encode递归,赋予了在函数式语言中实现递归的能力:

  • 函数的不动点是说:在函数F定义域内存在一个X,使得F(X)=X。比如对于抛物线函数f(x)=x^2,它有两个不动点01,因为f(0)=0f(1)=1;而X不一定是值,也可以是一个函数,即存在一个g(x), 使得F(g(x))=g(x) ;换句话说上面例子中的值 x 是 g(x) 的一个特例,即常值函数 g(x) = cc是一个常数)。
  • 组合子(组合子逻辑): 不含自由变量的lambda表达式。(A free variable is a variable that is not a bound variable, and a bound variable is a variable which is contained inside the body of a lambda expression that has that variable name as one of its arguments [参考闭包])。如辅助组合子(identity等)、谓词组合子(filter、sort等)、函数组合子(map、reduce等)、SKI组合子等。组合子通常不声明任何变量也不包含任务逻辑,是一种高阶函数,它不改变原函数的原有功能特性,但可以通过变换参数、转变流程、控制输出来:增强函数 / 管理函数执行流程并在链式调用中对中间结果进行操作。
  • Curry定义出的Y-combinator,使得对于任意函数g,都有Yg=g(Yg),从而实现在匿名函数中递归调用自身,解决了匿名函数递归时不能用名称指代原始函数的问题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值