SlideShare a Scribd company logo
1 of 26
Download to read offline
Clojure 与 FP



Qin Jian
2013-04
说明
●
    初学者,没有深入内容
●
    不系统,零碎,分散
●
    引用为主
●
    与 clojure 没有太强联系
动机
●
    之前 lambda 算子的继续
●
    Programming Language on Coursera
Clojure
●
    LISP   + JVM
●
    FP on JVM
●
    https://github.com/clojure/clojure
●
    http://clojure.org
可能算是的特点
●
    不变数据与副作用的控制
●
    支持惰性求值
    –   http://blog.sina.com.cn/s/blog_5d90e82f0101jz6j.h
        tml
    –   王垠的批判 haskell
可能的优点?无副作用/无变量的结果
●
    单元测试
    –   没有外部状态,调试,测试方便
●
    并行
    –   没有变量 -> 没有竞争
    –   erlang<-prolog
支路问题
●
    Continuation
●
    Monad in Haskell
    –   http://zhuoqiang.me/what-is-monad.html
    –   http://www.iis.sinica.edu.tw/~scm/ncs/2009/11/a-mo
        nad-primer/comment-page-1/
    –   http://www.douban.com/group/topic/1238401/
    –   http://yi-programmer.com/2010-04-06_haskell_and_ca
        tegory_translate.html
    –   http://stefan-klinger.de/files/monadGuide.pdf
●
    Uniqueness
语法
●
    Lambda 算子
    –   上次的内容:
        ●
            http://www.slideshare.net/qinjian623/lambda-15570486
语法-续
●
    S and M
    –   S-expression
        ●
            树结构的数据格式表示形式。有形式化的文档描述,一
            个没有通过的 RFC ,地址:
            http://people.csail.mit.edu/rivest/Sexp.txt
        ●
            用于通讯数据。
        ●
            John McCarthy 最先提到。
语法-续
●
    S and M
    –   岔路
        ●
            Dennis Ritchie found dead 2011.10.12
        ●
            John McCarthy 2011.10.24
        ●
            Steve Jobs 2011.10.05


    –   第一代计算机科学家正在离开 (Jobs 不算 )
语法-续
 S                              M

 (QUOTE A B C)                  (A B C)

 (CAR X)                        car[x]

 (CAR (APPEND (QUOTE (A B C))   car[append[(A B C); (D E F)]]
 (QUOTE (D E F))))
语法-续- sexp
●
    reinvent, 显然,三者的表达能力等价,语法相
    异但相近。相关的讨论:
    –   http://quoderat.megginson.com/2007/01/03/all-markup-ends-
        up-looking-like-xml/

    –   http://eli.thegreenplace.net/2012/03/04/some-thoughts-on-j
        son-vs-s-expressions/
●
    XML 相对更加接近数据, JSON 和 S-exp 则与语
    言更紧密。
●
    技术哲学话题,见仁见智。
语法-续-高阶函数
●
    Map Reduce
    –   http://www.cs.cornell.edu/courses/cs3110/2009sp/lectures/lec05.html
●
    Google 与 Hadoop
    –   良好的数据操作的抽象,可以作为一个通用的大规模数据处理框架,因
        为可以并行。 80 年代末就存在了使用这一抽象的并行系统 The Connection
        Machine 。但是时机对于技术的影响极大,有其自己的进化路径, Google
        将其发扬光大, Hadoop 作为 Google 的 Map reduce 的开源实现,目前已经
        被被广泛应用。
    –   "Our abstraction is inspired by the map and reduce primitives present in Lisp and
        many other functional languages. We realized that most of our computations involved
        applying a map operation to each logical record in our input in order to compute a set
        of intermediate key/value pairs, and then applying a reduce operation to all the
        values that shared the same key in order to combine the derived data appropriately."
语法-续-高阶函数
●
    Map Reduce
    –   《大数据 互联网大规模数据挖掘与分布式处理》
        ●
            第二章内容
        ●
            利用两者实现选择、投影、并交差集的运算
