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

Slides:



Advertisements
Similar presentations
Functional Programming Lecture 10 - type checking.
Advertisements

More ML Compiling Techniques David Walker. Today More data structures lists More functions More modules.
Programovací jazyky F# a OCaml Chapter 4. Generic and recursive types.
Modern Programming Languages, 2nd ed.
A Third Look At ML 1. Outline More pattern matching Function values and anonymous functions Higher-order functions and currying Predefined higher-order.
Introduction to OCaml Slides prepared by Matt Gruskin Some material borrowed from the CIS 500 lecture notes.
Higher-order functions in OCaml. Higher-order functions A first-order function is one whose parameters and result are all "data" A second-order function.
Type Checking, Inference, & Elaboration CS153: Compilers Greg Morrisett.
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists Dan Grossman Winter 2013.
Writing functions in OCaml. Defining a simple function # let add (x, y) = x + y;; val add : int * int -> int = Notice what this says: –add is a value.
Core Java Lecture 4-5. What We Will Cover Today What Are Methods Scope and Life Time of Variables Command Line Arguments Use of static keyword in Java.
ML Lists.1 Standard ML Lists. ML Lists.2 Lists  A list is a finite sequence of elements. [3,5,9] ["a", "list" ] []  ML lists are immutable.  Elements.
More about functions Plus a few random things. 2 Tail recursion A function is said to be tail recursive if the recursive call is the very last thing it.
Programming Languages Section 1 1 Programming Languages Section 1. SML Fundamentals Xiaojuan Cai Spring 2015.
ML: a quasi-functional language with strong typing Conventional syntax: - val x = 5; (*user input *) val x = 5: int (*system response*) - fun len lis =
Patterns in ML functions. Formal vs. actual parameters Here's a function definition (in C): –int add (int x, int y) { return x + y; } –x and y are the.
CSE341: Programming Languages Lecture 5 Pattern-Matching Dan Grossman Fall 2011.
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists Dan Grossman Fall 2011.
Mark Hennessy Dept. Computer Science NUI Maynooth 1 CaML continued CS 351 Programming Paradigms Dept. Computer Science NUI Maynooth.
0 PROGRAMMING IN HASKELL Chapter 4 - Defining Functions.
Introduction to ML - Part 2 Kenny Zhu. What is next? ML has a rich set of structured values Tuples: (17, true, “stuff”) Records: {name = “george”, age.
Introduction to ML Last time: Basics: integers, Booleans, tuples,... simple functions introduction to data types This time, we continue writing an evaluator.
FUNCTIONAL PROGRAMMING IN ERLANG ID1218 Lecture Christian Schulte Software and Computer Systems School of Information and.
TODAY’S LECTURE Review Chapter 2 Go over exercises.
OCaml The PL for the discerning hacker.. Hello. I’m Zach, one of Sorin’s students.
0 REVIEW OF HASKELL A lightening tour in 45 minutes.
Patterns in OCaml functions. Formal vs. actual parameters Here's a function definition (in C): –int add (int x, int y) { return x + y; } –x and y are.
What is an Array? An array is a collection of variables. Arrays have three important properties: –group of related items(for example, temperature for.
5 BASIC CONCEPTS OF ANY PROGRAMMING LANGUAGE Let’s get started …
Chapter 9: Functional Programming in a Typed Language.
Functional Programming With examples in F#. Pure Functional Programming Functional programming involves evaluating expressions rather than executing commands.
1-Nov-15 Haskell II Functions and patterns. Data Types Int + - * / ^ even odd Float + - * / ^ sin cos pi truncate Char ord chr isSpace isUpper … Bool.
11/1/20151 GC16/3011 Functional Programming Lecture 5 Miranda patterns, functions, recursion and lists.
A Second Look At ML 1. Outline Patterns Local variable definitions A sorting example 2.
F28PL1 Programming Languages Lecture 13: Standard ML 3.
A Third Look At ML Chapter NineModern Programming Languages, 2nd ed.1.
CSE 341 : Programming Languages Lecture 2 Functions, Pairs, Lists Zach Tatlock Spring 2014.
CMSC 330: Organization of Programming Languages Functional Programming with OCaml.
CMSC 330: Organization of Programming Languages Maps and Folds Anonymous Functions.
CMSC 330: Organization of Programming Languages
CMSC 330: Organization of Programming Languages Operational Semantics a.k.a. “WTF is Project 4, Part 3?”
Introduction to Objective Caml. General comments ML is a purely functional language--there are (almost) no side effects There are two basic dialects of.
Chapter SevenModern Programming Languages1 A Second Look At ML.
2: Basics Basics Programming C# © 2003 DevelopMentor, Inc. 12/1/2003.
CMSC 330: Organization of Programming Languages Operational Semantics.
Haskell Chapter 5, Part II. Topics  Review/More Higher Order Functions  Lambda functions  Folds.
1 Objective Caml (Ocaml) Aaron Bloomfield CS 415 Fall 2005.
0 PROGRAMMING IN HASKELL Typeclasses and higher order functions Based on lecture notes by Graham Hutton The book “Learn You a Haskell for Great Good” (and.
6-Jul-16 Haskell II Functions and patterns. Data Types Int + - * / ^ even odd Float + - * / ^ sin cos pi truncate Char ord chr isSpace isUpper … Bool.
CMSC 330: Organization of Programming Languages Object Oriented Programming with OCaml.
Polymorphic Functions
Types CSCE 314 Spring 2016.
ML: a quasi-functional language with strong typing
Theory of Computation Lecture 4: Programs and Computable Functions II
ML Again ( Chapter 7) Patterns Local variable definitions
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists
A lightening tour in 45 minutes
Expanded Recursive Diagrams OCAML rapid tour, day 2
CMSC 330: Organization of Programming Languages
Objective caml Daniel Jackson MIT Lab for Computer Science 6898: Advanced Topics in Software Design March 18, 2002.
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists
CSCE 314: Programming Languages Dr. Dylan Shell
CSE-321 Programming Languages Introduction to Functional Programming
Madhusudan Parthasarathy
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists
PROGRAMMING IN HASKELL
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists
Presentation transcript:

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

CMSC 3302 Review Functions – let [rec] = Types – int, bool, string Lists – [i1; i2; i3] or i1::i2::i3::[] Matching – match with -> | ->

CMSC 3303 Review let hd l = match l with (h::t) -> h –hd [1;2;3] (* evaluates to 1 *) let hd l = match l with (h::_) -> h –hd [] (* error! no pattern matches *) let tl l = match l with (h::t) -> t –tl [1;2;3] (* evaluates to [2; 3] *)

CMSC 3304 More Examples let f l = match l with (h1::(h2::_)) -> h1 + h2 –f [1;2;3] – (* evaluates to 3 *) let g l = match l with [h1; h2] -> h1 + h2 –g [1; 2] – (* evaluates to 3 *) –g [1; 2; 3] – (* error! no pattern matches *) Exactly 2 element list [h1;h2] 2+ element list (h1::(h2::_))

CMSC 3305 An Abbreviation let f p = e, where p is a pattern, is a shorthand for let f x = match x with p -> e Examples –let hd (h::_) = h –let tl (_::t) = t –let f (x::y::_) = x + y –let g [x; y] = x + y Useful if there’s only one acceptable input

CMSC 3306 Pattern Matching Lists of Lists You can do pattern matching on these as well Examples –let addFirsts ((x::_) :: (y::_) :: _) = x + y addFirsts [ [1; 2; 3]; [4; 5]; [7; 8; 9] ] = 5 –let addFirstSecond ((x::_)::(_::y::_)::_) = x + y addFirstSecond [ [1; 2; 3]; [4; 5]; [7; 8; 9] ] = 6 Note: You probably won’t do this much or at all –You’ll mostly write recursive functions over lists –We’ll see that soon

CMSC 3307 OCaml Functions Take One Argument Recall this example –It looks like you’re passing in two arguments –Actually, you’re passing in a tuple instead And using pattern matching Tuples are constructed using (e 1,..., e n ) –They’re like C structs but without field labels, and allocated on the heap –Unlike lists, tuples do not need to be homogenous –E.g., (1, ["string1"; "string2"]) is a valid tuple Tuples are deconstructed using pattern matching let plus (x, y) = x + y;; plus (3, 4);;

CMSC 3308 Examples with Tuples let plusThree (x, y, z) = x + y + z let addOne (x, y, z) = (x+1, y+1, z+1) –plusThree (addOne (3, 4, 5)) (* returns 15 *) let sum ((a, b), c) = (a+c, b+c) –sum ((1, 2), 3) = (4, 5) let plusFirstTwo (x::y::_, a) = (x + a, y + a) –plusFirstTwo ([1; 2; 3], 4) = (5, 6) let tls (_::xs, _::ys) = (xs, ys) –tls ([1; 2; 3], [4; 5; 6; 7]) = ([2; 3], [5; 6; 7]) Remember, semicolon for lists, comma for tuples –[1, 2] = [(1, 2)] = a list of size one –(1; 2) = a syntax error

CMSC 3309 Another Example let f l = match l with x::(_::y) -> (x,y) What is f [1;2;3;4] ? (1,[3;4])

CMSC List and Tuple Types Tuple types use * to separate components Examples –(1, 2) : int * int –(1, "string", 3.5) : int * string * float –(1, ["a"; "b"], 'c') : –[(1,2)] : –[(1, 2); (3, 4)] : –[(1,2); (1,2,3)] :

CMSC List and Tuple Types Tuple types use * to separate components Examples –(1, 2) : int * int –(1, "string", 3.5) : int * string * float –(1, ["a"; "b"], 'c') : int * string list * char –[(1,2)] : (int * int) list –[(1, 2); (3, 4)] : (int * int) list –[(1,2); (1,2,3)] : error

Brief Aside This form of “plus” is also valid: Its type is int -> int -> int Recall the tuple version: Its type is int * int -> int The former is an example of currying or partial evaluation We will discuss this later; for now, view it as OCaml’s special method of “multiple” parameter passing CMSC 330 let plus x y = x + y;; plus 3 4;; let plus (x, y) = x + y;; plus (3, 4);;

CMSC Type declarations type can be used to create new names for types –useful for combinations of lists and tuples –like “typedef” in C Examples type my_type = int * (int list) (3, [1; 2]) : my_type type my_type2 = int * char * (int * float) (3, ‘a’, (5, 3.0)) : my_type2

CMSC Polymorphic Types Some functions we saw require specific list types –let plusFirstTwo (x::y::_, a) = (x + a, y + a) –plusFirstTwo : int list * int -> (int * int) But other functions work for any list –let hd (h::_) = h –hd [1; 2; 3](* returns 1 *) –hd ["a"; "b"; "c"](* returns "a" *) OCaml gives such functions polymorphic types –hd : 'a list -> 'a –this says the function takes a list of any element type 'a, and returns something of that type

CMSC Examples of Polymorphic Types let tl (_::t) = t –tl : 'a list -> 'a list let swap (x, y) = (y, x) –swap : 'a * 'b -> 'b * 'a let tls (_::xs, _::ys) = (xs, ys) –tls : 'a list * 'b list -> 'a list * 'b list

CMSC Tuples Are a Fixed Size # let foo x = match x with (a, b) -> a + b | (a, b, c) -> a + b + c;; This pattern matches values of type 'a * 'b * 'c but is here used to match values of type 'd * 'e Thus there's never more than one match case with tuples –So the shorted form is always usable: let foo (a, b) = a + b

CMSC Conditionals Use if...then...else like C/Java –No parentheses and no end if grade >= 90 then print_string "You got an A" else if grade >= 80 then print_string "You got a B" else if grade >= 70 then print_string "You got a C" else print_string "You’re not doing so well"

CMSC Conditionals (cont’d) In OCaml, conditionals return a result –The value of whichever branch is true/false –Like ? : in C, C++, and Java # if 7 > 42 then "hello" else "goodbye";; - : string = "goodbye" # let x = if true then 3 else 4;; x : int = 3 # if false then 3 else 3.0;; This expression has type float but is here used with type int

CMSC OCaml Equality # let a1 = [1;2;3];; val a1 : int list = [1; 2; 3] # let a2 = [1;2;3];; val a2 : int list = [1; 2; 3] # a1 == a2;; - : bool = false (shallow equality) # a1 = a2;; -: bool = true (deep equality) -<> is negation of = -!= is negation of ==

CMSC Recursive Functions The rec part means “define a recursive function” –This is special for technical reasons –let x = e1 in e2x in scope within e2 –let rec x = e1 in e2x in scope within e2 and e1 OCaml will complain if you use let instead of let rec let rec fact n = if n = 0 then 1 else n * fact (n-1);;

CMSC Recursion = Looping Recursion is essentially the only way to iterate –(The only way we’re going to talk about) Another example let rec print_up_to (n, m) = print_int n; print_string "\n"; if n < m then print_up_to (n + 1, m)

CMSC Lists and Recursion Lists have a recursive structure –And so most functions over lists will be recursive –This is just like an inductive definition The length of the empty list is zero The length of a nonempty list is 1 plus the length of the tail –Type of length function? let rec length l = match l with [] -> 0 | (_::t) -> 1 + (length t)

CMSC More examples of let (try to evaluate) let x = 1 in x;; let x = x in x;; let x = 4 in let x = x + 1 in x;; let f n = 10;; let f n = if n = 0 then 1 else n * f (n – 1);; f 0;; f 1;; let g x = g x;;

CMSC More examples of let let x = 1 in x;; (* 1 *) let x = x in x;; (* error, x is unbound *) let x = 4 in let x = x + 1 in x;; (* 5 *) let f n = 10;; let f n = if n = 0 then 1 else n * f (n – 1);; f 0;; (* 1 *) f 1;; (* 10 *) let g x = g x;; (* error *)

CMSC Recursion Exercises sum l (* sum of elts in l *) negate l (* negate elements in list *) last l (* last element of l *)

CMSC Recursion Exercises sum l (* sum of elts in l *) let rec sum l = match l with [] -> 0 | (x::xs) -> x + (sum xs) negate l (* negate elements in list *) let rec negate l = match l with [] -> [] | (x::xs) -> (-x) :: (negate xs) last l (* last element of l *) let rec last l = match l with [x] -> x | (x::xs) -> last xs