Which of the following expressions has the type int list?

Similar documents
Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120)

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

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

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120)

OCaml. ML Flow. Complex types: Lists. Complex types: Lists. The PL for the discerning hacker. All elements must have same type.

Programming Languages

News. Programming Languages. Complex types: Lists. Recap: ML s Holy Trinity. CSE 130: Spring 2012

Programming Languages

Programming Languages

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

Plan (next 4 weeks) 1. Fast forward. 2. Rewind. 3. Slow motion. Rapid introduction to what s in OCaml. Go over the pieces individually

Lists. Prof. Clarkson Fall Today s music: "Blank Space" by Taylor Swift

CS 321 Programming Languages

CSE 130 [Winter 2014] Programming Languages

CSCE 314 TAMU Fall CSCE 314: Programming Languages Dr. Flemming Andersen. Haskell Functions

Shell CSCE 314 TAMU. Haskell Functions

OCaml. History, Variants. ML s holy trinity. Interacting with ML. Base type: Integers. Base type: Strings. *Notes from Sorin Lerner at UCSD*

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

Any questions. Say hello to OCaml. Say hello to OCaml. Why readability matters. History, Variants. Plan (next 4 weeks)

A Second Look At ML. Chapter Seven Modern Programming Languages, 2nd ed. 1

Programming Languages and Techniques (CIS120)

COSE212: Programming Languages. Lecture 3 Functional Programming in OCaml

Homework 3 COSE212, Fall 2018

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

Haskell: Lists. CS F331 Programming Languages CSCE A331 Programming Language Concepts Lecture Slides Friday, February 24, Glenn G.

CS Lecture 5: Pattern Matching. Prof. Clarkson Fall Today s music: Puff, the Magic Dragon by Peter, Paul & Mary

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

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

Programming Languages and Techniques (CIS120)

Recap from last time. Programming Languages. CSE 130 : Fall Lecture 3: Data Types. Put it together: a filter function

Programming Languages and Techniques (CIS120)

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

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

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

Programming Languages

A Programming Language. A different language g is a different vision of life. CSE 130 : Fall Two variables. L1: x++; y--; (y=0)?

Theorem Proving Principles, Techniques, Applications Recursion

Thinking Induc,vely. COS 326 David Walker Princeton University

Function definitions. CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists. Example, extended. Some gotchas. Zach Tatlock Winter 2018

CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists. Zach Tatlock Winter 2018

So what does studying PL buy me?

Programming Languages

CS 360: Programming Languages Lecture 10: Introduction to Haskell

Note that pcall can be implemented using futures. That is, instead of. we can use

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

Introduction to OCaml

Recap: ML s Holy Trinity

CS131 Typed Lambda Calculus Worksheet Due Thursday, April 19th

SML A F unctional Functional Language Language Lecture 19

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

Recap. Recap. If-then-else expressions. If-then-else expressions. If-then-else expressions. If-then-else expressions

Lecture: Functional Programming

CSE 130 [Spring 2014] Programming Languages

Booleans (aka Truth Values) Programming Languages and Compilers (CS 421) Booleans and Short-Circuit Evaluation. Tuples as Values.

Programming Languages

CVO103: Programming Languages. Lecture 5 Design and Implementation of PLs (1) Expressions

News. Programming Languages. Recap. Recap: Environments. Functions. of functions: Closures. CSE 130 : Fall Lecture 5: Functions and Datatypes

Programming Languages and Techniques (CIS120)

CSCI 2041: Pattern Matching Basics

Programming Languages and Techniques (CIS120)

Begin at the beginning

Programming Languages

Programming in Standard ML: Continued

Programming Languages and Techniques (CIS120)

CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists. Dan Grossman Fall 2011

The List Datatype. CSc 372. Comparative Programming Languages. 6 : Haskell Lists. Department of Computer Science University of Arizona

CS 11 Haskell track: lecture 1

Data Types The ML Type System

Lecture 2: Big-Step Semantics

CS 11 Ocaml track: lecture 2

Programming Languages

Programming Languages and Techniques (CIS120)

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

A Programming Language. A different language g is a different vision of life. CSE 130 : Spring Two variables. L1: x++; y--; (y=0)?

Programming Languages. So why study PL? A Programming Language. A different language is a different vision of life. CSE 130 : Fall 2015

Programming Languages. So why study PL? A Programming Language. A different language is a different vision of life. CSE 130 : Spring 2015

Programming Languages

Sometimes it s good to be lazy

Haskell Review COMP360

Tail Recursion: Factorial. Begin at the beginning. How does it execute? Tail recursion. Tail recursive factorial. Tail recursive factorial

Side note: Tail Recursion. Begin at the beginning. Side note: Tail Recursion. Base Types. Base Type: int. Base Type: int

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

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

Datatype declarations

Programming Languages and Techniques (CIS120)

CS 320: Concepts of Programming Languages

Informatics 1 Functional Programming Lecture 4. Lists and Recursion. Don Sannella University of Edinburgh

Programming Languages

Programming Languages and Techniques (CIS120)

CMSC 330: Organization of Programming Languages. OCaml Imperative Programming

Programming Languages and Techniques (CIS120)

Iteration and For Loops