语法-续-高阶函数
●
    Map(func list)
    –将 list 中的每个元素都经过 func 进行操作,形成新的一个 list 。
     其实也可以同时操作多个 list, 相对的 func 就需要同时传入多个
     参数。
●
    Reduce(func init list)
    –   func 接受两个参数,以此遍历 list ,刚开始传入的是 list 的第一和
        第二项,然后通过 func 计算返回值,作为下次迭代传入的第一
        个参数。如有 init ,第一次传入的为 init 和 list 第一个项。
●
    Map 可以被 Reduce 来实现
语法-续
●
    call-by-value or call-by-name
    –   http://www.cs.columbia.edu/~sedwards/classes/201
        0/w4115-spring/functional.pdf
●
    Applicative or Normal Order
语言与编程范型的松散关系
●
    Java 可以写出 FP 风格代码
    – 没有意义
●
    LISP != FP
    –   历史参见引用资料
其他的一些语言
●
    ML -> F# 、 OCaml
    –   Coursera.org
●
    LISP -> elisp 、 common lisp 、 clojure
    –   elisp 教程参见引用资料
    –   elisp 是 dynamic scope
●
    Scala in twitter
    –   http://www.artima.com/scalazine/articles/twitter_on_scala.html
●
    Haskell
    –   “ 纯”函数式编程语言,无副作用
示例与程序
●
    小陶上次的字符组合程序
    –   basic 、 memoize 、 laziness
    –   见代码
