SlideShare a Scribd company logo
1 of 64
Download to read offline
Introduction to Functional Programming with
Ocaml
Pramode C.E
http://pramode.net

January 30, 2014
Workshop Plan
Here is what we will do:
Learn some Ocaml syntax and write simple Ocaml programs
Learn important concepts like: closures, higher order functions,
purity, lazy vs strict evaluation, currying, tail calls/TCO,
immutability, persistent data structures, type inference etc!
Understand the essence of the "Functional Programming"
paradigm through Ocaml
Not beginner-level stu - but I will try to keep things as simple as
possible!

Pramode C.E

Introduction to Functional Programming with Ocaml
Ocaml - some background info

Written in 1996 by Xavier Leroy and other researchers at
INRIA, France
A member of the ML family
http://en.wikipedia.org/wiki/ML_(programming_language)
Supports Functional, Imperative and Object oriented
programming
A powerful static type system with type inference
Major inuence behind Microsoft F#
Recent developments: the Core standard library (by Jane
Street) and realworldocaml.org

Pramode C.E

Introduction to Functional Programming with Ocaml
Pramode C.E

Introduction to Functional Programming with Ocaml
Where is it being used?

List of companies: http://ocaml.org/learn/companies.html
Facebook - pf, a tool for analysing PHP source
Citrix - Uses Ocaml in XenServer, a virtualization system
Jane Street - develops trading systems in Ocaml
Bloomberg - uses Ocaml for developing nancial derivatives
risk management applications

Pramode C.E

Introduction to Functional Programming with Ocaml
The Ocaml REPL
Invoke the REPL with the utop command at the shell prompt:
utop # print_string hellon;;
hello
- : unit = ()
utop # 2 * 3;;
- : int = 6
utop # let x = 10;;
val x : int = 10
utop # x * 2;;
- : int = 20
utop # let y = 5 in y * 8;;
- : int = 40
utop #

Question: what is the dierence between let and let ... in?
Pramode C.E

Introduction to Functional Programming with Ocaml
Function Denition

let add a b = a + b
let m = add 10 20

We dene add to be a function which accepts two parameters of
type int and returns a value of type int. Similarly, m is dened as a
variable of type int.
But wait ... we haven't actually dened all these types!

Pramode C.E

Introduction to Functional Programming with Ocaml
Type Inference

We have NOT specied the return type of the function or the type
of the variable m. Ocaml infers that (the technical term is HindleyMilner type inference)
This is the type which utop shows us:
val add : int - int - int
Read this as: function takes two int's as arguments and returns an
int.

Pramode C.E

Introduction to Functional Programming with Ocaml
Static vs Dynamic typing

#Python - dynamic typing
def foo(x): return x/2
/* C - static typing with NO type inference */
int foo(int x) { return x / 2; }
(* ML - static typing with type inference *)
let foo x = x / 2

Pramode C.E

Introduction to Functional Programming with Ocaml
Expression Oriented Programming

let i = 0
let p = if (i  0) then -1 else -2
let q = if (true) then hello else world

Unlike languages like C/Java, almost everything in Ocaml is an
expression, ie, something which computes a value! Rather than
programming with statements, we program with expressions.

Pramode C.E

Introduction to Functional Programming with Ocaml
Recursion

(* compute sum of first n numbers *)
let rec sum n =
if (n = 0) then 0 else n + sum (n - 1)
let r = sum 10

Try calling the function sum with a large number (say 1000000)
as parameter! You get a stack overow!

Pramode C.E

Introduction to Functional Programming with Ocaml
Tail Calls and TCO

(* compute sum of first n numbers *)
let rec sum n acc =
if (n = 0) then acc else sum (n - 1) (acc + n)
let r = sum 10 0

This is a tail-recursive version of the previous function - the
Ocaml compiler converts the tail call to a loop, thereby avoiding
stack overow!

Pramode C.E

Introduction to Functional Programming with Ocaml
Tail Calls and TCO
(sum 4)
(4 + sum 3)
(4 + (3 + sum 2))
(4 + (3 + (2 + sum 1)))
(4 + (3 + (2 + (1 + sum 0))))
(4 + (3 + (2 + (1 + 0))))
(4 + (3 + (2 + 1)))
(4 + (3 + 3))
(4 + 6)
(10)
-----------------------------(sum 4 0)
(sum 3 4)
(sum 2 7)
(sum 1 9)
(sum 0 10)
(10)
Pramode C.E

Introduction to Functional Programming with Ocaml
Parametric polymorphism
let foo x = x

Our function foo is the identity function; it takes in a value and
returns the same! What is the inferred type of this function?
More examples:
let foo x y = x
let foo2 x y = if true then x else y
let foo3 x y = if x then y else y
let foo4 x y = if x then y else x
Pramode C.E

Introduction to Functional Programming with Ocaml
Higher order functions

let double x = 2*x
let triple x = 3*x
let apply f x = f x
let p = apply double 10
let q = apply triple 10

What is type of apply?

Pramode C.E

Introduction to Functional Programming with Ocaml
Summation once again!
let sqr x = x * x
let cube x = x * x * x
let rec sumSimple a b =
if a = b then a else a + sumSimple (a + 1) b
let rec sumSquares a b =
if a = b then (sqr a) else (sqr a) + sumSquares (a + 1) b
let rec sumCubes a b =
if a = b then (cube a) else (cube a) + sumCubes (a + 1) b

Pramode C.E

Introduction to Functional Programming with Ocaml
Summation - a more elegant method

let identity x = x
let sqr x = x * x
let cube x = x * x * x
let rec sum f a b =
if a = b then (f a) else (f a) + sum f (a + 1) b
let p = sum identity 0 4
let q = sum sqr 0 4
let r = sum cube 0 4

Pramode C.E

Introduction to Functional Programming with Ocaml
Anonymous functions

let apply f x = f x
let p = apply (fun x - 2*x) 10

We can create anonymous functions on-the-y! fun x - 2*x is a
function which takes an x and returns 2*x.

Pramode C.E

Introduction to Functional Programming with Ocaml
Methods on collections: Map/Filter/Reduce
open Core.Std
let a = [1;2;3;4]
let p = List.map a (fun x - x * x)
let q = List.filter a (fun x - (x mod 2) = 0)
let r = List.reduce a (fun x y - x + y)

Map applies a function on all elements of a sequence. Filter selects
a set of values from a sequence based on the boolean value
returned by a function passed as its parameter - both functions
return a new sequence. Reduce combines the elements of a
sequence into a single element.
Pramode C.E

Introduction to Functional Programming with Ocaml
More methods on collections

open Core.Std
let even x = (x mod 2) = 0
let a = [1;2;3;4;5;6;7]
let b = [2;4;6;5;10;11;13;12]
let
let
let
let

c
d
e
f

=
=
=
=

List.for_all a even
List.exists a even
List.take_while a even
List.partition_tf a even

Pramode C.E

Introduction to Functional Programming with Ocaml
Nested functions / functions returning functions

(* foo returns sqr *)
let foo () =
let sqr x = x * x in sqr
let f = foo ()
let g = f 10

Pramode C.E

Introduction to Functional Programming with Ocaml
Lexical Closure

let foo1 x = let foo2 y = x + y in foo2
let f = foo1 10
let r = f 20

Pramode C.E

Introduction to Functional Programming with Ocaml
Lexical Closure

The function foo1 returns a closure.
A closure is a function which carries with it references to the
environment in which it was dened.
When we call f(20), the foo2 function gets executed with
the environment it had when it was dened - in this
environment, the value of x is 10.

Pramode C.E

Introduction to Functional Programming with Ocaml
Simple closure examples

let sqr x = x * x
let cube x = x * x * x
let compose f g = fun x - f (g x)
let f = compose sqr cube
let m = f 2

Pramode C.E

Introduction to Functional Programming with Ocaml
Simple closure examples
open Core.Std
let remove_low_scores xs threshold =
List.filter xs (fun score - score = threshold)
let m = remove_low_scores [20; 35; 80; 92; 40; 98] 50

