Ocaml Pattern Matching

Ocaml Pattern Matching

OCaml pattern matching Tuples and lists CS235 Languages and Automata Department of Computer Science Wellesley College Lists in Java and OCaml Java OCaml empty() [] preprend(x, ys) x :: ys head(xs) List.hd(xs) tail(xs) List.tl(xs) isEmpty(xs) xs = [] *Note that [exp1; exp2; … ; expn] is an OCaml abbreviation for exp1 :: exp2 :: … :: expn :: []. Pattern matching 8-2 1 Summing an IntList in Java // Returns the integer sum of all element in ns public static int sumList (IntList ns) { if (IntList.isEmpty(ns) { return 0; } else { return IntList.head(ns) + sum(IntList.tail(ns));; } } s1 91 0 -22 Ø Pattern matching 8-3 Summing an integer list in OCaml # let rec sum ns = if ns = [] then 0 else List.hd(ns) + (sum (List.tl ns));; Pattern matching 8-4 2 Summing an integer list in OCaml # let rec sum ns = match ns with [] -> 0 | (n :: ns’) -> n + sum(ns’);; # sum [];; # sum [3];; # sum [3;2;7;5];; Pattern matching 8-5 range lo hi # range 3 7;; _: int list = [3;4;5;6;7] # range 5 5;; _: int list = [5] # range 6 5;; _: int list = [] *Returns a list of integers from lo up to hi, inclusive. The list is empty is lo > hi. Pattern matching 8-6 3 squares ns # squares [3;1;5;4;2];; _: int list = [9; 1; 25; 16; 4] # squares [5];; _: int list = [25] # squares [];; _: int list = [] # squares (range 3 6);; _: int list = [9; 16; 25; 36] Pattern matching 8-7 map f # let square x = x*x;; val square : int -> int = <fun> # square 3;; _: int = 9 # let rec map f = function [] -> [] | x :: xs -> f x :: map f xs;; val map : ('a -> 'b) -> 'a list -> 'b list = <fun> # map square (range 3 6);; - : int list = [9; 16; 25; 36] Pattern matching 8-8 4 isSorted xs # isSorted [];; _: bool = true # isSorted [3];; _: bool = true # isSorted [3;1;4;2];; _: bool = false # isSorted [1;2;3;4;5;6];; _: bool = true Pattern matching 8-9 Tuples # let t4 = (2 * 3, 4 < 5, "foo" ^ "bar", String.get "baz" 2);; # let w,x,y,z = t4;; # let first (x,_,_,_) = x;; # let last (_,_,_,z) = z;; Pattern matching 8-10 5 OCaml simplicity # let swap (a,b) = (b,a);; # swap (1+2,3=4);; # swap(swap(1+2,3=4));; Pattern matching 8-11 isMember (x,ys) # isMember(3, [5;2;3;1;4]);; _: bool = true # isMember(6, [5;2;3;1;4]);; _: bool = false # isMember(“be”, [“to”;”be”;”or”;”not”;”to”;”be”]);; _: bool = true # isMember((2,”too”),[(3,”three”);(1,”one”);(2,”two”)]);; _: bool = false Pattern matching 8-12 6.

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    6 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us