Scala 中的函数式编程基础(一)

 

 

主要来自 Scala 语言发明人 Martin Odersky 教授的 Coursera 课程 《Functional Programming Principles in Scala》

很久以前写过一个非常简单的 python lambda 函数博客,里头有 filter,map,reduce等,是python中很有意思的部分,可以先看看。

另外酷壳网陈皓写了一篇介绍函数式编程的博客,言简意赅,对理解函数式编程很有帮助,非常值得一看。


Scala 本意是可伸展。它的设计哲学是:允许用户通过定义感觉像原生语言支持一样的易用库去在他们需要的方向上改进和发展语言——Scala allows users to grow and adapt the language in the directions they need by defining easy-to-use libraries that feel like native language support.。Scala 运行在 Java 平台上,可以与所有的 Java 库无缝交互。

1. Function evaluations

Scala 是纯粹的面向对象式的编程,所有的 value 都是一个对象。
Scala 是函数式的编程,它把每一个函数看做一个 value,函数即对象。

1.1 函数的副作用

纯函数(Pure Function)是这样一种函数——输入输出数据流全是显式(Explicit)的。

显式(Explicit)的意思是,函数与外界交换数据只有一个唯一渠道——参数和返回值;函数从函数外部接受的所有输入信息都通过参数传递到该函数内部;函数输出到函数外部的所有信息都通过返回值传递到该函数外部。

隐式(Implicit)的意思是,函数通过参数和返回值以外的渠道,和外界进行数据交换。比如,读取全局变量,修改全局变量,都叫作以隐式的方式和外界进行数据交换;比如,利用I/O API(输入输出系统函数库)读取配置文件,或者输出到文件,打印到屏幕,都叫做隐式的方式和外界进行数据交换。

Pure Function的好处主要有几点:

  1. 无状态,Stateless。线程安全。不需要线程互斥或同步。
  2. Pure Function相互调用组装起来的函数,还是Pure Function。
  3. 应用程序或者运行环境(Runtime)可以对Pure Function的运算结果进行缓存,运算加快速度。

以上关于 pure function 的描述引用这篇博客
另外一篇博客也介绍函数的副作用,推荐看看!

在函数式编程语言里面,函数是一等公民,这意味着:

  • 像其他 value,可以在任何地方定义函数,包括函数内部
  • 像其他 value,函数可以作为其他函数的输入参数或者返回值
  • 像其他 value,函数也有自己的操作符进行组合等操作

1.2 参数调用的区别

scala 函数定义格式如下:
Alt text

  • call by value
    遇到表达式就计算,scala 函数参数默认进入函数内部之前计算出 value,再传进内部,关键字 val 也是立即计算模式
  • call by name
    惰性求值,这是函数式编程里面一个非常重要的概念。要用到的时候才计算,可以用 def fun(x:Int, y: => Int) = ... 中的 =>显式调用。关键字 def 也是这种模式

举个例子:

def loop: Int = loop    // 定义一个死循环,def 可以,val 不行
def constOne(x: Int, y: => Int) = 1 // 第一个参数x: call by value, 第二个参数y: call by name
constOne(1+2, loop)    // loop一直没用,所以没有计算,输出1
constOne(loop, 1+2)    // 遇到loop立即计算,陷入死循环

1.3 牛顿法求平方根

  • Scala 中递归函数需要写返回值类型,非递归不一定要写。
  • 函数定义可以放在函数内部,防止命名污染。

牛顿法的基本思路是递归,首先猜测为 1.0,如果误差过大,则猜测值改为 guess 与 x / guess 的平均值。

object session {
    def abs(x: Double) = if (x>=0) x else (-x)   
    def sqrt(x: Double) = {
        def sqrtIter(guess: Double): Double =
            if (isGoodEnough(guess)) guess
            else sqrtIter(improve(guess))

        def isGoodEnough(guess: Double) =
            abs(guess * guess - x) / x < 0.001

        def improve(guess: Double) =
            (guess + x / guess) / 2

        sqrtIter(1.0)
    }   
sqrt(2.0)  
}

1.4 尾递归

这篇博客推荐看看——递归与尾递归总结

