Programming Languages and Techniques (CIS120)

Similar documents
Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120)

CIS 120 Midterm I October 2, Name (printed): Username (login id):

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120)

Lab 10: OCaml sequences, comparators, stable sorting 12:00 PM, Nov 12, 2017

Programming Languages and Techniques (CIS120e)

Programming Languages and Techniques (CIS120)

Tuples. CMSC 330: Organization of Programming Languages. Examples With Tuples. Another Example

CIS 120 Midterm I February 16, 2015 SOLUTIONS

CS 321 Programming Languages

Name: CIS 120e Midterm I Review

CS Lecture 6: Map and Fold. Prof. Clarkson Fall Today s music: Selections from the soundtrack to 2001: A Space Odyssey

FUNCTIONAL PROGRAMMING

Programming Languages and Techniques (CIS120)

CMSC 330: Organization of Programming Languages. Lets, Tuples, Records

CMSC 330: Organization of Programming Languages. OCaml Expressions and Functions

CS Lecture 6: Map and Fold. Prof. Clarkson Spring Today s music: Selections from the soundtrack to 2001: A Space Odyssey

CMSC 330: Organization of Programming Languages. Functional Programming with Lists

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120)

CSE341: Programming Languages Lecture 9 Function-Closure Idioms. Dan Grossman Winter 2013

CMSC 330: Organization of Programming Languages. Functional Programming with Lists

CIS 120 Midterm II March 29, 2013 SOLUTIONS

Homework 8: Matrices Due: 11:59 PM, Oct 30, 2018

Some Advanced ML Features

Programming Languages and Techniques (CIS120)

Map and Fold. Prof. Clarkson Fall Today s music: Selections from the soundtrack to 2001: A Space Odyssey

Which of the following expressions has the type int list?

Programming Languages and Techniques (CIS120)

You ve encountered other ways of signalling errors. For example, if you lookup an unbound key in a hashtable, Java (and Scala) produce nulls:

Lab 7: OCaml 12:00 PM, Oct 22, 2017

Background. CMSC 330: Organization of Programming Languages. Useful Information on OCaml language. Dialects of ML. ML (Meta Language) Standard ML

Programming Languages and Techniques (CIS120)

CSE 130 [Winter 2014] Programming Languages

Programming Languages and Techniques (CIS120)

The Substitution Model. Nate Foster Spring 2018

Balanced Trees. Prof. Clarkson Fall Today s music: Get the Balance Right by Depeche Mode

CS153: Compilers Lecture 15: Local Optimization

CSE 341, Autumn 2005, Assignment 3 ML - MiniML Interpreter

Due: 9 February 2017 at 1159pm (2359, Pacific Standard Time)

Programming Languages and Techniques (CIS120e)

The story so far. Elements of Programming Languages. Pairs in various languages. Pairs

CMSC 330: Organization of Programming Languages. OCaml Higher Order Functions

Programming Languages and Techniques (CIS120)

Introduction to OCaml

Mini-ML. CS 502 Lecture 2 8/28/08

CMSC 330: Organization of Programming Languages. OCaml Higher Order Functions

The Haskell HOP: Higher-order Programming

MP 2 Continuation-Passing Style

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120)

CMSC 330: Organization of Programming Languages. OCaml Higher Order Functions

CIS 120 Midterm II November 7, Name (printed): Pennkey (login id):

CSE 130: Programming Languages. Polymorphism. Ranjit Jhala UC San Diego

COSE212: Programming Languages. Lecture 4 Recursive and Higher-Order Programming

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120)

This assignment has four parts. You should write your solution to each part in a separate file:

Dialects of ML. CMSC 330: Organization of Programming Languages. Dialects of ML (cont.) Features of ML. Functional Languages. Features of ML (cont.

CSE 130 Programming Languages. Lecture 3: Datatypes. Ranjit Jhala UC San Diego

Higher-Order Functions

CMSC 330: Organization of Programming Languages

Recap: ML s Holy Trinity. Story So Far... CSE 130 Programming Languages. Datatypes. A function is a value! Next: functions, but remember.

Algebraic data types. The case against null

CMSC 330: Organization of Programming Languages. OCaml Imperative Programming

CSE 130 Programming Languages. Datatypes. Ranjit Jhala UC San Diego

Balanced Trees. Nate Foster Spring 2018

Programming Languages and Techniques (CIS120)

CIS 120 Midterm II November 8, Name (printed): Pennkey (login id):

CSCI-GA Scripting Languages

CMSC 330: Organization of Programming Languages. OCaml Imperative Programming

CMSC330. Objects, Functional Programming, and lambda calculus

Part I: Written Problems

Haskell Review COMP360

} Evaluate the following expressions: 1. int x = 5 / 2 + 2; 2. int x = / 2; 3. int x = 5 / ; 4. double x = 5 / 2.

# true;; - : bool = true. # false;; - : bool = false 9/10/ // = {s (5, "hi", 3.2), c 4, a 1, b 5} 9/10/2017 4

