clojure and fp

26
Clojure FP Qin Jian 2013-04

Upload: qin-jian

Post on 25-May-2015

915 views

Category:

Technology


3 download

TRANSCRIPT

Page 1: Clojure and FP

Clojure 与 FP

Qin Jian

2013-04

Page 2: Clojure and FP

说明

● 初学者,没有深入内容● 不系统,零碎,分散● 引用为主● 与 clojure 没有太强联系

Page 3: Clojure and FP

动机

● 之前 lambda 算子的继续● Programming Language on Coursera

Page 4: Clojure and FP

Clojure

● LISP + JVM

● FP on JVM● https://github.com/clojure/clojure● http://clojure.org

Page 5: Clojure and FP

可能算是的特点

● 不变数据与副作用的控制● 支持惰性求值

– http://blog.sina.com.cn/s/blog_5d90e82f0101jz6j.html

– 王垠的批判 haskell

Page 6: Clojure and FP

可能的优点?无副作用/无变量的结果

● 单元测试– 没有外部状态,调试,测试方便

● 并行– 没有变量 -> 没有竞争– erlang<-prolog

Page 7: Clojure and FP

支路问题● 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

Page 8: Clojure and FP

语法

● Lambda 算子– 上次的内容:

● http://www.slideshare.net/qinjian623/lambda-15570486

Page 9: Clojure and FP

语法-续● S and M

– S-expression● 树结构的数据格式表示形式。有形式化的文档描述,一

个没有通过的 RFC ,地址:http://people.csail.mit.edu/rivest/Sexp.txt

● 用于通讯数据。● John McCarthy 最先提到。

Page 10: Clojure and FP

语法-续● S and M

– 岔路● Dennis Ritchie found dead 2011.10.12● John McCarthy 2011.10.24● Steve Jobs 2011.10.05

– 第一代计算机科学家正在离开 (Jobs 不算 )

Page 11: Clojure and FP

语法-续S M

(QUOTE A B C) (A B C)

(CAR X) car[x]

(CAR (APPEND (QUOTE (A B C)) (QUOTE (D E F))))

car[append[(A B C); (D E F)]]

Page 12: Clojure and FP

语法-续- 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-json-vs-s-expressions/

● XML 相对更加接近数据, JSON 和 S-exp 则与语言更紧密。

● 技术哲学话题,见仁见智。

Page 13: Clojure and FP

语法-续-高阶函数● 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."

Page 14: Clojure and FP

语法-续-高阶函数● Map Reduce

– 《大数据 互联网大规模数据挖掘与分布式处理》● 第二章内容● 利用两者实现选择、投影、并交差集的运算

Page 15: Clojure and FP

语法-续-高阶函数● Map(func list)

– 将 list中的每个元素都经过 func进行操作,形成新的一个 list。其实也可以同时操作多个 list,相对的 func就需要同时传入多个参数。

● Reduce(func init list)

– func接受两个参数,以此遍历 list,刚开始传入的是 list的第一和第二项,然后通过 func计算返回值,作为下次迭代传入的第一个参数。如有 init,第一次传入的为 init和 list第一个项。

● Map 可以被 Reduce来实现

Page 16: Clojure and FP

语法-续● call-by-value or call-by-name

– http://www.cs.columbia.edu/~sedwards/classes/2010/w4115-spring/functional.pdf

● Applicative or Normal Order

Page 17: Clojure and FP

语言与编程范型的松散关系

● Java 可以写出 FP风格代码– 没有意义

● LISP != FP

– 历史参见引用资料

Page 18: Clojure and 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

– “纯”函数式编程语言,无副作用

Page 19: Clojure and FP

示例与程序

● 小陶上次的字符组合程序– 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]]

Page 20: Clojure and FP

示例与程序● 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-since-okasaki

Page 21: Clojure and FP

嵌入式语言实现、解释器● 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》

Page 22: Clojure and FP

考虑中的玩具● Make or ant● Sql for csv file

Page 23: Clojure and FP

杂交化的趋势

● C++ lambda 引入● jvm class file dynamic 的类型引入,支持上层

动态语言● java 7 新属性

Page 24: Clojure and FP

没有银弹

Page 25: Clojure and FP

引用资料● 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

Page 26: Clojure and FP

引用资料

● elisp教程– By GNU

● http://www.gnu.org/software/emacs/emacs-lisp-intro/html_node/index.html

– By Xah● http://ergoemacs.org/emacs/elisp.html