Download presentation

Presentation is loading. Please wait.

Published byDarryl Noseworthy Modified over 2 years ago

1
Introduction to OCaml Slides prepared by Matt Gruskin Some material borrowed from the CIS 500 lecture notes

2
Functional Programming Persistent (immutable) data structures –No assignments in pure functional programming! Recursion –No loops in pure functional programming! Higher-order functions –Functions that take other functions as arguments or return functions as results OCaml is not a pure functional language –OCaml does have for and while loops, but I won’t show them here

3
Let statements Give a name to an expression or define a function # let pi = 3.1415;; val pi : float = 3.1415 # pi;; -: float = 3.1415 # let circle_area r = pi *. r *. r;; val circle_area : float -> float = # circle_area 4.0;; - : float = 50.264 >>> pi = 3.1415; >>> pi 3.1415000000000002 >>> def circle_area(r):... return pi * r * r;... >>> circle_area(4); 50.264000000000003

4
Syntax differences from Python Method calls –Parameters separated by spaces instead of commas… and no parentheses around parameter list –Python: method(a,b,c) –OCaml: method a b c

5
Type Inference Types are inferred (like Python) –So you don’t have to give parameters types Unlike Python, types are inferred BEFORE runtime –When entering a function in the toplevel you’ll see Param type -> param type -> … -> return type –This is why different operators are needed for different numeric types # let circle_area r = pi *. r *. r;; val circle_area : float -> float =

6
Lists Similar to lists in Python Unlike Python, implemented as linked lists Cons – hd :: tl (hd is first element, tl is a list) Unlike Python, lists are homogeneous –You can do [5,’a’,True] in Python but you can’t do [5;’a’;true] in OCaml # let l = [1;3;5;7];; val l : int list = [1; 3; 5; 7] # l;; - : int list = [1; 3; 5; 7] # [-1;0] @ l;; - : int list = [-1; 0; 1; 3; 5; 7] # List.hd l;; - : int = 1 # List.tl l;; -: int list = [3; 5; 7] # List.nth l 2;; - : int = 5 >>> l = [1,3,5,7] >>> l [1, 3, 5, 7] >>> [-1,0] + l [-1, 0, 1, 3, 5, 7] >>> l[0] 1 >>> l[1:] [3, 5, 7] >>> l[2] 5

7
Recursive functions let rec gcd a b = if a=0 then b else if a>b then gcd b a else gcd (b mod a) a let rec repeat k n = if n=0 then [] else k :: repeat k (n-1) # repeat 1 4;; - : int list = [1; 1; 1; 1] def gcd(a,b): if a == 0: return b if a > b: return gcd(b,a) return gcd(b % a, a) def repeat(k,n): if n == 0: return [] else: return [k] + repeat(k,n-1) >>> repeat(1,4) [1, 1, 1, 1]

8
Pattern Matching Alternative to imperative statements like if, switch let rec gcd a b = match a with 0 -> b | x when x > b -> gcd b a | _ -> gcd (b mod a) a let rec gcd a b = if a=0 then b else if a>b then gcd b a else gcd (b mod a) a

9
Tail Recursion let rec rev l = match l with [] -> [] | hd :: tl -> (rev tl) @ [hd] let rec tailrev l r = match l with [] -> r | hd :: tl -> tailrev tl (hd :: r) let rec tailrev ?(r=[]) l = match l with [] -> r | hd :: tl -> tailrev ~r:(hd :: r) tl def rev(l): if l == []: return [] else: return rev(l[1:]) + [l[0]] def tailrev(l,r=[]): if l == []: return r else: return tailrev(l[1:],[l[0]]+r) Result of recursive call is also result of entire function In OCaml, tail recursion uses constant stack space

10
Functions as parameters # let l = [3;5;7];; val l : int list = [3; 5; 7] # List.map (fun x -> x*2) l;; -: int list = [6; 10; 14] # List.fold_left (fun x y -> x + y) 0 l;; -: int = 15 # List.filter (fun x -> x > 4) l;; - : int list = [5; 7] # List.iter (Printf.printf "%i\n") l;; 3 5 7 : unit = () >>> l = [3,5,7] >>> map(lambda x: x*2,l) [6, 10, 14] >>> [x*2 for x in l] [6, 10, 14] >>> reduce(lambda x,y:x+y,l,0) 15 >>> filter(lambda x: x>4,l) [5, 7] >>> for x in l:... print x... 3 5 7

11
Quicksort PYTHON def sort(a): if a == []: return [] else: pivot = a[0] left = [x for x in a if x < pivot] right = [x for x in a[1:] if x >= pivot] return sort(left) + [pivot] + sort(right) OCaml let rec quicksort l = match l with [] -> [] | pivot :: t -> let left = List.filter (fun x -> x < pivot) t in let right = List.filter (fun x -> x >= pivot) t in (quicksort left) @ [pivot] @ (quicksort right)

12
Palindrome PYTHON def palindrome(s): if len(s) <= 1: return True return s[0] == s[-1] and palindrome(s[1:-1]) OCaml let rec palindrome s = s = (tailrev s)

Similar presentations

OK

CMSC 330: Organization of Programming Languages Tuples, Types, Conditionals and Recursion or “How many different OCaml topics can we cover in a single.

CMSC 330: Organization of Programming Languages Tuples, Types, Conditionals and Recursion or “How many different OCaml topics can we cover in a single.

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google

Ppt on minimum wages act in tamil Ppt on indian automobile industries Ppt on op amp circuits body Ppt on solar system for class 9 Ppt on human chromosomes pictures Training ppt on msds Ppt on forest fire in india Ppt on 21st century skills standards Ppt on financial statement analysis Download ppt on civil disobedience movement gandhi