A Func'onal Introduc'on. COS 326 David Walker Princeton University

Australian researchers develop typeface they say can boost memory, that could help students cramming for exams.

F28PL1 Programming Languages. Lecture 14: Standard ML 4

Functions. Nate Foster Spring 2018

CSE341: Programming Languages Lecture 9 Function-Closure Idioms. Dan Grossman Fall 2011

Operational Semantics. One-Slide Summary. Lecture Outline

Lecture #23: Conversion and Type Inference

Thinking Induc,vely. COS 326 David Walker Princeton University

Conversion vs. Subtyping. Lecture #23: Conversion and Type Inference. Integer Conversions. Conversions: Implicit vs. Explicit. Object x = "Hello";

To figure this out we need a more precise understanding of how ML works

Functional Programming. Introduction To Cool

Haske k ll An introduction to Functional functional programming using Haskell Purely Lazy Example: QuickSort in Java Example: QuickSort in Haskell

Welcome to... CS113: Introduction to C

Thinking Induc,vely. COS 326 David Walker Princeton University

Programming Languages

CSC324- TUTORIAL 5. Shems Saleh* *Some slides inspired by/based on Afsaneh Fazly s slides

Monday Feb 16, 5pm. solve these equalities. Midterm Thursday Feb 12. covers only ocaml part of the course, assignments 1 through 3.

Programming Languages and Techniques (CIS120)

Transcription:

Programming Languages and Techniques () Lecture 11 Feb 12, 2014 OpBons and Unit

Announcements Homework 4 available on the web today due Tuesday, Feb 17 th n- body physics simulabon start early; see Piazza for discussions Read Chapters 11, 12 & 13 (they re short) Midterm 1 Scheduled in class on Friday, Feb 21 st Review session Wednesday, Feb 19 th, 7-9PM in Levine 101

List processing The fold design pawern

FuncBons as Data We ve seen a number of ways in which funcbons can be treated as data in OCaml Present- day programming pracbce offers many more examples at the small scale : objects bundle funcbons (a.k.a. methods) with data iterators ( cursors for walking over data structures) event listeners (in GUIs) etc. The idiom is useful at the large scale : Google s MapReduce Framework for mapping across sets of key- value pairs Then reducing the results per key of the map Easily distributed to 10,000 machines to execute in parallel!

Refactoring code, again Is there a pawern in the definibon of these two funcbons? let rec exists (l : bool list) : bool =! begin match l with! [] -> false! h :: t -> h exists t! end! base case: Simple answer when the list is empty let rec acid_length (l : acid list) : int =! begin match l with! combine step: [] -> 0! Do something with x :: t -> 1 + acid_length t! the head of the list end! and the recursive call Can we factor out that pawern using first- class funcbons?

