FUNCTIONAL PROGRAMMING

Similar documents
PostgreSQL Functions By Example

Introduction to Python

We will learn the Python programming language. Why? Because it is easy to learn and many people write programs in Python so we can share.

The Java Series. Java Essentials I What is Java? Basic Language Constructs. Java Essentials I. What is Java?. Basic Language Constructs Slide 1

Semantic Analysis: Types and Type Checking

J a v a Quiz (Unit 3, Test 0 Practice)

There e really is No Place Like Rome to experience great Opera! Tel: to discuss your break to the Eternal City!

INTRODUCTION TO OBJECTIVE-C CSCI 4448/5448: OBJECT-ORIENTED ANALYSIS & DESIGN LECTURE 12 09/29/2011

Adding GADTs to OCaml the direct approach

9 Sat 11:30 am Golf with Tom and Harry

The Clean programming language. Group 25, Jingui Li, Daren Tuzi

4 th Grade Summer Mathematics Review #1. Name: 1. How many sides does each polygon have? 2. What is the rule for this function machine?

2015 Continuing Education Classes

LAUREA MAGISTRALE - CURRICULUM IN INTERNATIONAL MANAGEMENT, LEGISLATION AND SOCIETY. 1st TERM (14 SEPT - 27 NOV)

Some Scanner Class Methods

Chapter 7: Functional Programming Languages

How To Program In Scheme (Prolog)

Type Checking SQL for Secure Database Access

Mailing instructions Home Direct

PLAN YOUR PERFECT WINTER!

14 Sep Jul 2016 (Weeks 1 - V14)

The Properties of Signed Numbers Section 1.2 The Commutative Properties If a and b are any numbers,

3 Improving the Crab more sophisticated programming

Social-Media Posts for September s Life Insurance Awareness Month

Chapter 4 OOPS WITH C++ Sahaj Computer Solutions

First Java Programs. V. Paúl Pauca. CSC 111D Fall, Department of Computer Science Wake Forest University. Introduction to Computer Science

Utility Software II lab 1 Jacek Wiślicki, jacenty@kis.p.lodz.pl original material by Hubert Kołodziejski

DECLARATION OF PERFORMANCE NO. HU-DOP_TN _001

Tutorial on Writing Modular Programs in Scala

Phased Return to Work Guidance Human Resources Department

Software Model Checking. Equivalence Hierarchy

CS106A, Stanford Handout #38. Strings and Chars

Describing, manipulating and pricing financial contracts: The MLFi language

A Scala Tutorial for Java programmers

The programming language C. sws1 1

Programming Fundamentals. Lesson 20 Sections

MapReduce. MapReduce and SQL Injections. CS 3200 Final Lecture. Introduction. MapReduce. Programming Model. Example

Intro to Web Programming. using PHP, HTTP, CSS, and Javascript Layton Smith CSE 4000

Relational Databases

CSc 372. Comparative Programming Languages. 21 : Haskell Accumulative Recursion. Department of Computer Science University of Arizona

ACCA Interactive Timetable

COMPUTER SCIENCE. Paper 1 (THEORY)

ACCA Interactive Timetable

BANK B-I-N-G-O. service charge. credit card. pen. line nickel interest cash bank. ATM dollar check signature. debit card.

Databases 2011 The Relational Model and SQL

Programming Languages CIS 443

Overview. Elements of Programming Languages. Advanced constructs. Motivating inner class example

Part-time Diploma in InfoComm and Digital Media (Information Systems) Certificate in Information Systems Course Schedule & Timetable

Lab 9. Spam, Spam, Spam. Handout 11 CSCI 134: Spring, To gain experience with Strings. Objective

agucacaaacgcu agugcuaguuua uaugcagucuua

ACCA Interactive Timetable

Diagonalization. Ahto Buldas. Lecture 3 of Complexity Theory October 8, Slides based on S.Aurora, B.Barak. Complexity Theory: A Modern Approach.

Designing with Exceptions. CSE219, Computer Science III Stony Brook University

Writing Days and Months

CENTRAL OHIO TECHNICAL COLLEGE COLLEGE CALENDAR* Page 1

Moving from CS 61A Scheme to CS 61B Java

Chapter 15 Functional Programming Languages

Deterministic Discrete Modeling

Christianna S. Williams, University of North Carolina at Chapel Hill, Chapel Hill, NC

Introduction to type systems

Third Grade Math Games

Mixed Logic A B A B. 1. Ignore all bubbles on logic gates and inverters. This means

WRITING EQUATIONS USING THE 5-D PROCESS #43

Exposed Database( SQL Server) Error messages Delicious food for Hackers

Session 6. Microsoft Project. Emanuele Della Valle. Lecturer: Dario Cerizza

CSE373: Data Structures and Algorithms Lecture 3: Math Review; Algorithm Analysis. Linda Shapiro Winter 2015

Desirable Properties Of An Internet Addressing Scheme

A Propositional Dynamic Logic for CCS Programs

Analyse et Conception Formelle. Lesson 5. Crash Course on Scala

Point Biserial Correlation Tests

Factoring Algebra- Chapter 8B Assignment Sheet

