Docsity

Functional Programming with OCaml in CMSC 330, Study notes of Programming Languages

Partial preview of the text

Download Functional Programming with OCaml in CMSC 330 and more Study notes Programming Languages in PDF only on Docsity!

CMSC 330: Organization of

Programming Languages

Functional Programming with OCaml

Functional Programming

• Functions: inputs → outputs

• Higher-order functions

– Functions as arguments

– Functions as return values

• Recursion

• Inductive definitions

– Base case(s)

– Recursive case(s)

Background

• Late 1970's – ML (MetaLanguage) developed

– Robin Milner at the University of Edinburgh

– Part of a theorem proving system

– Statically typed

– Fewer parentheses!

• Descendants

– Miranda (1985)

– Caml (developed at INRIA in 1986)

– Standard ML (SML) (1990)

– Haskell (1990)

Dialects of ML

• Many other dialects of ML

– But SML/NJ and OCaml are most popular

– O = “Objective,” but probably won’t cover objects

• Languages all have the same core ideas

– But small and annoying syntactic differences

– So you should not buy a book with ML in the title

  • (^) Because it probably won’t cover OCaml

Functional languages

• In a pure functional language, every program is

just an expression evaluation

let add1 x = x + 1

let rec add (x,y) = if x=0 then y else add(x-1, add1(y))

add(2,3) = add(1,add1(3)) = add(0,add1(add1(3)))

= add1(add1(3)) = add1(3+1) = 3+1+

A Small OCaml Program- Things to Notice

( A small 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)

Line breaks, spacing ignored

Run, OCaml, Run (cont’d)

• Compiling and running the previous small

program:

% ocamlc ocaml1.ml % ./a.out 42 %

( A small OCaml program ) let x = 37;; let y = x + 5;; print_int y;; print_string "\n";;

ocaml1.ml:

Run, OCaml, Run (cont’d)

Expressions can also be typed and evaluated at the top-level:

# 3 + 4;;

- : int = 7

# let x = 37;; val x : int = 37

# x;;

- : int = 37

# let y = 5;; val y : int = 5

# let z = 5 + x;; val z : int = 42

# print_int z;; 42- : unit = ()

# print_string "Colorless green ideas sleep furiously";; Colorless green ideas sleep furiously- : unit = ()

# print_int "Colorless green ideas sleep furiously";; This expression has type string but is here used with type int

gives type and value of each expr

unit = “no interesting value” (like void)

“-” = “the expression you just typed”

Basic Types in OCaml

• Read e : t as “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 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 floating point multiplication

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 parameters after function name

no parentheses on function calls no return statement

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 fn x = (float_of_int x) . 3.

(* type int -> float *)

  • (^) print_string ( type string -> unit )

• Type a function name at top level to get its type

Type Annotations

• The syntax (e : t) asserts that “ e has type t ”

– This can be added anywhere you like

let (x : int) = 3

let z = (x : int) + 5

• Use to give functions parameter and return types

let fn (x:int):float =

(float_of_int x) *. 3.

– Note special position for return type

– Thus let g x:int = ... means g returns int

• Very useful for debugging, especially for more

complicated types