25
Introduction to OCaml Jean-Baptiste Jeannin Department of Computer Science Cornell University CS 6110 Lecture 3rd February 2011 Based on CS 3110 course notes, an SML tutorial by Mike George and an OCaml tutorial by Jed Liu Jean-Baptiste Jeannin Introduction to OCaml 1

Introduction to OCaml - Cornell University

  • Upload
    others

  • View
    9

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Introduction to OCaml - Cornell University

Introduction to OCaml

Jean-Baptiste Jeannin

Department of Computer ScienceCornell University

CS 6110 Lecture3rd February 2011

Based on CS 3110 course notes,an SML tutorial by Mike Georgeand an OCaml tutorial by Jed Liu

Jean-Baptiste Jeannin Introduction to OCaml 1

Page 2: Introduction to OCaml - Cornell University

Installing OCaml

I Follow instructions on the CS3110 website:www.cs.cornell.edu/courses/cs3110/2011sp/

I Windows:http://caml.inria.fr/ocaml/release.en.html

Get the MinGW-based native Win32 port

I Mac OSX:http://caml.inria.fr/ocaml/release.en.html

I Linux:yum install ocaml

apt-get install ocaml

emerge dev-lang/ocaml

Jean-Baptiste Jeannin Introduction to OCaml 1

Page 3: Introduction to OCaml - Cornell University

Which tools to use OCaml?

I Using the toplevel (demo)

I A text editor (Emacs with Tuareg or other) anda shell to compile (demo)

I Eclipse with a plugin

Jean-Baptiste Jeannin Introduction to OCaml 2

Page 4: Introduction to OCaml - Cornell University

Declaring Variables

let sixtyOneTen = 6110

(* A local variable declaration *)

let fortyTwo =

let six = 6

and nine = 7

in six * nine

Jean-Baptiste Jeannin Introduction to OCaml 3

Page 5: Introduction to OCaml - Cornell University

Base Types

let x : int = -7

let y : char = ’a’

let z : string = "moo"

let w : float = 3.14159

let v : bool = true

I OCaml has type inference

I Type declarations are optional in many places

I But having them makes it much easier todebug type errors!

Jean-Baptiste Jeannin Introduction to OCaml 4

Page 6: Introduction to OCaml - Cornell University

Tuples and Datatypes

(* Tuples (a.k.a. product types) *)

let t1 : int * int = (3, 5)

let t2 : string * bool * char = ("moo", true, ’q’)

let t3 : unit = () (* The empty tuple *)

(* A simple datatype (like enum or union) *)

type suit = Spades | Diamonds | Hearts | Clubs

let c : suit = Clubs

Jean-Baptiste Jeannin Introduction to OCaml 5

Page 7: Introduction to OCaml - Cornell University

More Datatypes

(* Datatype constructors can carry values *)

(* and be recursive (and look like CFGs) *)

type var = string

type exp = Var of var

| Lam of var * exp

| App of exp * exp

let id : exp = Lam ("x", Var "x")

let w : exp =

App (Lam ("x", App (Var "x", Var "x")),

Lam ("x", App (Var "x", Var "x")))

I Can build up tuples and datatypes...I How to break them down and actually use

them?Jean-Baptiste Jeannin Introduction to OCaml 6

Page 8: Introduction to OCaml - Cornell University

Pattern Matching

let t1 : int * int = ...

(* Binds two variables at once *)

let (a, b) = t1

(* Use _ for "don’t care" *)

let (_, b) = t1

(* Can match constants too *)

let (a, 5) = t1

I OCaml warns about non-exhaustive patterns

Jean-Baptiste Jeannin Introduction to OCaml 7

Page 9: Introduction to OCaml - Cornell University

More Pattern Matching

let suitname : string =

match c with

Spades -> "spades" | Diamonds -> "diamonds"

| Hearts -> "hearts" | Clubs -> "clubs"

(* Base types are just special datatypes *)

(* and can also be pattern-matched *)

let b : bool = ...

let x : int =

match b with

true -> 1

| false -> 0

(* Says the same thing and is better style: *)

let x : int = if b then 1 else 0

Jean-Baptiste Jeannin Introduction to OCaml 8

Page 10: Introduction to OCaml - Cornell University

More Pattern Matching

let suitname : string =

match c with

Spades -> "spades" | Diamonds -> "diamonds"

| Hearts -> "hearts" | Clubs -> "clubs"

(* Base types are just special datatypes *)

(* and can also be pattern-matched *)

let b : bool = ...

let x : int =

match b with

true -> 1

| false -> 0

(* Says the same thing and is better style: *)

let x : int = if b then 1 else 0

Jean-Baptiste Jeannin Introduction to OCaml 8

Page 11: Introduction to OCaml - Cornell University

A Warning about Pattern Matching

(* What does this evaluate to? *)

let pair = (42, 611)

let x = 611

match pair with

(x, 611) -> 0

| (42, x) -> 1

| _ -> 2

I Patterns can refer to datatype constructors andconstants

I But cannot refer to pre-existing variables

I They can only declare new variables

Jean-Baptiste Jeannin Introduction to OCaml 9

Page 12: Introduction to OCaml - Cornell University

A Warning about Pattern Matching

(* What does this evaluate to? *)

let pair = (42, 611)

let x = 611

match pair with

(x, 611) -> 0

| (42, x) -> 1