AP Computer Science Static Methods, Strings, User Input

American Red Cross Lifeguarding Courses Minnesota Territory

Introducing Formal Methods. Software Engineering and Formal Methods

UNIVERSITY OF LONDON (University College London) M.Sc. DEGREE 1998 COMPUTER SCIENCE D16: FUNCTIONAL PROGRAMMING. Answer THREE Questions.

Friendship and Encapsulation in C++

2-Step Credit Spreads

Class 32: The Java Collections Framework

Normal Form vs. Non-First Normal Form

Automata and Formal Languages

Unit 3 Banking: Opening an Account

Resco Mobile CRM Woodford (Rules Guide) Document version

x10* CP290 HOME CONTROL INTERFACE PROGRAMMING GUIDE FOR ADVANCED PROGRAMMERS

How To Create A Table In Sql (Ahem)

language 1 (source) compiler language 2 (target) Figure 1: Compiling a program

Relational Database: Additional Operations on Relations; SQL

Project Management Plan Outline ITSC

SDHNS 3 Hour English Food Handlers Class

The Smalltalk Programming Language. Beatrice Åkerblom

Glassfish, JAVA EE, Servlets, JSP, EJB

Problem 1. CS 61b Summer 2005 Homework #2 Due July 5th at the beginning of class

1st Grade Math Standard I Rubric. Number Sense. Score 4 Students show proficiency with numbers beyond 100.

Transcription:

FUNCTIONAL PROGRAMMING User-defined Data Types Prof. Clarkson Summer 2015 Today s music: Pokémon Theme by Jason Paige

Review Yesterday: New idioms and library functions: Map, fold, and other higher-order functions Today: Ways to define your own data types: records, tuples, variants, options

RECORDS

Record definition A record contains several named fields Before you can use a record, must define a record type: type time = {hour: int; min: int; ampm: string} To build a record: Write a record expression: {hour=10; min=10; ampm="am"} Order of fields doesn t matter: {min=10; hour=10; ampm="am"} is equivalent To access record's field: r.hour

Record expressions Syntax: {f1 = e1; ; fn = en} Evaluation: If e1 evaluates to v1, and en evaluates to vn Then {f1 = e1; ; fn = en} evaluates to {f1 = v1,, fn = vn} Result is a record value Type-checking: If e1 : t1 and e2 : t2 and en : tn, and if t is a defined type of the form {f1:t1,, fn:tn} then {f1 = e1; ; fn = en}: t

Record field access Syntax: e.f Evaluation: If e evaluates to {f = v, } Then e.f evaluates to v Type-checking: If e : t1 and if t1 is a defined type of the form {f:t2, } then e.f : t2

Evaluation notation We keep writing statements like: If e evaluates to {f = v, } then e.f evaluates to v Let's introduce a shorthand notation: Instead of "e evaluates to v" write "e ==> v" So we can now write: If e ==> {f = v, } then e.f ==> v

By name vs. by position Fields of record are identified by name order we write fields in expression is irrelevant Opposite choice: identify by position e.g., Would the student named NN. step forward? vs. Would the student in seat n step forward? You re accustomed to both: Java object fields accessed by name Java method arguments passed by position (but accessed in method body by name) OCaml has something you might not have seen: A kind of data accessed by position

PAIRS AND TUPLES

Pairs A pair of data: two pieces of data glued together e.g., (1,2) (true, "Hello") ([1;2;3], 0.5) We need language constructs to build pairs and to access the pieces...

Pairs: building Syntax: (e1,e2) Evaluation: If e1 ==> v1 and e2 ==> v2 Then (e1,e2) ==> (v1,v2) A pair of values is itself a value Type-checking: If e1:t1 and e2:t2, then (e1,e2):t1*t2 A new kind of type, the product type

Pairs: accessing Syntax: fst e and snd e Projection functions Evaluation: If e ==> (v1,v2) then fst e ==> v1 and snd e ==> v2 Type-checking: If e: ta*tb, then fst e has type ta and snd e has type tb

Tuples Actually, you can have tuples with more than two parts A new feature: a generalization of pairs Syntax, semantics are straightforward, except for projection... (e1,e2,,en) t1 * t2 * * tn fst e, snd e,??? Instead of generalizing projection functions, use pattern matching New kind of pattern, the tuple pattern: (p1,..., pn)

Pattern matching tuples match (1,2,3) with (x,y,z) -> x+y+z (* ==> 6 *) let thrd t = match t with (x,y,z) -> z (* thrd : 'a*'b*'c -> 'c *) Note: we never needed more than one branch in the match expression...

Pattern matching without match (* OK *) let thrd t = match t with (x,y,z) -> z (* good *) let thrd t = let (x,y,z) = t in z (* better *) let thrd t = let (_,_,z) = t in z (* best *) let thrd (_,_,z) = z

Extended syntax for let Previously we had this syntax: let x = e1 in e2 let [rec] f x1... xn = e1 in e2 Everywhere we had a variable identifier x, we can really use a pattern! let p = e1 in e2 let [rec] f p1... pn = e1 in e2 Old syntax is just a special case of new syntax, since a variable identifier is a pattern