递归相对常用的算法如普通循环等,运行效率较低。在递归调用的过程当中系统为每一层的返回点、局部量等开辟了栈来存储,因此递归次数过多容易造成栈溢出。

尾递归对应 imperative program(如c++)中的循环。 函数调用出现在调用者函数的尾部, 因为是尾部, 所以根本没有必要去保存任何局部变量(需要保存的变量可以放在函数参数列表里)。它的栈是常数,可复用,与循环一样高效。

递归:

// 阶乘函数
def factorial(n: Int): Int =
  if (n == 0) 1 else n * factotial(n - 1)

尾递归:

// 阶乘函数
def factorial(n: Int) = {
  def loop(acc: Int, n: Int): Int=
    if (n == 0) acc
    else loop(acc * n, n - 1)
  loop(1, n)
 }
Scala%20%u4E2D%u7684%u51FD%u6570%u5F0F%u7F16%u7A0B%u57FA%u7840%28%u4E00%29%0A%3D%3D%3D%0A%0A@%28scala%29%5B%u5B66%u4E60%u7B14%u8BB0%7CCoursera%5D%0A%0A%3E%20%u4E3B%u8981%u6765%u81EA%20Scala%20%u8BED%u8A00%u53D1%u660E%u4EBA%20Martin%20Odersky%20%u6559%u6388%u7684%20Coursera%20%u8BFE%u7A0B%20**%u300AFunctional%20Programming%20Principles%20in%20Scala%u300B**%u3002%0A%0A%u5F88%u4E45%u4EE5%u524D%u5199%u8FC7%u4E00%u4E2A%u975E%u5E38%u7B80%u5355%u7684%20%5Bpython%20lambda%20%u51FD%u6570%u535A%u5BA2%5D%28http%3A//www.cnblogs.com/daniel-D/p/3175426.html%29%uFF0C%u91CC%u5934%u6709%20filter%uFF0Cmap%uFF0Creduce%u7B49%uFF0C%u662Fpython%u4E2D%u5F88%u6709%u610F%u601D%u7684%u90E8%u5206%uFF0C%u53EF%u4EE5%u5148%u770B%u770B%u3002%0A%0A%u53E6%u5916%u9177%u58F3%u7F51%u9648%u7693%u5199%u4E86%u4E00%u7BC7%u4ECB%u7ECD%5B%u51FD%u6570%u5F0F%u7F16%u7A0B%5D%28http%3A//coolshell.cn/articles/10822.html%29%u7684%u535A%u5BA2%uFF0C%u8A00%u7B80%u610F%u8D45%uFF0C%u5BF9%u7406%u89E3%u51FD%u6570%u5F0F%u7F16%u7A0B%u5F88%u6709%u5E2E%u52A9%uFF0C%u975E%u5E38%u503C%u5F97%u4E00%u770B%u3002%0A%0A---%0A%0AScala%20%u672C%u610F%u662F%u53EF%u4F38%u5C55%u3002%u5B83%u7684%u8BBE%u8BA1%u54F2%u5B66%u662F%uFF1A**%u5141%u8BB8%u7528%u6237%u901A%u8FC7%u5B9A%u4E49%u611F%u89C9%u50CF%u539F%u751F%u8BED%u8A00%u652F%u6301%u4E00%u6837%u7684%u6613%u7528%u5E93%u53BB%u5728%u4ED6%u4EEC%u9700%u8981%u7684%u65B9%u5411%u4E0A%u6539%u8FDB%u548C%u53D1%u5C55%u8BED%u8A00**%u2014%u2014Scala%20allows%20users%20to%20grow%20and%20adapt%20the%20language%20in%20the%20directions%20they%20need%20by%20defining%20easy-to-use%20libraries%20that%20feel%20like%20native%20language%20support.%u3002Scala%20%u8FD0%u884C%u5728%20Java%20%u5E73%u53F0%u4E0A%uFF0C%u53EF%u4EE5%u4E0E%u6240%u6709%u7684%20Java%20%u5E93%u65E0%u7F1D%u4EA4%u4E92%u3002%0A%0A%23%23%201.%20Function%20evaluations%0AScala%20%u662F%u7EAF%u7CB9%u7684%u9762%u5411%u5BF9%u8C61%u5F0F%u7684%u7F16%u7A0B%uFF0C%u6240%u6709%u7684%20value%20%u90FD%u662F%u4E00%u4E2A%u5BF9%u8C61%u3002%0AScala%20%u662F%u51FD%u6570%u5F0F%u7684%u7F16%u7A0B%uFF0C%u5B83%u628A%u6BCF%u4E00%u4E2A%u51FD%u6570%u770B%u505A%u4E00%u4E2A%20value%uFF0C%u51FD%u6570%u5373%u5BF9%u8C61%u3002%0A%0A%23%23%23%201.1%20%u51FD%u6570%u7684%u526F%u4F5C%u7528%0A%0A%u7EAF%u51FD%u6570%uFF08Pure%20Function%uFF09%u662F%u8FD9%u6837%u4E00%u79CD%u51FD%u6570%u2014%u2014%u8F93%u5165%u8F93%u51FA%u6570%u636E%u6D41%u5168%u662F%u663E%u5F0F%uFF08Explicit%uFF09%u7684%u3002%20%0A%0A%u663E%u5F0F%uFF08Explicit%uFF09%u7684%u610F%u601D%u662F%uFF0C%u51FD%u6570%u4E0E%u5916%u754C%u4EA4%u6362%u6570%u636E%u53EA%u6709%u4E00%u4E2A%u552F%u4E00%u6E20%u9053%u2014%u2014%u53C2%u6570%u548C%u8FD4%u56DE%u503C%uFF1B%u51FD%u6570%u4ECE%u51FD%u6570%u5916%u90E8%u63A5%u53D7%u7684%u6240%u6709%u8F93%u5165%u4FE1%u606F%u90FD%u901A%u8FC7%u53C2%u6570%u4F20%u9012%u5230%u8BE5%u51FD%u6570%u5185%u90E8%uFF1B%u51FD%u6570%u8F93%u51FA%u5230%u51FD%u6570%u5916%u90E8%u7684%u6240%u6709%u4FE1%u606F%u90FD%u901A%u8FC7%u8FD4%u56DE%u503C%u4F20%u9012%u5230%u8BE5%u51FD%u6570%u5916%u90E8%u3002%20%0A%0A%u9690%u5F0F%uFF08Implicit%uFF09%u7684%u610F%u601D%u662F%uFF0C%u51FD%u6570%u901A%u8FC7%u53C2%u6570%u548C%u8FD4%u56DE%u503C%u4EE5%u5916%u7684%u6E20%u9053%uFF0C%u548C%u5916%u754C%u8FDB%u884C%u6570%u636E%u4EA4%u6362%u3002%u6BD4%u5982%uFF0C%u8BFB%u53D6%u5168%u5C40%u53D8%u91CF%uFF0C%u4FEE%u6539%u5168%u5C40%u53D8%u91CF%uFF0C%u90FD%u53EB%u4F5C%u4EE5%u9690%u5F0F%u7684%u65B9%u5F0F%u548C%u5916%u754C%u8FDB%u884C%u6570%u636E%u4EA4%u6362%uFF1B%u6BD4%u5982%uFF0C%u5229%u7528I/O%20API%uFF08%u8F93%u5165%u8F93%u51FA%u7CFB%u7EDF%u51FD%u6570%u5E93%uFF09%u8BFB%u53D6%u914D%u7F6E%u6587%u4EF6%uFF0C%u6216%u8005%u8F93%u51FA%u5230%u6587%u4EF6%uFF0C%u6253%u5370%u5230%u5C4F%u5E55%uFF0C%u90FD%u53EB%u505A%u9690%u5F0F%u7684%u65B9%u5F0F%u548C%u5916%u754C%u8FDB%u884C%u6570%u636E%u4EA4%u6362%u3002%20%0A%0APure%20Function%u7684%u597D%u5904%u4E3B%u8981%u6709%u51E0%u70B9%uFF1A%20%0A1.%20%u65E0%u72B6%u6001%uFF0CStateless%u3002%u7EBF%u7A0B%u5B89%u5168%u3002%u4E0D%u9700%u8981%u7EBF%u7A0B%u4E92%u65A5%u6216%u540C%u6B65%u3002%0A1.%20Pure%20Function%u76F8%u4E92%u8C03%u7528%u7EC4%u88C5%u8D77%u6765%u7684%u51FD%u6570%uFF0C%u8FD8%u662FPure%20Function%u3002%20%0A1.%20%u5E94%u7528%u7A0B%u5E8F%u6216%u8005%u8FD0%u884C%u73AF%u5883%uFF08Runtime%uFF09%u53EF%u4EE5%u5BF9Pure%20Function%u7684%u8FD0%u7B97%u7ED3%u679C%u8FDB%u884C%u7F13%u5B58%uFF0C%u8FD0%u7B97%u52A0%u5FEB%u901F%u5EA6%u3002%20%0A%0A%3E%20%u4EE5%u4E0A%u5173%u4E8E%20pure%20function%20%u7684%u63CF%u8FF0%u5F15%u7528%u8FD9%u7BC7%5B%u535A%u5BA2%5D%28http%3A//www.iteye.com/topic/177402%29%u3002%20%20%20%20%0A%u53E6%u5916%u4E00%u7BC7%5B%u535A%u5BA2%5D%28http%3A//www.cnblogs.com/snandy/archive/2011/08/14/2137898.html%29%u4E5F%u4ECB%u7ECD%u51FD%u6570%u7684%u526F%u4F5C%u7528%uFF0C%u63A8%u8350%u770B%u770B%uFF01%0A%0A%u5728%u51FD%u6570%u5F0F%u7F16%u7A0B%u8BED%u8A00%u91CC%u9762%uFF0C%u51FD%u6570%u662F**%u4E00%u7B49%u516C%u6C11**%uFF0C%u8FD9%u610F%u5473%u7740%uFF1A%0A-%20%u50CF%u5176%u4ED6%20value%uFF0C%u53EF%u4EE5%u5728%u4EFB%u4F55%u5730%u65B9%u5B9A%u4E49%u51FD%u6570%uFF0C%u5305%u62EC%u51FD%u6570%u5185%u90E8%0A-%20%u50CF%u5176%u4ED6%20value%uFF0C%u51FD%u6570%u53EF%u4EE5%u4F5C%u4E3A%u5176%u4ED6%u51FD%u6570%u7684%u8F93%u5165%u53C2%u6570%u6216%u8005%u8FD4%u56DE%u503C%0A-%20%u50CF%u5176%u4ED6%20value%uFF0C%u51FD%u6570%u4E5F%u6709%u81EA%u5DF1%u7684%u64CD%u4F5C%u7B26%u8FDB%u884C%u7EC4%u5408%u7B49%u64CD%u4F5C%0A%0A%23%23%23%201.2%20%u53C2%u6570%u8C03%u7528%u7684%u533A%u522B%0Ascala%20%u51FD%u6570%u5B9A%u4E49%u683C%u5F0F%u5982%u4E0B%uFF1A%0A%21%5BAlt%20text%5D%28./scala_1.png%29%0A%0A-%20**call%20by%20value**%0A%u9047%u5230%u8868%u8FBE%u5F0F%u5C31%u8BA1%u7B97%uFF0Cscala%20%u51FD%u6570%u53C2%u6570%u9ED8%u8BA4%u8FDB%u5165%u51FD%u6570%u5185%u90E8%u4E4B%u524D%u8BA1%u7B97%u51FA%20value%uFF0C%u518D%u4F20%u8FDB%u5185%u90E8%uFF0C%u5173%u952E%u5B57%20**val**%20%u4E5F%u662F%u7ACB%u5373%u8BA1%u7B97%u6A21%u5F0F%0A-%20**call%20by%20name**%20%20%20%20%0A%20%u60F0%u6027%u6C42%u503C%uFF0C%u8FD9%u662F%u51FD%u6570%u5F0F%u7F16%u7A0B%u91CC%u9762%u4E00%u4E2A%u975E%u5E38%u91CD%u8981%u7684%u6982%u5FF5%u3002%u8981%u7528%u5230%u7684%u65F6%u5019%u624D%u8BA1%u7B97%uFF0C%u53EF%u4EE5%u7528%20%60def%20fun%28x%3AInt%2C%20y%3A%20%3D%3E%20Int%29%20%3D%20...%60%20%u4E2D%u7684%20%60%3D%3E%60%u663E%u5F0F%u8C03%u7528%u3002%u5173%u952E%u5B57%20**def**%20%u4E5F%u662F%u8FD9%u79CD%u6A21%u5F0F%0A%0A%u4E3E%u4E2A%u4F8B%u5B50%uFF1A%0A%60%60%60scala%0Adef%20loop%3A%20Int%20%3D%20loop%20%20%20%20//%20%u5B9A%u4E49%u4E00%u4E2A%u6B7B%u5FAA%u73AF%uFF0Cdef%20%u53EF%u4EE5%uFF0Cval%20%u4E0D%u884C%0Adef%20constOne%28x%3A%20Int%2C%20y%3A%20%3D%3E%20Int%29%20%3D%201%20//%20%u7B2C%u4E00%u4E2A%u53C2%u6570x%3A%20call%20by%20value%2C%20%u7B2C%u4E8C%u4E2A%u53C2%u6570y%3A%20call%20by%20name%0AconstOne%281+2%2C%20loop%29%20%20%20%20//%20loop%u4E00%u76F4%u6CA1%u7528%uFF0C%u6240%u4EE5%u6CA1%u6709%u8BA1%u7B97%uFF0C%u8F93%u51FA1%0AconstOne%28loop%2C%201+2%29%20%20%20%20//%20%u9047%u5230loop%u7ACB%u5373%u8BA1%u7B97%uFF0C%u9677%u5165%u6B7B%u5FAA%u73AF%0A%60%60%60%0A%23%23%23%201.3%20%u725B%u987F%u6CD5%u6C42%u5E73%u65B9%u6839%0A-%20Scala%20%u4E2D%u9012%u5F52%u51FD%u6570%u9700%u8981%u5199%u8FD4%u56DE%u503C%u7C7B%u578B%uFF0C%u975E%u9012%u5F52%u4E0D%u4E00%u5B9A%u8981%u5199%u3002%0A-%20%u51FD%u6570%u5B9A%u4E49%u53EF%u4EE5%u653E%u5728%u51FD%u6570%u5185%u90E8%uFF0C%u9632%u6B62%u547D%u540D%u6C61%u67D3%u3002%0A%0A%u725B%u987F%u6CD5%u7684%u57FA%u672C%u601D%u8DEF%u662F%u9012%u5F52%uFF0C%u9996%u5148%u731C%u6D4B%u4E3A%201.0%uFF0C%u5982%u679C%u8BEF%u5DEE%u8FC7%u5927%uFF0C%u5219%u731C%u6D4B%u503C%u6539%u4E3A%20guess%20%u4E0E%20x%20/%20guess%20%u7684%u5E73%u5747%u503C%u3002%0A%60%60%60scala%0Aobject%20session%20%7B%0A%20%20%20%20def%20abs%28x%3A%20Double%29%20%3D%20if%20%28x%3E%3D0%29%20x%20else%20%28-x%29%20%20%20%0A%20%20%20%20def%20sqrt%28x%3A%20Double%29%20%3D%20%7B%0A%20%20%20%20%20%20%20%20def%20sqrtIter%28guess%3A%20Double%29%3A%20Double%20%3D%0A%20%20%20%20%20%20%20%20%20%20%20%20if%20%28isGoodEnough%28guess%29%29%20guess%0A%20%20%20%20%20%20%20%20%20%20%20%20else%20sqrtIter%28improve%28guess%29%29%0A%20%20%20%20%0A%20%20%20%20%20%20%20%20def%20isGoodEnough%28guess%3A%20Double%29%20%3D%0A%20%20%20%20%20%20%20%20%20%20%20%20abs%28guess%20*%20guess%20-%20x%29%20/%20x%20%3C%200.001%0A%0A%20%20%20%20%20%20%20%20def%20improve%28guess%3A%20Double%29%20%3D%0A%20%20%20%20%20%20%20%20%20%20%20%20%28guess%20+%20x%20/%20guess%29%20/%202%0A%0A%20%20%20%20%20%20%20%20sqrtIter%281.0%29%0A%20%20%20%20%7D%20%20%20%0Asqrt%282.0%29%20%20%0A%7D%0A%60%60%60%0A%0A%23%23%23%201.4%20%u5C3E%u9012%u5F52%0A%u8FD9%u7BC7%u535A%u5BA2%u63A8%u8350%u770B%u770B%u2014%u2014%5B%u9012%u5F52%u4E0E%u5C3E%u9012%u5F52%u603B%u7ED3%5D%28http%3A//www.cnblogs.com/Anker/archive/2013/03/04/2943498.html%29%u3002%0A%0A%u9012%u5F52%u76F8%u5BF9%u5E38%u7528%u7684%u7B97%u6CD5%u5982%u666E%u901A%u5FAA%u73AF%u7B49%uFF0C%u8FD0%u884C%u6548%u7387%u8F83%u4F4E%u3002**%u5728%u9012%u5F52%u8C03%u7528%u7684%u8FC7%u7A0B%u5F53%u4E2D%u7CFB%u7EDF%u4E3A%u6BCF%u4E00%u5C42%u7684%u8FD4%u56DE%u70B9%u3001%u5C40%u90E8%u91CF%u7B49%u5F00%u8F9F%u4E86%u6808%u6765%u5B58%u50A8%uFF0C%u56E0%u6B64%u9012%u5F52%u6B21%u6570%u8FC7%u591A%u5BB9%u6613%u9020%u6210%u6808%u6EA2%u51FA%u3002**%0A%0A%u5C3E%u9012%u5F52%u5BF9%u5E94%20imperative%20program%uFF08%u5982c++%uFF09%u4E2D%u7684%u5FAA%u73AF%u3002%20**%u51FD%u6570%u8C03%u7528%u51FA%u73B0%u5728%u8C03%u7528%u8005%u51FD%u6570%u7684%u5C3E%u90E8%2C%20%u56E0%u4E3A%u662F%u5C3E%u90E8%2C%20%u6240%u4EE5%u6839%u672C%u6CA1%u6709%u5FC5%u8981%u53BB%u4FDD%u5B58%u4EFB%u4F55%u5C40%u90E8%u53D8%u91CF%uFF08%u9700%u8981%u4FDD%u5B58%u7684%u53D8%u91CF%u53EF%u4EE5%u653E%u5728%u51FD%u6570%u53C2%u6570%u5217%u8868%u91CC%uFF09%u3002%u5B83%u7684%u6808%u662F%u5E38%u6570%uFF0C%u53EF%u590D%u7528%uFF0C%u4E0E%u5FAA%u73AF%u4E00%u6837%u9AD8%u6548%u3002**%0A%0A%u9012%u5F52%uFF1A%0A%60%60%60scala%0A//%20%u9636%u4E58%u51FD%u6570%0Adef%20factorial%28n%3A%20Int%29%3A%20Int%20%3D%0A%20%20if%20%28n%20%3D%3D%200%29%201%20else%20n%20*%20factotial%28n%20-%201%29%20%0A%60%60%60%0A%u5C3E%u9012%u5F52%uFF1A%0A%60%60%60scala%0A//%20%u9636%u4E58%u51FD%u6570%0Adef%20factorial%28n%3A%20Int%29%20%3D%20%7B%0A%20%20def%20loop%28acc%3A%20Int%2C%20n%3A%20Int%29%3A%20Int%3D%0A%20%20%20%20if%20%28n%20%3D%3D%200%29%20acc%0A%20%20%20%20else%20loop%28acc%20*%20n%2C%20n%20-%201%29%0A%20%20loop%281%2C%20n%29%0A%20%7D%20%20%0A%60%60%60

 

posted on 2014-06-27 22:41  daniel-D  阅读(1920)  评论(0编辑  收藏  举报

导航