The anonymous function fun score - score = threshold is
the closure here.
How do you know that it is a closure? Its body uses a variable
threshold which is not in its local environment (the local
environment, in this case, is the parameter list consisting of a
single parameter score)
Pramode C.E

Introduction to Functional Programming with Ocaml
Currying and Partial Application

Here is the denition from Wikipedia:
In mathematics and computer science, currying is the technique of
transforming a function that takes multiple arguments (or a tuple
of arguments) in such a way that it can be called as a chain of
functions, each with a single argument. It was originated by Moses
Schonnkel and later re-discovered by Haskell Curry.

Pramode C.E

Introduction to Functional Programming with Ocaml
Currying and Partial Application

let add x y = x + y
let f = add 10
let g = f 20

The type of add is int - int - int. The correct way to read this
is: add takes an int as argument and returns a function which maps
an int to an int.

Pramode C.E

Introduction to Functional Programming with Ocaml
Currying and Partial Application

let add x y z = x + y + z
let f = add 10
let g = f 20
let h = g 30

Pramode C.E

Introduction to Functional Programming with Ocaml
Algebraic Data Types - Product types

let a = (1, 2) (* type: int*int *)
let b = (hello, 10, 2) (* type: string*int*int *)

An Algebraic Data Type is a composite type - a type formed by
combining other types. Two classes of algebraic data types are the
product and sum types.

Pramode C.E

Introduction to Functional Programming with Ocaml
Algebraic Data Types - Sum types

type shape =

Circle of int
| Square of int
| Rectangle of (int*int)

let a = Circle 10 (* circle of radius 10 *)
let b = Square 20 (* square with sides 20 *)
let c = Rectangle (5,10) (* rectangle with sides 5 and 10 *)

The type shape describes a shape which can be one of Circle,
Square or Rectangle.

Pramode C.E

Introduction to Functional Programming with Ocaml
Pattern Matching

let a = (1, (2, hello))
let (x, (y, z)) = a

Pattern matching helps you pull apart complex datastructures and
analyse their components in an elegant way (not to be confused
with regular expression based pattern matching which is a
completely dierent thing).

Pramode C.E

Introduction to Functional Programming with Ocaml
Pattern Matching
type shape =

Circle of float
| Square of float
| Rectangle of (float*float)

let area s = match s with
| Circle r - 3.14 *. r *. r
| Square x - x *. x
| Rectangle (x, y) - x *. y
let a = area (Circle 10.0)
let b = area (Square 20.0)
let c = area (Rectangle (5.0,10.0))

Pramode C.E

Introduction to Functional Programming with Ocaml
Pattern Matching
type shape =

Circle of float
| Square of float
| Rectangle of (float*float)

let area s = match s with
| Circle r - 3.14 *. r *. r
| Square x - x *. x
let a = area (Circle 10.0)
let b = area (Square 20.0)
let c = area (Rectangle (5.0,10.0))

The compiler warns us when it detects that the match is not
exhaustive.
Pramode C.E

Introduction to Functional Programming with Ocaml
Pattern Matching

let rec len xs =
match xs with
| [] - 0
| hd :: tl - 1 + len tl
let a = len [1;2;3;4]

A simple recursive routine for computing length of a list. If list is
empty, length is 0, otherwise, it is 1 + length of remaining part of
the list.

Pramode C.E

Introduction to Functional Programming with Ocaml
Immutability and the nature of variables

let a = 1
let foo x = x + a;
a = 2 (* simply compares a with 2 *)
let a = 0 (* earlier binding gets shadowed *)
let m = foo 10 (* result is 11 *)

let bindings are immutable; you can shadow a binding, but an
earlier binding can't be mutated.

Pramode C.E

Introduction to Functional Programming with Ocaml
Immutability

Let's look at some Python list manipulation code:
a = [20, 30, 40, 50]
a.insert(0, 10)
print a # prints [10, 20, 30, 40, 50]

Python lists are mutable. The insert operation modies the original
list.

Pramode C.E

Introduction to Functional Programming with Ocaml
Immutability
Here is a similar program in Ocaml:
let a = [20;30;40;50]
let b = 10::a (* a remains unchanged *)
let c = [60;70;80]
let d = a @ c (* a remains unchanged *)

Good Ocaml style encourages use immutable data structures.

Pramode C.E

Introduction to Functional Programming with Ocaml
Immutability: Structure sharing

This is one way to implement 4::b - but it requires creating a full
copy of the old list b (red-colored cells 1, 2, 3 are copies of cells
containing the same values in b) and adding 4 at the head
position. Ocaml does NOT do it this way because it is very
inecient.

Pramode C.E

Introduction to Functional Programming with Ocaml
Immutability: Structure sharing

This is how Ocaml actually does 4::b. Note that the new list shares
all its elements (except 4) with the old list!

Pramode C.E

Introduction to Functional Programming with Ocaml
Immutability: Structure sharing

This is what happens when you append two lists A and B (using @
in Ocaml). Note that the appending operation needs to copy the
whole of list A.

Pramode C.E

Introduction to Functional Programming with Ocaml
Immutability: Structure sharing

The structure sharing idea can be used eciently with tree
data structures.
For more info: http:
//en.wikipedia.org/wiki/Persistent_data_structure

Persistent vectors in Scala:

http://www.codecommit.com/blog/scala/
implementing-persistent-vectors-in-scala

Pramode C.E

Introduction to Functional Programming with Ocaml
Mutability and aliasing

a = [1,2,3,4]
b = a
b.append(5)
print a

Having aliases to mutable objects can create very hard to track
down bugs in our code.

Pramode C.E

Introduction to Functional Programming with Ocaml
Non-strict evaluation

let my_if cond then_part else_part =
if cond then then_part else else_part
let a = my_if (1  2) 10 20

We are trying to write a function which behaves similar to the
built-in if control structure in Ocaml ... does it really work
properly? Let's try another example!

Pramode C.E

Introduction to Functional Programming with Ocaml
Non-strict evaluation

let foo1 () = print_string foo1n; 10
let foo2 () = print_string foo2n; 20
let my_if cond then_part else_part =
if cond then then_part else else_part
let a = my_if (1  2) (foo1()) (foo2())

Pramode C.E

Introduction to Functional Programming with Ocaml
Non-strict evaluation

The behaviour of if is non-strict: In the expression if
(cond) then e1 else e2, if cond is true e2 is NOT
EVALUATED. Also, if cond is false, e1 is NOT
EVALUATED.
By default, the behaviour of function calls in Ocaml is strict:
In the expression fun e1 e2 ... en, ALL the expressions e1, e2
... en are evaluated before the function is called.

Pramode C.E

Introduction to Functional Programming with Ocaml
Non-strict evaluation

let foo1 () = print_string foo1n; 10
let foo2 () = print_string foo2n; 20
let my_if cond then_part else_part =
if cond then (then_part()) else (else_part())
let a = my_if (1  2) (fun () - (foo1()))
(fun () - (foo2()))

Pramode C.E

Introduction to Functional Programming with Ocaml
Referential Transparency

If an expression can be replaced by its value without changing the
behaviour of the program, it is said to be referentially transparent
All occurrences of the expression 1+(2*3) can be replaced by
7 without changing the behaviour of the program.
Say the variable x (in a C program) has initial value 5. It is
NOT possible to replace all occurrences of the statement ++x
with the value 6.

Pramode C.E

Introduction to Functional Programming with Ocaml
Pure Functions
#include stdio.h
int balance = 1000;
int withdraw(int amt)
{
balance = balance - amt;
return balance;
}
int main()
{
printf(%dn, withdraw(100));
printf(%dn, withdraw(100));
return 0;
}
Pramode C.E

Introduction to Functional Programming with Ocaml
Pure Functions

A pure function always computes the same value given the
same parameters; for example, sin(0) is always 0. It is
Referentially Transparent.
Evaluation of a pure function does not cause any observable
side eects or output - like mutation of global variables or
output to I/O devices.

Pramode C.E