List Fold Fold (aka Reduce) Another foundabonal funcbon for programming with lists Captures the pawern of recursion over lists Also part of OCaml standard library (List.fold_right) Similar operabons for other recursive datatypes (fold_tree) let rec fold (combine: 'a -> 'b -> 'b)! (base:'b) (l : 'a list) : 'b =! begin match l with! [] -> base! x :: t -> combine x (fold combine base t)! end! let acid_length (l : acid list) : int =! fold (fun (x:acid) (acc:int) -> 1 + acc) 0 l! let exists (l : bool list) : bool =! fold (fun (x:bool) (acc:bool) -> x acc) false l!

How would you rewrite this funcbon let rec sum (x : int list) : int = begin match x with [] -> 0 h :: t -> h + sum t end! using fold? What should be the arguments for base and combine? 1. combine is: (fun (h:int) (acc:int) -> acc + 1) base is: 0! 2. combine is: (fun (h:int) (acc:int) -> h + acc) base is: 0! 3. combine is: (fun (h:int) (acc:int) -> h + acc) base is: 1! 4. sum can t be wriwen by with fold.

How would you rewrite this funcbon let rec reverse (x : int list) : int list = begin match x with [] -> [] h :: t -> reverse t @ [h] end! using fold? What should be the arguments for base and combine? 1. combine is: (fun (h:int) (acc:int list) -> h :: acc) base is: 0! 2. combine is: (fun (h:int) (acc:int list) -> acc @ [h]) base is: 0! 3. combine is: (fun (h:int) (acc:int list) -> acc @ [h]) base is: []! 4. reverse can t be wriwen by with fold.

Which of these is funcbon that calculates the maximum value in a list: 1. let rec list_max (x:'a list) : a = begin match x with [] -> [] h :: t -> max h (list_max t) end! 2. let rec list_max (x:'a list) : a = fold max 0 x! 3. let rec list_max (x: a list) : a = begin match x with h :: t -> max h (list_max t) end! 4. None of the above

Quiz answer list_max isn t defined for the empty list! let rec list_max (l:'a list) : a =! begin match l with! [] -> failwith empty list! [h] -> h! h::t -> max h (list_max t)! end! let list_max (l:'a list) : a =! begin match l with! [] -> failwith empty list! h::t -> fold max h t! end!

Client of list_max (* string_of_max calls list_max *) let string_of_max (x:int list) : string = string_of_int (list_max x)! Oops! string_of_max will fail if given [] Not so easy to debug if string_of_max is wriwen by one person and list_max is wriwen by another Interface of list_max is not very informabve val list_max : int list -> int

Dealing with ParBality OpBon Types

ParBal FuncBons SomeBmes funcbons aren t defined for all inputs: tree_max from the BST implementabon isn t defined for empty trees integer division by 0 Map.find k m when the key k isn t in the finite map m We have seen how to deal with parbality using failwith, but failwith aborts the program Can we do bewer? Hint: we already have all the technology we need.

OpBon Types Define a generic datatype of op5onal values: type a option =! None! Some of a! A parbal funcbon returns an opbon let list_max (l:list) : int option =! Contrast this with null value, a legal return value of any type caller can accidentally forget to check whether null was used; results in NullPointerExcepBons or crashes Sir Tony Hoare, Turing Award winner and inventor of null calls it his billion dollar mistake!

Example: list_max A funcbon that returns the maximum value of a list as an opbon (None if the list is empty) let list_max (l:'a list) : 'a option =! begin match l with! [] -> None! x::tl -> Some (fold max x tl)! end!

Revised client of list_max (* string_of_max calls list_max *)! let string_of_max (l:int list) : string =! begin match (list_max l) with! None -> no maximum! Some m -> string_of_int m! end! string_of_max will never fail The type of list_max makes it explicit that a client must check for parbality. val list_max : int list -> int option!

What is the type of this funcbon? let head (x: ) : = begin match x with [] -> None h :: t -> Some h end! 1. a list - > a 2. a list - > a list 3. a list - > b opbon 4. a list - > a opbon 5. None of the above

What is the value of this expression? let head (x: a list) : a option = begin match x with [] -> None h :: t -> Some h end in head [[1]]! 1. 1 2. Some 1 3. [1] 4. Some [1] 5. None of the above

What is the value of this expression? let head (x: a list) : a option = begin match x with [] -> None h :: t -> Some h end in [ head [1]; head [] ]! 1. [ 1 ; 0 ] 2. 1 3. [Some 1; None] 4. [None; None] 5. None of the above

Unit

Similar to "void" in Java or C unit: the trivial type For funcbons that don't take any arguments let f () : int = 3! let y : int = f ()! val f : unit -> int! val y : int! Also for funcbons that don't return anything, such as tesbng and prinbng funcbons a.k.a commands: (* run_test : string -> (unit -> bool) -> unit *)! ;; run_test TestName test! (* print_string : string -> unit *)! ;; print_string Hello, world!!

unit: the boring type Actually, () is a value just like any other value. For funcbons that don't take any interesbng arguments let f () : int = 3! let y : int = f ()! val f : unit -> int! val y : int! Also for funcbons that don't return anything interesbng, such as tesbng and prinbng funcbons a.k.a commands: (* run_test : string -> (unit -> bool) -> unit *)! ;; run_test TestName test! (* print_string : string -> unit *)! ;; print_string Hello, world!!

unit: the first- class type Can define values of type unit let x = ()! val x : unit! Can pawern match unit (even in funcbon definibons) let z = begin match x with () -> 4 end! fun () -> 3! Is the implicit else branch: ;; if z <> 4 then! failwith oops"! ;; if z <> 4 then! failwith oops"! else ()!

Sequencing Commands and Expressions Expressions of type unit are useful because of their side effects (e.g. prinbng) We can sequence those effects using ; unlike in C, Java, etc., ; doesn t terminate a statement it separates a command from an expression let f (x:int) : int = print_string f called with ; print_string (string_of_int x); x + x! do not use ; here! note the use of ; here We can think of ; as an infix funcbon of type: unit -> a -> a!

What is the type of f in the following program: 1. unit -> int! 2. unit -> unit! 3. int -> unit! 4. int -> int! 5. f is ill typed let f (x:int) = print_int x!

What is the type of f in the following program: let f (x:int) =!!!!!print_int (x + x)! 1. unit -> int! 2. unit -> unit! 3. int -> unit! 4. int -> int! 5. f is ill typed

What is the type of f in the following program: let f (x:int) =!!!!!(print_int x);!!!!!x + x! 1. unit -> int! 2. unit -> unit! 3. int -> unit! 4. int -> int! 5. f is ill typed

ImperaBve Programming

DeclaraBve programming persistent data structures Course Overview recursion is main control structure heavy use of funcbons as data ImperaBve programming mutable data structures (that can be modified in place ) itera5on is main control structure Object- oriented programming pervasive abstracbon by default mutable data structures / iterabon heavy use of funcbons (objects) as data We are here. Midterm 1 covers material up to this point.