Pattern matching arguments (* OK *) let sum_triple t = let (x,y,z) = t in x+y+z (* better *) let sum_triple (x,y,z) = x+y+z Note how that last version looks syntactically like a function in C/Java!

Question #3 What is the type of this expression? let (x,y) = snd( zar,( doz,42)) in (42,y) A. {x:string; y:int} B. int*int C. string*int D. int*string E. string*(string*int)

Question #3 What is the type of this expression? let (x,y) = snd( zar,( doz,42)) in (42,y) A. {x:string; y:int} B. int*int C. string*int D. int*string E. string*(string*int)

Unit Can actually have a tuple () with no components whatsoever Think of it as a degenerate tuple Or, like a Boolean that can only have one value Unit is a value written () and a type written unit Might seem dumb now; will be useful later!

Pattern matching records (* OK *) let get_hour t = match t with {hour=h; min=m; ampm=s} -> h (* better *) let get_hour t = match t with {hour=h; min=_; ampm=_} -> h (* better *) let get_hour t = match t with {hour; min; ampm} -> hour (* better *) let get_hour t = match t with {hour} -> hour (* better *) let get_hour t = let {hour} = t in hour (* better *) let get_hour {hour} = hour (* best *) let get_hour t = t.hour New kind of pattern, the record pattern: {f1[=p1];...; fn[=pn]}

By name vs. by position, again How to choose between coding (4,7,9) and {f=4;g=7;h=9}? Tuples are syntactically shorter Records are self-documenting For many (4? 8? 12?) fields, a record is usually a better choice

VARIANTS

Variant type day = Sun Mon Tue Wed Thu Fri Sat let day_to_int d = match d with Sun -> 1 Mon -> 2 Tue -> 3 Wed -> 4 Thu -> 5 Fri -> 6 Sat -> 7

Building and accessing variants Syntax: type t = C1... Cn the Ci are called constructors Evaluation: a constructor is already a value Type checking: Ci : t Accessing: use pattern matching; constructor name is a pattern

Pokémon variant

Pokémon variant type ptype = TNormal TFire TWater type peff = ENormal ENotVery ESuper let eff_to_float = function ENormal -> 1.0 ENotVery -> 0.5 ESuper -> 2.0 let eff_att_vs_def : ptype*ptype -> peff = function (TFire,TFire) -> ENotVery (TWater,TWater) -> ENotVery (TFire,TWater) -> ENotVery (TWater,TFire) -> ESuper _ -> ENormal

Argument order: records If you are worried about clients of function forgetting which order to pass arguments in tuple, use a record: type att_def = {att:ptype; def:ptype} let eff_att_vs_def : att_def -> peff = function {att=tfire;def=tfire} -> ENotVery {att=twater;def=twater} -> ENotVery {att=tfire;def=twater} -> ENotVery {att=twater;def=tfire} -> ESuper _ -> ENormal

Argument order: labeled arguments Or (though not quite as good) use labeled arguments: let eff_att_vs_def ~att ~def = match (att, def) with (TFire,TFire) -> ENotVery (TWater,TWater) -> ENotVery (TFire,TWater) -> ENotVery (TWater,TFire) -> ESuper _ -> ENormal let super = eff_att_vs_def ~att:twater ~def:tfire let super = eff_att_vs_def ~def:tfire ~att:twater let notvery = eff_att_vs_def TFire TWater

Variants vs. records vs. tuples Define Build/construct Access/destruct Variant type Constructor name Pattern matching Record type Record expression with { } Tuple N/A Tuple expression with ( ) Pattern matching OR field selection with dot operator. Pattern matching OR fst or snd Variants: one-of types aka sum types Records, tuples: each-of types aka product types

Question Which of the following would be better represented with records rather than variants? A. Coins, which can be pennies, nickels, dimes, or quarters B. Students, who have names and id numbers C. A plated dessert, which has a sauce, a creamy component, and a crunchy component D. A and C E. B and C

Question Which of the following would be better represented with records rather than datatypes? A. Coins, which can be pennies, nickels, dimes, or quarters B. Students, who have names and NetIDs C. A plated dessert, which has a sauce, a creamy component, and a crunchy component D. A and C E. B and C

OPTIONS

What is max of empty list? let rec max_list = function [] ->??? h::t -> max h (max_list t) How to fill in the??? min_int would be a reasonable choice or could raise an exception in Java, might return null... but OCaml gives us another option!

Options Options: t option is a type for any type t (much like t list is a type for any type t) Building and Type Checking and Evaluation: None has type 'a option much like [] has type 'a list None is a value Some e : t option if e:t much like e::[] has type t list if e:t If e==>v then Some e==>some v Accessing: match e with None ->... Some x ->...

Again: What is max of empty list? let rec max_list = function [] -> None h::t -> match max_list t with None -> Some h Some x -> Some (max h x) (* max_list : 'a list -> 'a option *) Very stylish! no possibility of exceptions no chance of programmer ignoring a null return

Recap: User-defined data types Records Tuples (pairs, unit) Variants Options