Introduction to Functional Programming with Ocaml
What is Functional Programming?

A style of programming which emphasizes composing your program
out of PURE functions and immutable data.
Theoretical foundation based on Alonzo Church's Lambda Calculus
In order for this style to be eective in the construction of real
world programs, we make use of most of the ideas seen so far
(higher order functions, lexical closures, currying, immutable and
persistent datastructures, lazy evaluation etc)

Pramode C.E

Introduction to Functional Programming with Ocaml
What is Functional Programming?

Questions to ask:
Is this the silver bullet?
How practical is a program composed completely out of
pure functions?
What are the benets of writing in a functional style?

Pramode C.E

Introduction to Functional Programming with Ocaml
Is FP the silver bullet?

Of course, there are NO silver bullets!
(http://en.wikipedia.org/wiki/No_Silver_Bullet)
Writing software is tough - no one methodology or technique
is going to solve all your problems

Pramode C.E

Introduction to Functional Programming with Ocaml
How practical is a program composed completely out of
pure functions?

Very impractical - unless your aim is to ght the chill by
heating up the CPU (which, by the way, is a side eect)
The idea is to write your program in such a way that it has a
purely functional core, surrounded by a few impure functions
at the outer layers

Pramode C.E

Introduction to Functional Programming with Ocaml
Functional core + impure outer layers
This example is simple and contrived, but it serves to illustrate the
idea. It is taken from the amazing book Functional Programming
in Scala by Paul Chiusano and Runar Bjarnason.
let declareWinner p =
let (name, score) = p in
print_string (name ^ n)
let winner p1 p2 =
let ((_, score1), (_, score2)) = (p1, p2) in
if (score1  score2) then (declareWinner p1)
else (declareWinner p2)

Note that winner is an impure function. We will now refactor it a
little!
Pramode C.E

Introduction to Functional Programming with Ocaml
Functional core + impure outer layers
let declareWinner p =
let (name, score) = p in
print_string (name ^ n)
let maxScore p1 p2 =
let ((_, score1), (_, score2)) = (p1, p2) in
if (score1  score2) then p1 else p2
let winner p1 p2 =
declareWinner (maxScore p1 p2)

Now we have separated the computation from the display logic;
maxScore is a pure function and winner is the impure function
at the outer layer!
Pramode C.E

Introduction to Functional Programming with Ocaml
Benets of functional style - easy reuse, easy testing
What if we wish to nd out the winner among a set of N players?
Easy!
open Core.Std
let maxScore p1 p2 =
let ((_, score1), (_, score2)) = (p1, p2) in
if (score1  score2) then p1 else p2
let a = [(Ram, 68); (John, 72); (Arun, 57)]
let b = List.reduce a maxScore

Pramode C.E

Introduction to Functional Programming with Ocaml
FP as good software engineering

Pure functions are easy to re-use as they have no context
other than the function parameters. (Think about re-using the
winner function in our rst version to compute the winner
among N players).
Pure functions are also easy to test. (Think about writing an
automated test for the winner function in the rst version).
Think of FP as Good Software Engineering!

Pramode C.E

Introduction to Functional Programming with Ocaml
Pure functions and the benet of local reasoning

If your function modies an object which is accessible from
many other functions, the eect of calling the function is much
more complex to analyse because you now have to analyse how
all these other functions get aected by the mutation.
Similarly, if the value computed by your function depends on
the value of an object which can be modied by many other
functions, it no longer becomes possible to reason about the
working of the function by only looking at the way the
function's parameters are manipulated.
The evaluation of pure functions can be done by a very simple
process of substitution.

Pramode C.E

Introduction to Functional Programming with Ocaml
Mutability and concurrency

Multi core CPU's are becoming commonplace
We need concurrency in our code to make eective use of the
many cores
This throws up a whole bunch of complex problems
A function can no longer assume that nobody else is watching
when it is happily mutating some data

Pramode C.E

Introduction to Functional Programming with Ocaml
Mutability and Concurrency

current_date = [2014, January, 7]
def changeDate(year, month, day):
current_date[0] = year
current_date[1] = month
current_date[2] = day
def showDate():
print current_date

Pramode C.E

Introduction to Functional Programming with Ocaml
Mutability and Concurrency
What happens if the functions changeDate() and showDate() run
as two independent threads on two CPU cores? Will showDate()
always see a consistent date value?
The traditional approach to maintaining correctness in the context
of multithreading is to use locks - but people who do it in practice
will tell you it is extremely tricky business.
It is claimed that one of the reasons for the resurgence of FP is the
emergence of multi-core processors and concurrency - it seems like
FP's emphasis on pure functions and immutability is a good match
for concurrent programming.

Pramode C.E

Introduction to Functional Programming with Ocaml
Moving ahead ...
Prof. Gregor Kiczales Introduction to Systematic Program
Design is a great online course for beginners:
https://www.coursera.org/course/programdesign

Join Prof.Grossman's programming languages class on
Coursera: https://www.coursera.org/course/proglang
and learn more about functional programming using SML and
Racket
Join Prof.Odersky's Functional Programming with Scala
course on Coursera for an introduction to both Scala and FP:
https://www.coursera.org/course/progfun

Watch the classic SICP lectures by Abelson and Sussman:
http://groups.csail.mit.edu/mac/classes/6.001/
abelson-sussman-lectures/

A talk by Yaron Minsky of Jane Street on how to program
eectively in ML: http://vimeo.com/14313378
Pramode C.E

Introduction to Functional Programming with Ocaml
Books (not limited to Ocaml)

Ocaml from the very beginning is a gentle introduction to
the language suitable for beginners:
http://ocaml-book.com/

Real world Ocaml is the book which will take you to Ocaml
mastery: http://realworldocaml.org
SICP, the classic: http://mitpress.mit.edu/sicp/
HTDP: http://www.ccs.neu.edu/home/matthias/HtDP2e/
CTM: http://www.info.ucl.ac.be/~pvr/book.html

Pramode C.E

Introduction to Functional Programming with Ocaml
Other resources (not limited to Ocaml)
Ocaml for the masses - ACM Queue paper by Yaron Minsky
http://queue.acm.org/detail.cfm?id=2038036

Out of the tar pit - paper by Ben Mosely and Peter Marks
(google it)
Lectures by Eric Meijer: google for C9 Lectures Erik Meijer
functional
Persistent Data Structures and Managed References - talk
by Rich Hickey (author of the Clojure programming language).
http://www.infoq.com/presentations/
Value-Identity-State-Rich-Hickey

Can programming be liberated from the von Neumann style
- by John Backus. http://www.thocp.net/biographies/
papers/backus_turingaward_lecture.pdf

Pramode C.E

Introduction to Functional Programming with Ocaml

More Related Content

What's hot

Functional Programming Patterns for the Pragmatic Programmer
Functional Programming Patterns for the Pragmatic ProgrammerFunctional Programming Patterns for the Pragmatic Programmer
Functional Programming Patterns for the Pragmatic ProgrammerRaúl Raja Martínez
 
Procedural Programming: It’s Back? It Never Went Away
Procedural Programming: It’s Back? It Never Went AwayProcedural Programming: It’s Back? It Never Went Away
Procedural Programming: It’s Back? It Never Went AwayKevlin Henney
 
All Aboard The Scala-to-PureScript Express!
All Aboard The Scala-to-PureScript Express!All Aboard The Scala-to-PureScript Express!
All Aboard The Scala-to-PureScript Express!John De Goes
 
First-Class Patterns
First-Class PatternsFirst-Class Patterns
First-Class PatternsJohn De Goes
 
The Design of the Scalaz 8 Effect System
The Design of the Scalaz 8 Effect SystemThe Design of the Scalaz 8 Effect System
The Design of the Scalaz 8 Effect SystemJohn De Goes
 
The Easy-Peasy-Lemon-Squeezy, Statically-Typed, Purely Functional Programming...
The Easy-Peasy-Lemon-Squeezy, Statically-Typed, Purely Functional Programming...The Easy-Peasy-Lemon-Squeezy, Statically-Typed, Purely Functional Programming...
The Easy-Peasy-Lemon-Squeezy, Statically-Typed, Purely Functional Programming...John De Goes
 
Scala - where objects and functions meet
Scala - where objects and functions meetScala - where objects and functions meet
Scala - where objects and functions meetMario Fusco
 
Demystifying functional programming with Scala
Demystifying functional programming with ScalaDemystifying functional programming with Scala
Demystifying functional programming with ScalaDenis
 
Refactoring to Immutability
Refactoring to ImmutabilityRefactoring to Immutability
Refactoring to ImmutabilityKevlin Henney
 
Java 7, 8 & 9 - Moving the language forward
Java 7, 8 & 9 - Moving the language forwardJava 7, 8 & 9 - Moving the language forward
Java 7, 8 & 9 - Moving the language forwardMario Fusco
 
Functional Programming With Scala
Functional Programming With ScalaFunctional Programming With Scala
Functional Programming With ScalaKnoldus Inc.
 
Scala is java8.next()
Scala is java8.next()Scala is java8.next()
Scala is java8.next()daewon jeong
 
One Monad to Rule Them All
One Monad to Rule Them AllOne Monad to Rule Them All
One Monad to Rule Them AllJohn De Goes
 
Halogen: Past, Present, and Future
Halogen: Past, Present, and FutureHalogen: Past, Present, and Future
Halogen: Past, Present, and FutureJohn De Goes
 
Type Classes in Scala and Haskell
Type Classes in Scala and HaskellType Classes in Scala and Haskell
Type Classes in Scala and HaskellHermann Hueck
 
Lambda? You Keep Using that Letter
Lambda? You Keep Using that LetterLambda? You Keep Using that Letter
Lambda? You Keep Using that LetterKevlin Henney
 

What's hot (20)

Functional Programming Patterns for the Pragmatic Programmer
Functional Programming Patterns for the Pragmatic ProgrammerFunctional Programming Patterns for the Pragmatic Programmer
Functional Programming Patterns for the Pragmatic Programmer
 
MTL Versus Free
MTL Versus FreeMTL Versus Free
MTL Versus Free
 
Procedural Programming: It’s Back? It Never Went Away
Procedural Programming: It’s Back? It Never Went AwayProcedural Programming: It’s Back? It Never Went Away
Procedural Programming: It’s Back? It Never Went Away
 
All Aboard The Scala-to-PureScript Express!
All Aboard The Scala-to-PureScript Express!All Aboard The Scala-to-PureScript Express!
All Aboard The Scala-to-PureScript Express!
 
First-Class Patterns
First-Class PatternsFirst-Class Patterns
First-Class Patterns
 
Comparing JVM languages
Comparing JVM languagesComparing JVM languages
Comparing JVM languages
 
Clojure basics
Clojure basicsClojure basics
Clojure basics
 
The Design of the Scalaz 8 Effect System
The Design of the Scalaz 8 Effect SystemThe Design of the Scalaz 8 Effect System
The Design of the Scalaz 8 Effect System
 
The Easy-Peasy-Lemon-Squeezy, Statically-Typed, Purely Functional Programming...
The Easy-Peasy-Lemon-Squeezy, Statically-Typed, Purely Functional Programming...The Easy-Peasy-Lemon-Squeezy, Statically-Typed, Purely Functional Programming...
The Easy-Peasy-Lemon-Squeezy, Statically-Typed, Purely Functional Programming...
 
Scala - where objects and functions meet
Scala - where objects and functions meetScala - where objects and functions meet
Scala - where objects and functions meet
 
Demystifying functional programming with Scala
Demystifying functional programming with ScalaDemystifying functional programming with Scala
Demystifying functional programming with Scala
 
Refactoring to Immutability
Refactoring to ImmutabilityRefactoring to Immutability
Refactoring to Immutability
 
Kotlin, why?
Kotlin, why?Kotlin, why?
Kotlin, why?
 
Java 7, 8 & 9 - Moving the language forward
Java 7, 8 & 9 - Moving the language forwardJava 7, 8 & 9 - Moving the language forward
Java 7, 8 & 9 - Moving the language forward
 
Functional Programming With Scala
Functional Programming With ScalaFunctional Programming With Scala
Functional Programming With Scala
 
Scala is java8.next()
Scala is java8.next()Scala is java8.next()
Scala is java8.next()
 
One Monad to Rule Them All
One Monad to Rule Them AllOne Monad to Rule Them All
One Monad to Rule Them All
 
Halogen: Past, Present, and Future
Halogen: Past, Present, and FutureHalogen: Past, Present, and Future
Halogen: Past, Present, and Future
 
Type Classes in Scala and Haskell
Type Classes in Scala and HaskellType Classes in Scala and Haskell
Type Classes in Scala and Haskell
 
Lambda? You Keep Using that Letter
Lambda? You Keep Using that LetterLambda? You Keep Using that Letter
Lambda? You Keep Using that Letter
 

Viewers also liked

すべてをRacketに取り込もう! ~Racket FFI と Package システムの使い方~
すべてをRacketに取り込もう! ~Racket FFI と Package システムの使い方~すべてをRacketに取り込もう! ~Racket FFI と Package システムの使い方~
すべてをRacketに取り込もう! ~Racket FFI と Package システムの使い方~Kazuhiro Hishinuma
 
Using functional programming within an industrial product group: perspectives...
Using functional programming within an industrial product group: perspectives...Using functional programming within an industrial product group: perspectives...
Using functional programming within an industrial product group: perspectives...Anil Madhavapeddy
 
Camomile : A Unicode library for OCaml
Camomile : A Unicode library for OCamlCamomile : A Unicode library for OCaml
Camomile : A Unicode library for OCamlYamagata Yoriyuki
 
Mirage: ML kernels in the cloud (ML Workshop 2010)
Mirage: ML kernels in the cloud (ML Workshop 2010)Mirage: ML kernels in the cloud (ML Workshop 2010)
Mirage: ML kernels in the cloud (ML Workshop 2010)Anil Madhavapeddy
 
An Introduction to Functional Programming using Haskell
An Introduction to Functional Programming using HaskellAn Introduction to Functional Programming using Haskell
An Introduction to Functional Programming using HaskellMichel Rijnders
 
Introduction to haskell
Introduction to haskellIntroduction to haskell
Introduction to haskellLuca Molteni
 
OCamlでWebアプリケーションを作るn個の方法
OCamlでWebアプリケーションを作るn個の方法OCamlでWebアプリケーションを作るn個の方法
OCamlでWebアプリケーションを作るn個の方法Hiroki Mizuno
 
Os Peytonjones
Os PeytonjonesOs Peytonjones
Os Peytonjonesoscon2007
 
OCaml Labs introduction at OCaml Consortium 2012
OCaml Labs introduction at OCaml Consortium 2012OCaml Labs introduction at OCaml Consortium 2012
OCaml Labs introduction at OCaml Consortium 2012Anil Madhavapeddy
 
Real World OCamlを読んでLispと協調してみた
Real World OCamlを読んでLispと協調してみたReal World OCamlを読んでLispと協調してみた
Real World OCamlを読んでLispと協調してみたblackenedgold
 
関数型プログラミング入門 with OCaml
関数型プログラミング入門 with OCaml関数型プログラミング入門 with OCaml
関数型プログラミング入門 with OCamlHaruka Oikawa
 
PythonistaがOCamlを実用する方法
PythonistaがOCamlを実用する方法PythonistaがOCamlを実用する方法
PythonistaがOCamlを実用する方法Yosuke Onoue
 
Neural Turing Machine Tutorial
Neural Turing Machine TutorialNeural Turing Machine Tutorial
Neural Turing Machine TutorialMark Chang
 
Haskell for the Real World
Haskell for the Real WorldHaskell for the Real World
Haskell for the Real WorldBryan O'Sullivan
 

Viewers also liked (20)

Ocaml
OcamlOcaml
Ocaml
 
すべてをRacketに取り込もう! ~Racket FFI と Package システムの使い方~
すべてをRacketに取り込もう! ~Racket FFI と Package システムの使い方~すべてをRacketに取り込もう! ~Racket FFI と Package システムの使い方~
すべてをRacketに取り込もう! ~Racket FFI と Package システムの使い方~
 
Using functional programming within an industrial product group: perspectives...
Using functional programming within an industrial product group: perspectives...Using functional programming within an industrial product group: perspectives...
Using functional programming within an industrial product group: perspectives...
 
Camomile : A Unicode library for OCaml
Camomile : A Unicode library for OCamlCamomile : A Unicode library for OCaml
Camomile : A Unicode library for OCaml
 
Mirage: ML kernels in the cloud (ML Workshop 2010)
Mirage: ML kernels in the cloud (ML Workshop 2010)Mirage: ML kernels in the cloud (ML Workshop 2010)
Mirage: ML kernels in the cloud (ML Workshop 2010)
 
Haskell - Functional Programming
Haskell - Functional ProgrammingHaskell - Functional Programming
Haskell - Functional Programming
 
An Introduction to Functional Programming using Haskell
An Introduction to Functional Programming using HaskellAn Introduction to Functional Programming using Haskell
An Introduction to Functional Programming using Haskell
 
計算数学
計算数学計算数学
計算数学
 
Lispmeetup11
Lispmeetup11Lispmeetup11
Lispmeetup11
 
Introduction to haskell
Introduction to haskellIntroduction to haskell
Introduction to haskell
 
OCamlでWebアプリケーションを作るn個の方法
OCamlでWebアプリケーションを作るn個の方法OCamlでWebアプリケーションを作るn個の方法
OCamlでWebアプリケーションを作るn個の方法
 
Os Peytonjones
Os PeytonjonesOs Peytonjones
Os Peytonjones
 
OCaml Labs introduction at OCaml Consortium 2012
OCaml Labs introduction at OCaml Consortium 2012OCaml Labs introduction at OCaml Consortium 2012
OCaml Labs introduction at OCaml Consortium 2012
 
Real World OCamlを読んでLispと協調してみた
Real World OCamlを読んでLispと協調してみたReal World OCamlを読んでLispと協調してみた
Real World OCamlを読んでLispと協調してみた
 
関数型プログラミング入門 with OCaml
関数型プログラミング入門 with OCaml関数型プログラミング入門 with OCaml
関数型プログラミング入門 with OCaml
 
PythonistaがOCamlを実用する方法
PythonistaがOCamlを実用する方法PythonistaがOCamlを実用する方法
PythonistaがOCamlを実用する方法
 
Why Haskell
Why HaskellWhy Haskell
Why Haskell
 
Neural Turing Machine Tutorial
Neural Turing Machine TutorialNeural Turing Machine Tutorial
Neural Turing Machine Tutorial
 
Object-oriented Basics
Object-oriented BasicsObject-oriented Basics
Object-oriented Basics
 
Haskell for the Real World
Haskell for the Real WorldHaskell for the Real World
Haskell for the Real World
 

Similar to Introduction to functional programming using Ocaml

Introduction to Functional Programming with Scala
Introduction to Functional Programming with ScalaIntroduction to Functional Programming with Scala
Introduction to Functional Programming with Scalapramode_ce
 
Stdlib functions lesson
Stdlib functions lessonStdlib functions lesson
Stdlib functions lessonteach4uin
 
documents.pub_new-features-in-java-8-it-jpoialjavanaitedwien15java8pdf-java-8...
documents.pub_new-features-in-java-8-it-jpoialjavanaitedwien15java8pdf-java-8...documents.pub_new-features-in-java-8-it-jpoialjavanaitedwien15java8pdf-java-8...
documents.pub_new-features-in-java-8-it-jpoialjavanaitedwien15java8pdf-java-8...Akaks
 
openMP loop parallelization
openMP loop parallelizationopenMP loop parallelization
openMP loop parallelizationAlbert DeFusco
 
Python High Level Functions_Ch 11.ppt
Python High Level Functions_Ch 11.pptPython High Level Functions_Ch 11.ppt
Python High Level Functions_Ch 11.pptAnishaJ7
 
Столпы функционального программирования для адептов ООП, Николай Мозговой
Столпы функционального программирования для адептов ООП, Николай МозговойСтолпы функционального программирования для адептов ООП, Николай Мозговой
Столпы функционального программирования для адептов ООП, Николай МозговойSigma Software
 
PYTHON-PROGRAMMING-UNIT-II (1).pptx
PYTHON-PROGRAMMING-UNIT-II (1).pptxPYTHON-PROGRAMMING-UNIT-II (1).pptx
PYTHON-PROGRAMMING-UNIT-II (1).pptxgeorgejustymirobi1
 
sonam Kumari python.ppt
sonam Kumari python.pptsonam Kumari python.ppt
sonam Kumari python.pptssuserd64918
 
C++ Function
C++ FunctionC++ Function
C++ FunctionHajar
 
Lecture#6 functions in c++
Lecture#6 functions in c++Lecture#6 functions in c++
Lecture#6 functions in c++NUST Stuff
 
Functional Programming in JavaScript
Functional Programming in JavaScriptFunctional Programming in JavaScript
Functional Programming in JavaScriptWill Livengood
 
New Functional Features of Java 8
New Functional Features of Java 8New Functional Features of Java 8
New Functional Features of Java 8franciscoortin
 

Similar to Introduction to functional programming using Ocaml (20)

Introduction to Functional Programming with Scala
Introduction to Functional Programming with ScalaIntroduction to Functional Programming with Scala
Introduction to Functional Programming with Scala
 
Stdlib functions lesson
Stdlib functions lessonStdlib functions lesson
Stdlib functions lesson
 
Wien15 java8
Wien15 java8Wien15 java8
Wien15 java8
 
Monads in Swift
Monads in SwiftMonads in Swift
Monads in Swift
 
documents.pub_new-features-in-java-8-it-jpoialjavanaitedwien15java8pdf-java-8...
documents.pub_new-features-in-java-8-it-jpoialjavanaitedwien15java8pdf-java-8...documents.pub_new-features-in-java-8-it-jpoialjavanaitedwien15java8pdf-java-8...
documents.pub_new-features-in-java-8-it-jpoialjavanaitedwien15java8pdf-java-8...
 
cp05.pptx
cp05.pptxcp05.pptx
cp05.pptx
 
openMP loop parallelization
openMP loop parallelizationopenMP loop parallelization
openMP loop parallelization
 
Python High Level Functions_Ch 11.ppt
Python High Level Functions_Ch 11.pptPython High Level Functions_Ch 11.ppt
Python High Level Functions_Ch 11.ppt
 
Столпы функционального программирования для адептов ООП, Николай Мозговой
Столпы функционального программирования для адептов ООП, Николай МозговойСтолпы функционального программирования для адептов ООП, Николай Мозговой
Столпы функционального программирования для адептов ООП, Николай Мозговой
 
functions
functionsfunctions
functions
 
PYTHON-PROGRAMMING-UNIT-II (1).pptx
PYTHON-PROGRAMMING-UNIT-II (1).pptxPYTHON-PROGRAMMING-UNIT-II (1).pptx
PYTHON-PROGRAMMING-UNIT-II (1).pptx
 
sonam Kumari python.ppt
sonam Kumari python.pptsonam Kumari python.ppt
sonam Kumari python.ppt
 
Lecture 4
Lecture 4Lecture 4
Lecture 4
 
C++ Function
C++ FunctionC++ Function
C++ Function
 
Lecture#6 functions in c++
Lecture#6 functions in c++Lecture#6 functions in c++
Lecture#6 functions in c++
 
Stacks.ppt
Stacks.pptStacks.ppt
Stacks.ppt
 
Stacks.ppt
Stacks.pptStacks.ppt
Stacks.ppt
 
Functional programming in C++
Functional programming in C++Functional programming in C++
Functional programming in C++
 
Functional Programming in JavaScript
Functional Programming in JavaScriptFunctional Programming in JavaScript
Functional Programming in JavaScript
 
New Functional Features of Java 8
New Functional Features of Java 8New Functional Features of Java 8
New Functional Features of Java 8
 

Recently uploaded

AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsMemoori
 
costume and set research powerpoint presentation
costume and set research powerpoint presentationcostume and set research powerpoint presentation
costume and set research powerpoint presentationphoebematthew05
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptxLBM Solutions
 
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024BookNet Canada
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brandgvaughan
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationSafe Software
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...shyamraj55
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024Scott Keck-Warren
 
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Wonjun Hwang
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Enterprise Knowledge
 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024The Digital Insurer
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticscarlostorres15106
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr LapshynFwdays
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Scott Keck-Warren
 
Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024Neo4j
 
Bluetooth Controlled Car with Arduino.pdf
Bluetooth Controlled Car with Arduino.pdfBluetooth Controlled Car with Arduino.pdf
Bluetooth Controlled Car with Arduino.pdfngoud9212
 

Recently uploaded (20)

AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial Buildings
 
costume and set research powerpoint presentation
costume and set research powerpoint presentationcostume and set research powerpoint presentation
costume and set research powerpoint presentation
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptx
 
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brand
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024
 
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping Elbows
 
Vulnerability_Management_GRC_by Sohang Sengupta.pptx
Vulnerability_Management_GRC_by Sohang Sengupta.pptxVulnerability_Management_GRC_by Sohang Sengupta.pptx
Vulnerability_Management_GRC_by Sohang Sengupta.pptx
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024
 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
 
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptxE-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024
 
Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024
 
Bluetooth Controlled Car with Arduino.pdf
Bluetooth Controlled Car with Arduino.pdfBluetooth Controlled Car with Arduino.pdf
Bluetooth Controlled Car with Arduino.pdf
 

Introduction to functional programming using Ocaml

  • 1. Introduction to Functional Programming with Ocaml Pramode C.E http://pramode.net January 30, 2014
  • 2. Workshop Plan Here is what we will do: Learn some Ocaml syntax and write simple Ocaml programs Learn important concepts like: closures, higher order functions, purity, lazy vs strict evaluation, currying, tail calls/TCO, immutability, persistent data structures, type inference etc! Understand the essence of the "Functional Programming" paradigm through Ocaml Not beginner-level stu - but I will try to keep things as simple as possible! Pramode C.E Introduction to Functional Programming with Ocaml
  • 3. Ocaml - some background info Written in 1996 by Xavier Leroy and other researchers at INRIA, France A member of the ML family http://en.wikipedia.org/wiki/ML_(programming_language) Supports Functional, Imperative and Object oriented programming A powerful static type system with type inference Major inuence behind Microsoft F# Recent developments: the Core standard library (by Jane Street) and realworldocaml.org Pramode C.E Introduction to Functional Programming with Ocaml
  • 4. Pramode C.E Introduction to Functional Programming with Ocaml
  • 5. Where is it being used? List of companies: http://ocaml.org/learn/companies.html Facebook - pf, a tool for analysing PHP source Citrix - Uses Ocaml in XenServer, a virtualization system Jane Street - develops trading systems in Ocaml Bloomberg - uses Ocaml for developing nancial derivatives risk management applications Pramode C.E Introduction to Functional Programming with Ocaml
  • 6. The Ocaml REPL Invoke the REPL with the utop command at the shell prompt: utop # print_string hellon;; hello - : unit = () utop # 2 * 3;; - : int = 6 utop # let x = 10;; val x : int = 10 utop # x * 2;; - : int = 20 utop # let y = 5 in y * 8;; - : int = 40 utop # Question: what is the dierence between let and let ... in? Pramode C.E Introduction to Functional Programming with Ocaml
  • 7. Function Denition let add a b = a + b let m = add 10 20 We dene add to be a function which accepts two parameters of type int and returns a value of type int. Similarly, m is dened as a variable of type int. But wait ... we haven't actually dened all these types! Pramode C.E Introduction to Functional Programming with Ocaml
  • 8. Type Inference We have NOT specied the return type of the function or the type of the variable m. Ocaml infers that (the technical term is HindleyMilner type inference) This is the type which utop shows us: val add : int - int - int Read this as: function takes two int's as arguments and returns an int. Pramode C.E Introduction to Functional Programming with Ocaml
  • 9. Static vs Dynamic typing #Python - dynamic typing def foo(x): return x/2 /* C - static typing with NO type inference */ int foo(int x) { return x / 2; } (* ML - static typing with type inference *) let foo x = x / 2 Pramode C.E Introduction to Functional Programming with Ocaml
  • 10. Expression Oriented Programming let i = 0 let p = if (i 0) then -1 else -2 let q = if (true) then hello else world Unlike languages like C/Java, almost everything in Ocaml is an expression, ie, something which computes a value! Rather than programming with statements, we program with expressions. Pramode C.E Introduction to Functional Programming with Ocaml
  • 11. Recursion (* compute sum of first n numbers *) let rec sum n = if (n = 0) then 0 else n + sum (n - 1) let r = sum 10 Try calling the function sum with a large number (say 1000000) as parameter! You get a stack overow! Pramode C.E Introduction to Functional Programming with Ocaml
  • 12. Tail Calls and TCO (* compute sum of first n numbers *) let rec sum n acc = if (n = 0) then acc else sum (n - 1) (acc + n) let r = sum 10 0 This is a tail-recursive version of the previous function - the Ocaml compiler converts the tail call to a loop, thereby avoiding stack overow! Pramode C.E Introduction to Functional Programming with Ocaml
  • 13. Tail Calls and TCO (sum 4) (4 + sum 3) (4 + (3 + sum 2)) (4 + (3 + (2 + sum 1))) (4 + (3 + (2 + (1 + sum 0)))) (4 + (3 + (2 + (1 + 0)))) (4 + (3 + (2 + 1))) (4 + (3 + 3)) (4 + 6) (10) -----------------------------(sum 4 0) (sum 3 4) (sum 2 7) (sum 1 9) (sum 0 10) (10) Pramode C.E Introduction to Functional Programming with Ocaml
  • 14. Parametric polymorphism let foo x = x Our function foo is the identity function; it takes in a value and returns the same! What is the inferred type of this function? More examples: let foo x y = x let foo2 x y = if true then x else y let foo3 x y = if x then y else y let foo4 x y = if x then y else x Pramode C.E Introduction to Functional Programming with Ocaml
  • 15. Higher order functions let double x = 2*x let triple x = 3*x let apply f x = f x let p = apply double 10 let q = apply triple 10 What is type of apply? Pramode C.E Introduction to Functional Programming with Ocaml
  • 16. Summation once again! let sqr x = x * x let cube x = x * x * x let rec sumSimple a b = if a = b then a else a + sumSimple (a + 1) b let rec sumSquares a b = if a = b then (sqr a) else (sqr a) + sumSquares (a + 1) b let rec sumCubes a b = if a = b then (cube a) else (cube a) + sumCubes (a + 1) b Pramode C.E Introduction to Functional Programming with Ocaml
  • 17. Summation - a more elegant method let identity x = x let sqr x = x * x let cube x = x * x * x let rec sum f a b = if a = b then (f a) else (f a) + sum f (a + 1) b let p = sum identity 0 4 let q = sum sqr 0 4 let r = sum cube 0 4 Pramode C.E Introduction to Functional Programming with Ocaml
  • 18. Anonymous functions let apply f x = f x let p = apply (fun x - 2*x) 10 We can create anonymous functions on-the-y! fun x - 2*x is a function which takes an x and returns 2*x. Pramode C.E Introduction to Functional Programming with Ocaml
  • 19. Methods on collections: Map/Filter/Reduce open Core.Std let a = [1;2;3;4] let p = List.map a (fun x - x * x) let q = List.filter a (fun x - (x mod 2) = 0) let r = List.reduce a (fun x y - x + y) Map applies a function on all elements of a sequence. Filter selects a set of values from a sequence based on the boolean value returned by a function passed as its parameter - both functions return a new sequence. Reduce combines the elements of a sequence into a single element. Pramode C.E Introduction to Functional Programming with Ocaml
  • 20. More methods on collections open Core.Std let even x = (x mod 2) = 0 let a = [1;2;3;4;5;6;7] let b = [2;4;6;5;10;11;13;12] let let let let c d e f = = = = List.for_all a even List.exists a even List.take_while a even List.partition_tf a even Pramode C.E Introduction to Functional Programming with Ocaml
  • 21. Nested functions / functions returning functions (* foo returns sqr *) let foo () = let sqr x = x * x in sqr let f = foo () let g = f 10 Pramode C.E Introduction to Functional Programming with Ocaml
  • 22. Lexical Closure let foo1 x = let foo2 y = x + y in foo2 let f = foo1 10 let r = f 20 Pramode C.E Introduction to Functional Programming with Ocaml
  • 23. Lexical Closure The function foo1 returns a closure. A closure is a function which carries with it references to the environment in which it was dened. When we call f(20), the foo2 function gets executed with the environment it had when it was dened - in this environment, the value of x is 10. Pramode C.E Introduction to Functional Programming with Ocaml
  • 24. Simple closure examples let sqr x = x * x let cube x = x * x * x let compose f g = fun x - f (g x) let f = compose sqr cube let m = f 2 Pramode C.E Introduction to Functional Programming with Ocaml
  • 25. Simple closure examples open Core.Std let remove_low_scores xs threshold = List.filter xs (fun score - score = threshold) let m = remove_low_scores [20; 35; 80; 92; 40; 98] 50 The anonymous function fun score - score = threshold is the closure here. How do you know that it is a closure? Its body uses a variable threshold which is not in its local environment (the local environment, in this case, is the parameter list consisting of a single parameter score) Pramode C.E Introduction to Functional Programming with Ocaml
  • 26. Currying and Partial Application Here is the denition from Wikipedia: In mathematics and computer science, currying is the technique of transforming a function that takes multiple arguments (or a tuple of arguments) in such a way that it can be called as a chain of functions, each with a single argument. It was originated by Moses Schonnkel and later re-discovered by Haskell Curry. Pramode C.E Introduction to Functional Programming with Ocaml
  • 27. Currying and Partial Application let add x y = x + y let f = add 10 let g = f 20 The type of add is int - int - int. The correct way to read this is: add takes an int as argument and returns a function which maps an int to an int. Pramode C.E Introduction to Functional Programming with Ocaml
  • 28. Currying and Partial Application let add x y z = x + y + z let f = add 10 let g = f 20 let h = g 30 Pramode C.E Introduction to Functional Programming with Ocaml
  • 29. Algebraic Data Types - Product types let a = (1, 2) (* type: int*int *) let b = (hello, 10, 2) (* type: string*int*int *) An Algebraic Data Type is a composite type - a type formed by combining other types. Two classes of algebraic data types are the product and sum types. Pramode C.E Introduction to Functional Programming with Ocaml
  • 30. Algebraic Data Types - Sum types type shape = Circle of int | Square of int | Rectangle of (int*int) let a = Circle 10 (* circle of radius 10 *) let b = Square 20 (* square with sides 20 *) let c = Rectangle (5,10) (* rectangle with sides 5 and 10 *) The type shape describes a shape which can be one of Circle, Square or Rectangle. Pramode C.E Introduction to Functional Programming with Ocaml
  • 31. Pattern Matching let a = (1, (2, hello)) let (x, (y, z)) = a Pattern matching helps you pull apart complex datastructures and analyse their components in an elegant way (not to be confused with regular expression based pattern matching which is a completely dierent thing). Pramode C.E Introduction to Functional Programming with Ocaml
  • 32. Pattern Matching type shape = Circle of float | Square of float | Rectangle of (float*float) let area s = match s with | Circle r - 3.14 *. r *. r | Square x - x *. x | Rectangle (x, y) - x *. y let a = area (Circle 10.0) let b = area (Square 20.0) let c = area (Rectangle (5.0,10.0)) Pramode C.E Introduction to Functional Programming with Ocaml
  • 33. Pattern Matching type shape = Circle of float | Square of float | Rectangle of (float*float) let area s = match s with | Circle r - 3.14 *. r *. r | Square x - x *. x let a = area (Circle 10.0) let b = area (Square 20.0) let c = area (Rectangle (5.0,10.0)) The compiler warns us when it detects that the match is not exhaustive. Pramode C.E Introduction to Functional Programming with Ocaml
  • 34. Pattern Matching let rec len xs = match xs with | [] - 0 | hd :: tl - 1 + len tl let a = len [1;2;3;4] A simple recursive routine for computing length of a list. If list is empty, length is 0, otherwise, it is 1 + length of remaining part of the list. Pramode C.E Introduction to Functional Programming with Ocaml
  • 35. Immutability and the nature of variables let a = 1 let foo x = x + a; a = 2 (* simply compares a with 2 *) let a = 0 (* earlier binding gets shadowed *) let m = foo 10 (* result is 11 *) let bindings are immutable; you can shadow a binding, but an earlier binding can't be mutated. Pramode C.E Introduction to Functional Programming with Ocaml
  • 36. Immutability Let's look at some Python list manipulation code: a = [20, 30, 40, 50] a.insert(0, 10) print a # prints [10, 20, 30, 40, 50] Python lists are mutable. The insert operation modies the original list. Pramode C.E Introduction to Functional Programming with Ocaml
  • 37. Immutability Here is a similar program in Ocaml: let a = [20;30;40;50] let b = 10::a (* a remains unchanged *) let c = [60;70;80] let d = a @ c (* a remains unchanged *) Good Ocaml style encourages use immutable data structures. Pramode C.E Introduction to Functional Programming with Ocaml
  • 38. Immutability: Structure sharing This is one way to implement 4::b - but it requires creating a full copy of the old list b (red-colored cells 1, 2, 3 are copies of cells containing the same values in b) and adding 4 at the head position. Ocaml does NOT do it this way because it is very inecient. Pramode C.E Introduction to Functional Programming with Ocaml
  • 39. Immutability: Structure sharing This is how Ocaml actually does 4::b. Note that the new list shares all its elements (except 4) with the old list! Pramode C.E Introduction to Functional Programming with Ocaml
  • 40. Immutability: Structure sharing This is what happens when you append two lists A and B (using @ in Ocaml). Note that the appending operation needs to copy the whole of list A. Pramode C.E Introduction to Functional Programming with Ocaml
  • 41. Immutability: Structure sharing The structure sharing idea can be used eciently with tree data structures. For more info: http: //en.wikipedia.org/wiki/Persistent_data_structure Persistent vectors in Scala: http://www.codecommit.com/blog/scala/ implementing-persistent-vectors-in-scala Pramode C.E Introduction to Functional Programming with Ocaml
  • 42. Mutability and aliasing a = [1,2,3,4] b = a b.append(5) print a Having aliases to mutable objects can create very hard to track down bugs in our code. Pramode C.E Introduction to Functional Programming with Ocaml
  • 43. Non-strict evaluation let my_if cond then_part else_part = if cond then then_part else else_part let a = my_if (1 2) 10 20 We are trying to write a function which behaves similar to the built-in if control structure in Ocaml ... does it really work properly? Let's try another example! Pramode C.E Introduction to Functional Programming with Ocaml
  • 44. Non-strict evaluation let foo1 () = print_string foo1n; 10 let foo2 () = print_string foo2n; 20 let my_if cond then_part else_part = if cond then then_part else else_part let a = my_if (1 2) (foo1()) (foo2()) Pramode C.E Introduction to Functional Programming with Ocaml
  • 45. Non-strict evaluation The behaviour of if is non-strict: In the expression if (cond) then e1 else e2, if cond is true e2 is NOT EVALUATED. Also, if cond is false, e1 is NOT EVALUATED. By default, the behaviour of function calls in Ocaml is strict: In the expression fun e1 e2 ... en, ALL the expressions e1, e2 ... en are evaluated before the function is called. Pramode C.E Introduction to Functional Programming with Ocaml
  • 46. Non-strict evaluation let foo1 () = print_string foo1n; 10 let foo2 () = print_string foo2n; 20 let my_if cond then_part else_part = if cond then (then_part()) else (else_part()) let a = my_if (1 2) (fun () - (foo1())) (fun () - (foo2())) Pramode C.E Introduction to Functional Programming with Ocaml
  • 47. Referential Transparency If an expression can be replaced by its value without changing the behaviour of the program, it is said to be referentially transparent All occurrences of the expression 1+(2*3) can be replaced by 7 without changing the behaviour of the program. Say the variable x (in a C program) has initial value 5. It is NOT possible to replace all occurrences of the statement ++x with the value 6. Pramode C.E Introduction to Functional Programming with Ocaml
  • 48. Pure Functions #include stdio.h int balance = 1000; int withdraw(int amt) { balance = balance - amt; return balance; } int main() { printf(%dn, withdraw(100)); printf(%dn, withdraw(100)); return 0; } Pramode C.E Introduction to Functional Programming with Ocaml
  • 49. Pure Functions A pure function always computes the same value given the same parameters; for example, sin(0) is always 0. It is Referentially Transparent. Evaluation of a pure function does not cause any observable side eects or output - like mutation of global variables or output to I/O devices. Pramode C.E Introduction to Functional Programming with Ocaml
  • 50. What is Functional Programming? A style of programming which emphasizes composing your program out of PURE functions and immutable data. Theoretical foundation based on Alonzo Church's Lambda Calculus In order for this style to be eective in the construction of real world programs, we make use of most of the ideas seen so far (higher order functions, lexical closures, currying, immutable and persistent datastructures, lazy evaluation etc) Pramode C.E Introduction to Functional Programming with Ocaml
  • 51. What is Functional Programming? Questions to ask: Is this the silver bullet? How practical is a program composed completely out of pure functions? What are the benets of writing in a functional style? Pramode C.E Introduction to Functional Programming with Ocaml
  • 52. Is FP the silver bullet? Of course, there are NO silver bullets! (http://en.wikipedia.org/wiki/No_Silver_Bullet) Writing software is tough - no one methodology or technique is going to solve all your problems Pramode C.E Introduction to Functional Programming with Ocaml
  • 53. How practical is a program composed completely out of pure functions? Very impractical - unless your aim is to ght the chill by heating up the CPU (which, by the way, is a side eect) The idea is to write your program in such a way that it has a purely functional core, surrounded by a few impure functions at the outer layers Pramode C.E Introduction to Functional Programming with Ocaml
  • 54. Functional core + impure outer layers This example is simple and contrived, but it serves to illustrate the idea. It is taken from the amazing book Functional Programming in Scala by Paul Chiusano and Runar Bjarnason. let declareWinner p = let (name, score) = p in print_string (name ^ n) let winner p1 p2 = let ((_, score1), (_, score2)) = (p1, p2) in if (score1 score2) then (declareWinner p1) else (declareWinner p2) Note that winner is an impure function. We will now refactor it a little! Pramode C.E Introduction to Functional Programming with Ocaml
  • 55. Functional core + impure outer layers let declareWinner p = let (name, score) = p in print_string (name ^ n) let maxScore p1 p2 = let ((_, score1), (_, score2)) = (p1, p2) in if (score1 score2) then p1 else p2 let winner p1 p2 = declareWinner (maxScore p1 p2) Now we have separated the computation from the display logic; maxScore is a pure function and winner is the impure function at the outer layer! Pramode C.E Introduction to Functional Programming with Ocaml
  • 56. Benets of functional style - easy reuse, easy testing What if we wish to nd out the winner among a set of N players? Easy! open Core.Std let maxScore p1 p2 = let ((_, score1), (_, score2)) = (p1, p2) in if (score1 score2) then p1 else p2 let a = [(Ram, 68); (John, 72); (Arun, 57)] let b = List.reduce a maxScore Pramode C.E Introduction to Functional Programming with Ocaml
  • 57. FP as good software engineering Pure functions are easy to re-use as they have no context other than the function parameters. (Think about re-using the winner function in our rst version to compute the winner among N players). Pure functions are also easy to test. (Think about writing an automated test for the winner function in the rst version). Think of FP as Good Software Engineering! Pramode C.E Introduction to Functional Programming with Ocaml
  • 58. Pure functions and the benet of local reasoning If your function modies an object which is accessible from many other functions, the eect of calling the function is much more complex to analyse because you now have to analyse how all these other functions get aected by the mutation. Similarly, if the value computed by your function depends on the value of an object which can be modied by many other functions, it no longer becomes possible to reason about the working of the function by only looking at the way the function's parameters are manipulated. The evaluation of pure functions can be done by a very simple process of substitution. Pramode C.E Introduction to Functional Programming with Ocaml
  • 59. Mutability and concurrency Multi core CPU's are becoming commonplace We need concurrency in our code to make eective use of the many cores This throws up a whole bunch of complex problems A function can no longer assume that nobody else is watching when it is happily mutating some data Pramode C.E Introduction to Functional Programming with Ocaml
  • 60. Mutability and Concurrency current_date = [2014, January, 7] def changeDate(year, month, day): current_date[0] = year current_date[1] = month current_date[2] = day def showDate(): print current_date Pramode C.E Introduction to Functional Programming with Ocaml
  • 61. Mutability and Concurrency What happens if the functions changeDate() and showDate() run as two independent threads on two CPU cores? Will showDate() always see a consistent date value? The traditional approach to maintaining correctness in the context of multithreading is to use locks - but people who do it in practice will tell you it is extremely tricky business. It is claimed that one of the reasons for the resurgence of FP is the emergence of multi-core processors and concurrency - it seems like FP's emphasis on pure functions and immutability is a good match for concurrent programming. Pramode C.E Introduction to Functional Programming with Ocaml
  • 62. Moving ahead ... Prof. Gregor Kiczales Introduction to Systematic Program Design is a great online course for beginners: https://www.coursera.org/course/programdesign Join Prof.Grossman's programming languages class on Coursera: https://www.coursera.org/course/proglang and learn more about functional programming using SML and Racket Join Prof.Odersky's Functional Programming with Scala course on Coursera for an introduction to both Scala and FP: https://www.coursera.org/course/progfun Watch the classic SICP lectures by Abelson and Sussman: http://groups.csail.mit.edu/mac/classes/6.001/ abelson-sussman-lectures/ A talk by Yaron Minsky of Jane Street on how to program eectively in ML: http://vimeo.com/14313378 Pramode C.E Introduction to Functional Programming with Ocaml
  • 63. Books (not limited to Ocaml) Ocaml from the very beginning is a gentle introduction to the language suitable for beginners: http://ocaml-book.com/ Real world Ocaml is the book which will take you to Ocaml mastery: http://realworldocaml.org SICP, the classic: http://mitpress.mit.edu/sicp/ HTDP: http://www.ccs.neu.edu/home/matthias/HtDP2e/ CTM: http://www.info.ucl.ac.be/~pvr/book.html Pramode C.E Introduction to Functional Programming with Ocaml
  • 64. Other resources (not limited to Ocaml) Ocaml for the masses - ACM Queue paper by Yaron Minsky http://queue.acm.org/detail.cfm?id=2038036 Out of the tar pit - paper by Ben Mosely and Peter Marks (google it) Lectures by Eric Meijer: google for C9 Lectures Erik Meijer functional Persistent Data Structures and Managed References - talk by Rich Hickey (author of the Clojure programming language). http://www.infoq.com/presentations/ Value-Identity-State-Rich-Hickey Can programming be liberated from the von Neumann style - by John Backus. http://www.thocp.net/biographies/ papers/backus_turingaward_lecture.pdf Pramode C.E Introduction to Functional Programming with Ocaml