●
    python 的 FP 风格玩具快排
    –   q=lambda s:s if len(s)<2 else q([x for x in s[1:]if
        x<s[0]])+[s[0]]+q([x for x in s[1:]if x>=s[0]]
示例与程序
●
    Purely Functional Data Structures until 1998
    –   http://www.cs.cmu.edu/~rwh/theses/okasaki.pdf
●
    New Data Structures since 1998
    –   http://cstheory.stackexchange.com/questions/1539/
        whats-new-in-purely-functional-data-structures-si
        nce-okasaki
嵌入式语言实现、解释器
●
    eval
    –   Racket <- Scheme
    –   资料来源
        ●
            https://class.coursera.org/proglang-2012-001/class/index
●
    bootstrap scheme
    –   1700 lines c
    –   尾递归最后转换为迭代
●
    Java 的伪递归构建
    –   《 The Role of the Study of Programming Languages in the Education of
        a Programmer 》
考虑中的玩具
●
    Make or ant
●
    Sql for csv file
杂交化的趋势
●
    C++ lambda 引入
●
    jvm class file dynamic   的类型引入,支持上层
    动态语言
●
    java 7 新属性
没有银弹
引用资料
●
    Lisp 历史
    –   History of Lisp by John McCarthy
        ●
            http://www-formal.stanford.edu/jmc/history/lisp/lisp.html
    –   History of Lisp by Paul Graham and also On Lisp
        ●
            http://www.paulgraham.com/lisphistory.html
        ●
            On Lisp ,有中文翻译版本。
●
    书籍
    –   Paradigms of Artificial Intelligence Programming: Case Studies in Common
        Lisp by Peter Norvig
        ●
            http://norvig.com/paip.html
    –   Concepts, Techniques, and Models of Computer Programming
        ●
            http://www.info.ucl.ac.be/~pvr/book.html
引用资料
●
    elisp 教程
    –   By GNU
        ●
            http://www.gnu.org/software/emacs/emacs-lisp-intro/htm
            l_node/index.html
    –   By Xah
        ●
            http://ergoemacs.org/emacs/elisp.html

More Related Content

Viewers also liked (13)

Gogol and the st. petersburg myth
Gogol and the st. petersburg mythGogol and the st. petersburg myth
Gogol and the st. petersburg myth
 
Cumbres borrascosas /Wuthering Heights
Cumbres borrascosas /Wuthering HeightsCumbres borrascosas /Wuthering Heights
Cumbres borrascosas /Wuthering Heights
 
On the story. unit 1
On the story. unit 1On the story. unit 1
On the story. unit 1
 
Short sale listing presentation sigal realty
Short sale listing presentation   sigal realtyShort sale listing presentation   sigal realty
Short sale listing presentation sigal realty
 
Short sale listing presentation sigal realty
Short sale listing presentation   sigal realtyShort sale listing presentation   sigal realty
Short sale listing presentation sigal realty
 
Presentacióriuraus
PresentacióriurausPresentacióriuraus
Presentacióriuraus
 
Narrative Conrad and Coppola
Narrative  Conrad and CoppolaNarrative  Conrad and Coppola
Narrative Conrad and Coppola
 
Diagram previous knowlege session
Diagram previous knowlege sessionDiagram previous knowlege session
Diagram previous knowlege session
 
On reading Dostoievski
On reading DostoievskiOn reading Dostoievski
On reading Dostoievski
 
Torch7 and ConvNet
Torch7 and ConvNetTorch7 and ConvNet
Torch7 and ConvNet
 
My Emacs Configs
My Emacs ConfigsMy Emacs Configs
My Emacs Configs
 
Tiempo y relato. t3
Tiempo y relato. t3Tiempo y relato. t3
Tiempo y relato. t3
 
Introductions to Neural Networks,Basic concepts
Introductions to Neural Networks,Basic conceptsIntroductions to Neural Networks,Basic concepts
Introductions to Neural Networks,Basic concepts
 

Similar to Clojure and FP

Adorable python
Adorable pythonAdorable python
Adorable python
Rhythm Sun
 
20130325 mldm monday spide r
20130325 mldm monday spide r20130325 mldm monday spide r
20130325 mldm monday spide r
Chia-Chi Chang
 
3小时 快速了解postgre sql
3小时 快速了解postgre sql3小时 快速了解postgre sql
3小时 快速了解postgre sql
Michael Fan
 
Baidu LSP and DISQL for Log Analysis
Baidu LSP and DISQL for Log AnalysisBaidu LSP and DISQL for Log Analysis
Baidu LSP and DISQL for Log Analysis
Xiaoming Chen
 

Similar to Clojure and FP (20)

Golangintro
GolangintroGolangintro
Golangintro
 
Hi Haskell
Hi HaskellHi Haskell
Hi Haskell
 
給軟體工程師的不廢話 R 語言精要班
給軟體工程師的不廢話 R 語言精要班給軟體工程師的不廢話 R 語言精要班
給軟體工程師的不廢話 R 語言精要班
 
The Evolution of Data Systems
The Evolution of Data SystemsThe Evolution of Data Systems
The Evolution of Data Systems
 
Weibo lamp improvements
Weibo lamp improvementsWeibo lamp improvements
Weibo lamp improvements
 
Adorable python
Adorable pythonAdorable python
Adorable python
 
Introduction of Spark by Wang Haihua
Introduction of Spark by Wang HaihuaIntroduction of Spark by Wang Haihua
Introduction of Spark by Wang Haihua
 
Tcfsh bootcamp day2
 Tcfsh bootcamp day2 Tcfsh bootcamp day2
Tcfsh bootcamp day2
 
微博Lamp性能优化之路(2014)
微博Lamp性能优化之路(2014)微博Lamp性能优化之路(2014)
微博Lamp性能优化之路(2014)
 
前端爆肝之旅+React上山前的小專案心得分享
前端爆肝之旅+React上山前的小專案心得分享前端爆肝之旅+React上山前的小專案心得分享
前端爆肝之旅+React上山前的小專案心得分享
 
Postgre sql intro 0
Postgre sql intro 0Postgre sql intro 0
Postgre sql intro 0
 
20130325 mldm monday spide r
20130325 mldm monday spide r20130325 mldm monday spide r
20130325 mldm monday spide r
 
Golang server design pattern
Golang server design patternGolang server design pattern
Golang server design pattern
 
getPDF.aspx
getPDF.aspxgetPDF.aspx
getPDF.aspx
 
getPDF.aspx
getPDF.aspxgetPDF.aspx
getPDF.aspx
 
給初學者的Spark教學
給初學者的Spark教學給初學者的Spark教學
給初學者的Spark教學
 
3小时 快速了解postgre sql
3小时 快速了解postgre sql3小时 快速了解postgre sql
3小时 快速了解postgre sql
 
從統計到資料科學
從統計到資料科學從統計到資料科學
從統計到資料科學
 
Baidu LSP and DISQL for Log Analysis
Baidu LSP and DISQL for Log AnalysisBaidu LSP and DISQL for Log Analysis
Baidu LSP and DISQL for Log Analysis
 
Linux c++ 编程之链接与装载 -基础篇--v0.3--20120509
Linux c++ 编程之链接与装载 -基础篇--v0.3--20120509Linux c++ 编程之链接与装载 -基础篇--v0.3--20120509
Linux c++ 编程之链接与装载 -基础篇--v0.3--20120509
 

Clojure and FP

  • 1. Clojure 与 FP Qin Jian 2013-04
  • 2. 说明 ● 初学者,没有深入内容 ● 不系统,零碎,分散 ● 引用为主 ● 与 clojure 没有太强联系
  • 3. 动机 ● 之前 lambda 算子的继续 ● Programming Language on Coursera
  • 4. Clojure ● LISP + JVM ● FP on JVM ● https://github.com/clojure/clojure ● http://clojure.org
  • 5. 可能算是的特点 ● 不变数据与副作用的控制 ● 支持惰性求值 – http://blog.sina.com.cn/s/blog_5d90e82f0101jz6j.h tml – 王垠的批判 haskell
  • 6. 可能的优点?无副作用/无变量的结果 ● 单元测试 – 没有外部状态,调试,测试方便 ● 并行 – 没有变量 -> 没有竞争 – erlang<-prolog
  • 7. 支路问题 ● Continuation ● Monad in Haskell – http://zhuoqiang.me/what-is-monad.html – http://www.iis.sinica.edu.tw/~scm/ncs/2009/11/a-mo nad-primer/comment-page-1/ – http://www.douban.com/group/topic/1238401/ – http://yi-programmer.com/2010-04-06_haskell_and_ca tegory_translate.html – http://stefan-klinger.de/files/monadGuide.pdf ● Uniqueness
  • 8. 语法 ● Lambda 算子 – 上次的内容: ● http://www.slideshare.net/qinjian623/lambda-15570486
  • 9. 语法-续 ● S and M – S-expression ● 树结构的数据格式表示形式。有形式化的文档描述,一 个没有通过的 RFC ,地址: http://people.csail.mit.edu/rivest/Sexp.txt ● 用于通讯数据。 ● John McCarthy 最先提到。
  • 10. 语法-续 ● S and M – 岔路 ● Dennis Ritchie found dead 2011.10.12 ● John McCarthy 2011.10.24 ● Steve Jobs 2011.10.05 – 第一代计算机科学家正在离开 (Jobs 不算 )
  • 11. 语法-续 S M (QUOTE A B C) (A B C) (CAR X) car[x] (CAR (APPEND (QUOTE (A B C)) car[append[(A B C); (D E F)]] (QUOTE (D E F))))
  • 12. 语法-续- sexp ● reinvent, 显然,三者的表达能力等价,语法相 异但相近。相关的讨论: – http://quoderat.megginson.com/2007/01/03/all-markup-ends- up-looking-like-xml/ – http://eli.thegreenplace.net/2012/03/04/some-thoughts-on-j son-vs-s-expressions/ ● XML 相对更加接近数据, JSON 和 S-exp 则与语 言更紧密。 ● 技术哲学话题,见仁见智。
  • 13. 语法-续-高阶函数 ● Map Reduce – http://www.cs.cornell.edu/courses/cs3110/2009sp/lectures/lec05.html ● Google 与 Hadoop – 良好的数据操作的抽象,可以作为一个通用的大规模数据处理框架,因 为可以并行。 80 年代末就存在了使用这一抽象的并行系统 The Connection Machine 。但是时机对于技术的影响极大,有其自己的进化路径, Google 将其发扬光大, Hadoop 作为 Google 的 Map reduce 的开源实现,目前已经 被被广泛应用。 – "Our abstraction is inspired by the map and reduce primitives present in Lisp and many other functional languages. We realized that most of our computations involved applying a map operation to each logical record in our input in order to compute a set of intermediate key/value pairs, and then applying a reduce operation to all the values that shared the same key in order to combine the derived data appropriately."
  • 14. 语法-续-高阶函数 ● Map Reduce – 《大数据 互联网大规模数据挖掘与分布式处理》 ● 第二章内容 ● 利用两者实现选择、投影、并交差集的运算
  • 15. 语法-续-高阶函数 ● Map(func list) –将 list 中的每个元素都经过 func 进行操作,形成新的一个 list 。 其实也可以同时操作多个 list, 相对的 func 就需要同时传入多个 参数。 ● Reduce(func init list) – func 接受两个参数,以此遍历 list ,刚开始传入的是 list 的第一和 第二项,然后通过 func 计算返回值,作为下次迭代传入的第一 个参数。如有 init ,第一次传入的为 init 和 list 第一个项。 ● Map 可以被 Reduce 来实现
  • 16. 语法-续 ● call-by-value or call-by-name – http://www.cs.columbia.edu/~sedwards/classes/201 0/w4115-spring/functional.pdf ● Applicative or Normal Order
  • 17. 语言与编程范型的松散关系 ● Java 可以写出 FP 风格代码 – 没有意义 ● LISP != FP – 历史参见引用资料
  • 18. 其他的一些语言 ● ML -> F# 、 OCaml – Coursera.org ● LISP -> elisp 、 common lisp 、 clojure – elisp 教程参见引用资料 – elisp 是 dynamic scope ● Scala in twitter – http://www.artima.com/scalazine/articles/twitter_on_scala.html ● Haskell – “ 纯”函数式编程语言,无副作用
  • 19. 示例与程序 ● 小陶上次的字符组合程序 – basic 、 memoize 、 laziness – 见代码 ● python 的 FP 风格玩具快排 – q=lambda s:s if len(s)<2 else q([x for x in s[1:]if x<s[0]])+[s[0]]+q([x for x in s[1:]if x>=s[0]]
  • 20. 示例与程序 ● Purely Functional Data Structures until 1998 – http://www.cs.cmu.edu/~rwh/theses/okasaki.pdf ● New Data Structures since 1998 – http://cstheory.stackexchange.com/questions/1539/ whats-new-in-purely-functional-data-structures-si nce-okasaki
  • 21. 嵌入式语言实现、解释器 ● eval – Racket <- Scheme – 资料来源 ● https://class.coursera.org/proglang-2012-001/class/index ● bootstrap scheme – 1700 lines c – 尾递归最后转换为迭代 ● Java 的伪递归构建 – 《 The Role of the Study of Programming Languages in the Education of a Programmer 》
  • 22. 考虑中的玩具 ● Make or ant ● Sql for csv file
  • 23. 杂交化的趋势 ● C++ lambda 引入 ● jvm class file dynamic 的类型引入,支持上层 动态语言 ● java 7 新属性
  • 25. 引用资料 ● Lisp 历史 – History of Lisp by John McCarthy ● http://www-formal.stanford.edu/jmc/history/lisp/lisp.html – History of Lisp by Paul Graham and also On Lisp ● http://www.paulgraham.com/lisphistory.html ● On Lisp ,有中文翻译版本。 ● 书籍 – Paradigms of Artificial Intelligence Programming: Case Studies in Common Lisp by Peter Norvig ● http://norvig.com/paip.html – Concepts, Techniques, and Models of Computer Programming ● http://www.info.ucl.ac.be/~pvr/book.html
  • 26. 引用资料 ● elisp 教程 – By GNU ● http://www.gnu.org/software/emacs/emacs-lisp-intro/htm l_node/index.html – By Xah ● http://ergoemacs.org/emacs/elisp.html