If we have a call. Now consider fastmap, a version of map that uses futures: Now look at the call. That is, instead of

C++ Programming: From Problem Analysis to Program Design, Third Edition

Introduction to Objective Caml

Metaprogramming assignment 3

Programming Languages. Next: Building datatypes. Suppose I wanted. Next: Building datatypes 10/4/2017. Review so far. Datatypes.

Transcription:

Which of the following expressions has the type int list? 1) [3; true] 2) [1;2;3]::[1;2] 3) []::[1;2]::[] 4) (1::2)::(3::4)::[] 5) [1;2;3;4] Answer: 5

Which of the following expressions has the type (int list) list? 1) [3; true] 2) [1;2;3]::[1;2] 3) []::[1;2]::[] 4) (1::2)::(3::4)::[] 5) [1;2;3;4] Answer: 3

Programming Languages and Techniques () Lecture 4 January 19, 2018 Lists, Recursion and Tuples

Announcements Read Chapters 3 (Lists) and 4 (Tuples) of the lecture notes We will start Chapters 5 & 6 on Monday Weirich OH (next week only) Tues 3:30-5PM HW01 due Tuesday at midnight Clicker attendance starts Wednesday

A list is either: or Review: What is a list? [] the empty list, sometimes called nil v :: tail a head value v, followed by a list of the remaining elements, the tail The :: operator, pronounced cons, constructs a new list from a head element and a shorter list. Lists are an example of an inductive datatype.

The Inductive Definition of Lists A list is either: [] the empty list, sometimes called nil or v :: tail a head value v, followed by a list of the remaining elements, the tail What is going on!? The definition of list mentions list! Solution: list is inductive: The empty list [] is the (only) list of 0 elements To construct a list of (1+n) elements, add an element to an existing list of n elements The set of list values contains all and only values constructed this way Corresponding computation principle: recursion

Recursion Over Lists in Code The function calls itself recursively so the function declaration must be marked with rec. Lists are either empty or nonempty. Pattern matching determines which. let rec length (l : string list) : int = begin match l with [] -> 0 ( x :: rest ) -> 1 + length rest end If the list is non-empty, then x is the first string in the list and rest is the remainder of the list.

Example: Recursion Recursion principle: Compute a function value for a given input by combining the results for strictly smaller subcomponents of the input. length (1::2::3::[]) = 1 + length (2::3::[]) length (2::3::[]) = 1 + length (3::[]) length (3::[]) = 1 + length [] length [] = 0 let rec length (l:string list) : int = begin match l with [] -> 0 ( x :: rest ) -> 1 + length rest end

Interactive Interlude email.ml

Recursive function patterns Recursive functions over lists follow a general pattern: let rec length (l : string list) : int = begin match l with [] -> 0 ( x :: rest ) -> 1 + length rest end let rec contains (l:string list) (s:string) : bool = begin match l with [] -> false ( x :: rest ) -> (s = x) contains rest s end

Structural Recursion Over Lists Structural recursion builds an answer from smaller components: let rec f (l : list) : = begin match l with [] -> ( x :: rest ) -> f rest end The branch for [] calculates the value (f []) directly. this is the base case of the recursion The branch for x::rest calculates (f(x::rest)) given x and (f rest). this is the inductive case of the recursion

Tuples and Tuple Patterns

Forms of Structured Data OCaml provides two ways of packaging multiple values together into a single compound value: Lists: arbitrary-length sequence of values of a single, fixed type example: a list of email addresses Tuples: fixed-length sequence of values of arbitrary types example: tuple of name, phone #, and email

Tuples In OCaml, tuples are created by writing the values, separated by commas, in parentheses: let my_pair = (3, true) let my_triple = ( Hello, 5, false) let my_quaduple = (1,2, three,false) Tuple types are written using * e.g. my_triple has type: string * int * bool

Pattern Matching Tuples Tuples can be inspected by pattern matching: let first (x: string * int) : string = begin match x with (left, right) -> left end first ( b, 10) b As with lists, the pattern follows the syntax of the values, naming the subcomponents

Mixing Tuples and Lists Tuples and lists can mix freely: [(1, a ); (2, b ); (3, c )] : (int * string) list ([1;2;3], [ a ; b ; c ]) : (int list) * (string list)

Nested Patterns So far, we ve seen simple patterns: [] matches empty list x::tl matches nonempty list (a,b) matches pairs (tuples with 2 elts) (a,b,c) matches triples (tuples with 3 elts) Like expressions, patterns can nest: x :: [] matches lists with 1 element [x] matches lists with 1 element x::(y::tl) matches lists of length at least 2 (x::xs, y::ys) matches pairs of non-empty lists

Wildcard Pattern Another handy pattern is the wildcard pattern: ::tl matches a non-empty list, but only names tail (_,x) matches a pair, but only names the 2 nd part A wildcard pattern indicates that the value of the corresponding subcomponent is irrelevant. And hence needs no name.

Unused Branches The branches in a match expression are considered in order from top to bottom. If you have redundant matches, then some later branches might not be reachable. OCaml will give you a warning let bad_cases (l : int list) : int = begin match l with [] -> 0 x::_ -> x This case matches more lists than that one does. x::y::tl -> x + y (* unreachable *) end