Download Study Material for Functional Programming with OCaml | CMSC 631 and more Study notes Computer Science in PDF only on Docsity!
CMSC 631
Fall 2007
Functional Programming with OCaml
Background
• 1973 – ML developed at Univ. of Edinburgh
- Part of a theorem proving system LCF
- The Logic of Computable Functions
• SML/NJ (“Standard ML of New Jersey”)
- http://www.smlnj.org
- Developed at Bell Labs and Princeton; now Yale,
AT&T Research, Univ. of Chicago (among others)
• OCaml
- http://www.ocaml.org
- Developed at INRIA (The French National Institute
for Research in Computer Science)
More Information on OCaml
• Translation available
on the course web site
Applications with
Objective Caml
• Web site also has link
to another book
Objective Caml
Programming
Language
Features of ML
- Higher-order functions
- Functions can be parameters and return values
- “Mostly functional”
- Data types and pattern matching
- Convenient for certain kinds of data structures
- Static Type System
- Type inference
- No need to write types in the source language
- Supports parametric polymorphism ( generics in Java, templates
in C++)
- Exceptions
- Garbage Collection
Things to Notice
( This is an OCaml program )
let x = 37;;
let y = x + 5;;
print_int y;;
print_string
"\n";;
Use (* *) for comments (may nest)
;; ends a top-level expression
Use let to bind variables
No type declarations
Need to use correct print function
(OCaml also has printf)
Run, OCaml, Run
• Can compile OCaml programs using ocamlc
- Produces .cmo (“compiled object”) and .cmi
(“compiled interface”) files
- We’ll talk about interface files later
- By default, also links to produce executable a.out
- Use -o to set output file name
- Use -c to compile only to .cmo/.cmi and not to link
- We’ll give you a Makefile if you need to compile your files
Basic Types in OCaml
• Read e : t has “expression e has type t”
42 : int true : bool
"hello" : string 'c' : char
3.14 : float () : unit (* don’t care value *)
• OCaml has static types to help you avoid errors
- Note: Sometimes the messages are a bit confusing
# 1 + true;;
This expression has type bool but is here used with
type int
- Watch for the underline as a hint to what went wrong
- But not always reliable
More on the Let Construct
• let is more often used for local variables
- let x = e1 in e2 means
- Evaluate e
- Then evaluate e2, with x bound to result of evaluating e
- x is not visible “outside” of e
**let pi = 3.14 in pi . 3.0 . 3.0;;
pi;;
Bind pi in body of let
mult. on floating point
Error
Nested Let
• Uses of let can be nested
let pi = 3.14 in
let r = 3.0 in
**pi . r . r;;
( pi, r no longer in scope )
{
float pi = 3.14;
float r = 3.0;
pi * r * r;
}
/ pi, r not in scope /
Defining Functions
let next x = x + 1;;
next 3;;
let plus (x, y) = x + y;;
plus (3, 4);;
Use let to define functions
List arguments after fn name
No return statement No parens on fun calls
Function Types
• In OCaml, -> is the function type constructor
- The type t1 -> t2 is a function with argument or
domain type t1 and return or range type t
• Examples
- let next x = x + 1 ( type int -> int )
- *let foo x = (float_of_int x) . 3.
( type int -> float )
- print_string ( type string -> unit )
• Type in fn name at top level to get type
Type Annotations
• Syntax (e : t) to assert “ e has type t ”
- You can add this anywhere you like
let (x : int) = 3
let z = (x : int) + 5
• Use to give functions param and return types
let foo (x:int):float =
*(float_of_int x) . 3.
- Note special position for return type
- Thus let bar x:int = ... means bar returns int
• Very useful for debugging
- Especially for more complicated types
Lists in OCaml
• The basic data structure in OCaml is the list
- Write down a list as [e1; e2; ...; en]
[1;2;3]
- : int list = [1;2;3]
- Notice int list – lists must be homogeneous
- The empty list is []
[]
- : 'a list
- The 'a means “a list containing anything”
- We’ll see much more about this later
- Warning: Don’t use comma instead of semicolon
- Means something different (we’ll see in a bit)
Lists are Linked
• [1;2;3] is represented above
- A non-empty list is a pair (element, rest of list)
- The element is the head of the list
- The pointer is the tail or rest of the list
- ...which is itself a list!
• Thus in math, a list is either
- The empty list []
- Or, a pair consisting of an element and a list
- This recursive structure will come in handy shortly