| _ -> 2

I Patterns can refer to datatype constructors andconstants

I But cannot refer to pre-existing variables

I They can only declare new variables

Jean-Baptiste Jeannin Introduction to OCaml 9

Page 13: Introduction to OCaml - Cornell University

Functions

(* A variable with a function value *)

let square : int -> int =

fun (x:int) -> x * x (* anonymous fun! *)

(* Same thing, more succinct *)

let square (x:int) : int = x * x

Jean-Baptiste Jeannin Introduction to OCaml 10

Page 14: Introduction to OCaml - Cornell University

Recursive Functions

let rec fact (x:int) : int =

if x = 0 then 1

else x * (fact (x-1))

(* Mutually recursive functions *)

let rec isOdd (x:int) : bool =

x != 0 && isEven (x-1)

and isEven (x:int) : bool =

x = 0 || isOdd (x-1)

Jean-Baptiste Jeannin Introduction to OCaml 11

Page 15: Introduction to OCaml - Cornell University

More Functions

(* How many arguments does this take? *)

let rec gcd (a, b) : int =

if b = 0 then a

else gcd (b, a mod b)

(* More explicitly: *)

let rec gcd (p : int * int) : int =

match p with (a, b) ->

if b = 0 then a

else gcd (b, a mod b)

Jean-Baptiste Jeannin Introduction to OCaml 12

Page 16: Introduction to OCaml - Cornell University

More Functions

(* How many arguments does this take? *)

let rec gcd (a, b) : int =

if b = 0 then a

else gcd (b, a mod b)

(* More explicitly: *)

let rec gcd (p : int * int) : int =

match p with (a, b) ->

if b = 0 then a

else gcd (b, a mod b)

Jean-Baptiste Jeannin Introduction to OCaml 12

Page 17: Introduction to OCaml - Cornell University

Curried Functions

let rec gcd (a, b) : int =

if b = 0 then a

else gcd (b, a mod b)

(* Preferred style: *)

let rec gcd’ (a:int) (b:int) : int =

if b = 0 then a

else gcd’ b (a mod b)

(* Has type int -> int -> int *)

(* More explicitly: *)

let rec gcd’ (a:int) : int -> int =

fun (b:int) ->

if b = 0 then a

else gcd’ b (a mod b)

Jean-Baptiste Jeannin Introduction to OCaml 13

Page 18: Introduction to OCaml - Cornell University

A Minor Tangent...

I We havegcd : int * int -> int

gcd’: int -> (int -> int)

I Through currying and uncurrying, these typesare somehow “equivalent”

Jean-Baptiste Jeannin Introduction to OCaml 14

Page 19: Introduction to OCaml - Cornell University

Polymorphism

(* What is this function’s type? *)

let id x = x

(* More explicitly *)

let id (x : ’a) : ’a = x

(* A polymorphic datatype *)

type ’a lst =

Empty

| Cons of (’a * ’a lst)

let rec map (f:’a -> ’b) (l:’a lst) : ’b lst =

match l with

Empty -> Empty

| Cons (hd, tl) -> Cons (f hd, map f tl)

Jean-Baptiste Jeannin Introduction to OCaml 15

Page 20: Introduction to OCaml - Cornell University

Polymorphism

(* What is this function’s type? *)

let id x = x

(* More explicitly *)

let id (x : ’a) : ’a = x

(* A polymorphic datatype *)

type ’a lst =

Empty

| Cons of (’a * ’a lst)

let rec map (f:’a -> ’b) (l:’a lst) : ’b lst =

match l with

Empty -> Empty

| Cons (hd, tl) -> Cons (f hd, map f tl)

Jean-Baptiste Jeannin Introduction to OCaml 15

Page 21: Introduction to OCaml - Cornell University

Lists

I OCaml has lists built-inI [] is the empty listI :: is the cons operatorI @ is the append operatorI [1; 2; 3] is a three-element list

(note the semicolons)

let rec reverse (l : ’a list) : ’a list =

match l with

[] -> []

| hd :: tl -> (reverse tl) @ [hd]

I A fancy list pattern:[a; (42, [611]); (b, c::d)]

Jean-Baptiste Jeannin Introduction to OCaml 16

Page 22: Introduction to OCaml - Cornell University

Local Declarations: a better reverse

(* A better reverse *)

let reverse (l : ’a list) : ’a list =

let rec reverse_aux l1 l2 =

match l1 with

[] -> l2

| h::t -> reverse_aux t (h::l2) in

reverse_aux l []

Jean-Baptiste Jeannin Introduction to OCaml 17

Page 23: Introduction to OCaml - Cornell University

Putting It All Together

I Demo: #use "fv.ml"

Jean-Baptiste Jeannin Introduction to OCaml 18

Page 24: Introduction to OCaml - Cornell University

Summary

I Types, tuples, datatypes

I Pattern matching

I Higher-order functions, anonymous functions,currying

I Polymorphism

Jean-Baptiste Jeannin Introduction to OCaml 19

Page 25: Introduction to OCaml - Cornell University

Resources

I CS 3110 noteshttp://www.cs.cornell.edu/courses/cs3110/2011sp/

I Objective CAML Tutorialhttp://www.ocaml-tutorial.org/

I OCaml manualhttp://caml.inria.fr/pub/docs/manual-ocaml/

Jean-Baptiste Jeannin Introduction to OCaml 20