2016年的最后几小时, 随便写写关于Scala和OCaml的一些入门体验好了.

今年对FP语言特别感兴趣, 上了两门Scala的公开课(here and here)和一门OCaml的公开课(here), 在博客中写了一系列的笔记, 课后作业也都认真做完了. 斗胆说这两门语言都算入门了吧... 这里就随便写一下使用这两门语言的感受, 想到哪里写到哪里...

FP语言和之前接触的语言确实不大一样, 比如之前我都有种错觉, 学什么语言只要知道循环/条件/基本类型运算怎么写, 就差不多可以上手了...... 然后遇到了FP, 发现循环语句其实是不必要的... 记得看到过一篇文章, 类比学FP就好像开了很多种车的老司机突然开始学开宇宙飞船, 肯定各种WTF不适应了~

以前谈到FP我只能联想到一些Python里的FP特性: lambda表达式, 高阶函数之类的, 顶多还想到个闭包... 不过Python里面的FP特性和Scala/OCaml里的比起来还是差了不少: i.e. 现在非常希望Python里可以支持pattern matching...


Scala算是比较亲民的FP语言了(和Java有点像...), 也是我最早接触的FP语言. EPFL的那两门公开课质量很棒, 毕竟是Scala的作者亲自来上的...

  • immutable types: 习惯了就好, 就像java里所有东西都是final的, 要修改什么东西的时候改成新建一个, immutable数据的优点就是并行方便啊...
  • 一切皆为表达式, specifically …


this week: discuss how to handle events in user-interface — MVC, functional reactive programming.

Lecture 4.1 - Imperative Event Handling: The Observer Pattern

Traditional way of handling events: observer Pattern (MVC). Used when views need to react to change in a model.

MVC: model-view-controller for user interface

  • Views can announce …


This week: scala for imperative programming.

Lecture 3.1 - Functions and State

So far: pure functional programming
→ side-effect free: therefore time doesn't matter.
Any rewriting that terminates lead to the same solution. (Churcher-Rosser Th)

Now: mutable states

Stateful objects: objects can have state that change over time. (state is …


Lecture 2.1 - Structural Induction on Trees (optional)

Generalize the structural induction on list to general structures like trees.

To prove a property P(t) for all trees t:
* show for any leave l, P(l) holds
* for each internal node t with subtrees s1...sn, show P(s1 …


Recap: Functions and Pattern Matching

case classes

ex: json json objects can be seq, num, str, bool,...

⇒ represented as abstract class and case classes.

pattern matching

→ question: what is the type of the {case(key, value)=>"..."} clause?

it is (JBinding => String) type, which is a shorthand for Function1[JBinding …


6.1 - Other Collections

so far: only seen List. → more (immutable) collections.


List: is linear -- access to head is faster than middle or end element. Vector: better rand access performance.

represented as very shallow trees(32-split at each node)

Vector support similar operations as List (head, tail,map …


5.1 - More Functions on Lists

already known methods:


sublist and ele access:

  • xs.length
  • xs.last
  • xs.init: all elementh except last element
  • xs.take(n): sublist of first n elements
  • xs.drop(n): the rest of list after taking first n elements
  • xs …


4.1 - Objects Everywhere

scala is pure OO: every value is an obj, every operation is a method of obj.

scala.Int scala.Boolean maps to JVM standard primitive types.

Implement Boolean withous primitive type in scala:

Then defin false and true as objects, give implementation for ifThenElse() funciton …


This week, we'll cover traits, and we'll learn how to organize classes into hierarchies. We'll cover the hierarchy of standard Scala types, and see how to organize classes and traits into packages. Finally, we'll touch upon the different sorts of polymorphism in Scala.

3.1 - Class Hierarchies

abstract class …


This week, we'll learn about functions as first-class values, and higher order functions. We'll also learn about Scala's syntax and how it's formally defined. Finally, we'll learn about methods, classes, and data abstraction through the design of a data structure for rational numbers.

2.1 - Higher-Order Functions

higher order …