CS 321 Programming Languages

Similar documents
Forward recursion. CS 321 Programming Languages. Functions calls and the stack. Functions calls and the stack

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

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

FUNCTIONAL PROGRAMMING

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

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

Programming Languages and Compilers (CS 421)

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

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

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

Forward Recursion. Programming Languages and Compilers (CS 421) Mapping Recursion. Forward Recursion: Examples. Folding Recursion.

CSCI-GA Scripting Languages

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

Functions Over Lists. Programming Languages and Compilers (CS 421) Structural Recursion. Functions Over Lists. Structural Recursion : List Example

Gain familiarity and practice with folding and mapping. The following supplementary materials may be helpful in completing this assignment:

School of Computer Science

List Processing in SML

Which of the following expressions has the type int list?

List Processing in SML

CS 11 Ocaml track: lecture 2

List Processing in Ocaml

List Functions, and Higher-Order Functions

Introduction to Objective Caml

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

Why OCaml? Introduction to Objective Caml. Features of OCaml. Applications written in OCaml. Stefan Wehr

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

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

CSCI 2041: First Class Functions

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

CS 340 Spring 2019 Midterm Exam

Introduction to OCaml

An introduction introduction to functional functional programming programming using usin Haskell

PROGRAMMING IN HASKELL. CS Chapter 6 - Recursive Functions

Programming Languages and Techniques (CIS120)

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

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

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

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

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

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

CSE 130 [Winter 2014] Programming Languages

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

Introduction to OCaml

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

CSE341 Section 3. Standard-Library Docs, First-Class Functions, & More

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

02157 Functional Programming. Michael R. Ha. Disjoint Unions and Higher-order list functions. Michael R. Hansen

Programming Languages

CSE 341 : Programming Languages Midterm, Spring 2015

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120)

Introduction to Haskell

Programovací jazyky F# a OCaml. Chapter 5. Hiding recursion using function-as-values

CSCE 314 Programming Languages

SNU Programming Language Theory

Shell CSCE 314 TAMU. Higher Order Functions

CS3110 Spring 2016 Lecture 6: Modules

Principles of Programming Languages Topic: Functional Programming Professor L. Thorne McCarty Spring 2003

Topic 5: Examples and higher-order functions

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

Functional Programming. Pure Functional Programming

CPL 2016, week 10. Clojure functional core. Oleg Batrashev. April 11, Institute of Computer Science, Tartu, Estonia

CMSC 330, Fall 2013, Practice Problems 3

02157 Functional Programming Tagged values and Higher-order list functions

Patterns The Essence of Functional Programming

F# - LISTS. In this method, you just specify a semicolon-delimited sequence of values in square brackets. For example

Programming Languages and Compilers (CS 421)

Some Advanced ML Features

Lists. Adrian Groza. Department of Computer Science Technical University of Cluj-Napoca

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

CS 457/557: Functional Languages

MoreIntro_annotated.v. MoreIntro_annotated.v. Printed by Zach Tatlock. Oct 04, 16 21:55 Page 1/10

n What is its running time? 9/18/17 2 n poor_rev [1,2,3] = n (poor_rev [1] = n ((poor_rev [1] =

Programming Languages and Techniques (CIS120)

Thinking Induc,vely. COS 326 David Walker Princeton University

CSc 372 Comparative Programming Languages

02157 Functional Programming. Michael R. Ha. Tagged values and Higher-order list functions. Michael R. Hansen

CSE341 Spring 2017, Midterm Examination April 28, 2017

Programming Languages

CSc 372. Comparative Programming Languages. 8 : Haskell Function Examples. Department of Computer Science University of Arizona

CIS 120 Midterm I February 16, 2015 SOLUTIONS

Part I. Language Core

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

CSE341, Fall 2011, Midterm Examination October 31, 2011

CS1 Lecture 3 Jan. 18, 2019

Programming Languages

CS 135 Winter 2018 Tutorial 7: Accumulative Recursion and Binary Trees. CS 135 Winter 2018 Tutorial 7: Accumulative Recursion and Binary Trees 1

max function Next concat function max function What s the pattern? concat function More on recursion Higher-order functions

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

CSci 4223 Principles of Programming Languages

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

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

Programming Languages and Compilers (CS 421)

CMSC 330, Fall 2013, Practice Problem 3 Solutions

CS 360: Programming Languages Lecture 10: Introduction to Haskell

OCaml Style Guide Fall 2017

Cornell University 12 Oct Solutions. (a) [9 pts] For each of the 3 functions below, pick an appropriate type for it from the list below.

Programming Languages and Techniques (CIS120)

Chapter 3 Linear Structures: Lists

CMSC 330: Organization of Programming Languages

Transcription:

CS 321 Programming Languages Intro to OCaml Lists Baris Aktemur Özyeğin University Last update made on Monday 2 nd October, 2017 at 19:27. Some of the contents here are taken from Elsa Gunter and Sam Kamin s OCaml notes available at http://courses.engr.illinois.edu/cs421 Özyeğin University CS 321 Programming Languages 1 Lists First example of a recursive datatype (aka algebraic datatype). Unlike tuples, lists are homogeneous in type (all elements same type). A list has two forms. Empty: written as []. Non-empty: with a head element and a tail, written as x::xs. The tail of a list is a list of the same type. :: operation, read as cons, combines a head element and a tail. Syntactic sugar: [x] is x::[] [x1;x2;x3;...;xn] is x1::x2::x3::...::xn::[] Özyeğin University CS 321 Programming Languages 2

Lists # [];; - : a list = [] # [1];; - : int list = [1] # 1::[];; - : int list = [1] # [1;2;3;4];; - : int list = [1; 2; 3; 4] # 1::2::[3;4];; - : int list = [1; 2; 3; 4] # let a::b = [1;2;3;4];; (* Pattern matching on lists *) (* A warning suppressed *) val a : int = 1 val b : int list = [2; 3; 4] Özyeğin University CS 321 Programming Languages 3 Lists are homogeneous # let badlist = [1; 2.3; 5];; Error: This expression has type float but an expression was expected of type int Özyeğin University CS 321 Programming Languages 4

Lists # [2.5; 3.8; 0.77];; - : float list = [2.5; 3.8; 0.77] # [true; false; true];; - : bool list = [true; false; true] # [ a ; b ; c ; d ];; - : char list = [ a ; b ; c ; d ] # ["hello"; "world"];; - : string list = ["hello"; "world"] # [[1;2]; [3;4;5]; []; [6]];; - : int list list = [[1; 2]; [3; 4; 5]; []; [6]] # [2,3];; - : (int * int) list = [(2, 3)] Özyeğin University CS 321 Programming Languages 5 Lists are homogeneous Which of the following lists is invalid? 1. [2; 3; 4; 6] 2. [2.3; 4.5; 6.7] 3. [2,3; 4,5; 6,7] 4. [2,3.4; 4,5.6; 6.8,7] 5. [["hi"; "there"]; ["whatcha"]; []; ["doin"]] Özyeğin University CS 321 Programming Languages 6

Exercise What are the types? (or flag error) # [ a ; b ];; # [ a ; b ; "c"];; # [(1,2); (3,4)];; # [(1,2); (3,4,5)];; # [(1,[2]); (3,[4;5])];; # [ a, b ];; Özyeğin University CS 321 Programming Languages 7 Exercise Provide values (other than empty list) to form lists of given types. #???;; - : int list #???;; - : int list list #???;; - : (int * string) list #???;; - : string list list #???;; - : (int * string list) list #???;; - : (int * string list) list list Özyeğin University CS 321 Programming Languages 8

Purity cons operation is pure ; it does not destroy/modify existing lists, but rather it constructs a new one. # let lst = [2;3;4;5];; val lst : int list = [2; 3; 4; 5] # 1::lst;; - : int list = [1; 2; 3; 4; 5] # lst;; - : int list = [2; 3; 4; 5] Özyeğin University CS 321 Programming Languages 9 Lists # let a::b::c = [1;2;3;4];; (* Pattern matching on lists *) (* A warning suppressed *) val c : int list = [3; 4] val b : int = 2 val a : int = 1 # let a::_::c = [1;2;3;4];; (* Pattern matching on lists *) (* A warning suppressed *) val c : int list = [3; 4] val a : int = 1 # let list1 = [1;2;3;4];; val list1 : int list = [1; 2; 3; 4] # let list2 = [5;6;7];; val list2 : int list = [5; 6; 7] # list1@list2;; (* Append lists *) - : int list = [1; 2; 3; 4; 5; 6; 7] Özyeğin University CS 321 Programming Languages 10

match expression for pattern-matching # let rec power x n = match n with 0 -> 1 m -> x * power x (n - 1);; val power : int -> int -> int = <fun> Özyeğin University CS 321 Programming Languages 11 match expression for pattern-matching # let foo triple = match triple with (0, x, y) -> 1 (x, 0, y) -> 2 (x, y, 0) -> 3 _ -> 4;; val foo : int * int * int -> int = <fun> # foo (0,3,4);; - : int = 1 # foo (3,0,4);; - : int = 2 # foo (3,0,0);; - : int = 2 # foo (0,0,0);; - : int = 1 # foo (1,2,3);; - : int = 4 Özyeğin University CS 321 Programming Languages 12

match expression for pattern-matching # let incomplete n = match n with 0 -> "zero" 1 -> "one";; Warning 8: this pattern-matching is not exhaustive. Here is an example of a value that is not matched: 2 val incomplete : int -> string = <fun> Özyeğin University CS 321 Programming Languages 13 match expression for pattern-matching # let redundant n = match n with 0 -> "zero" m -> "number" 1 -> "one";; 1 -> "one";; ----^ Warning 11: this match case is unused. val redundant : int -> string = <fun> Özyeğin University CS 321 Programming Languages 14

Functions on lists # let headof lst = [] -> failwith "Empty list doesn t have a head" x::_ -> x;; (* don t care the tail *) val headof : a list -> a = <fun> # let tailof lst = [] -> failwith "Empty list doesn t have a tail" _::xs -> xs;; (* don t care the head *) val tailof : a list -> a list = <fun> Özyeğin University CS 321 Programming Languages 15 Functions on lists # headof [3;5;7;9];; - : int = 3 # tailof [3;5;7;9];; - : int list = [5; 7; 9] # tailof(tailof [3;5;7;9]);; - : int list = [7; 9] # headof(tailof [3;5;7;9]);; - : int = 5 (* head and tail already defined in the library *) # List.hd [1;2;3];; - : int = 1 # List.tl [1;2;3];; - : int list = [2; 3] Özyeğin University CS 321 Programming Languages 16

Functions on lists # let secondelementof lst = x::y::rest -> y;; Warning 8: this pattern-matching is not exhaustive. Here is an example of a value that is not matched: (_::[] []) val secondelementof : a list -> a # secondelementof [1;2;3;4;5];; - : int = 2 # secondelementof [1];; Exception: Match_failure ("//toplevel//", 20, 4). Özyeğin University CS 321 Programming Languages 17 Exercise Write a function to compute the sum of the first two elements of an int list. You can assume the list is of length at least 2. # let addfirsttwo... # addfirsttwo [5; 3; 2; 6];; - : int = 8 Özyeğin University CS 321 Programming Languages 18

Functions on lists # let rec lengthof lst = [] -> 0 x::xs -> 1 + lengthof xs;; val lengthof : a list -> int # lengthof [];; - : int = 0 # lengthof [1;2;3;4;5];; - : int = 5 # List.length [1;2;3;4];; (* defined in the library *) - : int = 4 Özyeğin University CS 321 Programming Languages 19 Exercise # let rec tally lst =???;; val tally : int list -> int # tally [];; - : int = 0 # tally [1;2;3;4;5;6];; - : int = 21 Özyeğin University CS 321 Programming Languages 20

Exercise Write a function to compute the sum of the lengths of the first two elements of an (int list) list. You can assume the list is of length at least 2: # let addfirsttwolengths... # addfirsttwolengths [[5; 3]; [2]; [6; 2; 5; 3]];; - : int = 3 Özyeğin University CS 321 Programming Languages 21 Exercise # let rec numzeros lst = val numzeros : int list -> int # numzeros [];; - : int = 0 # numzeros [1;2;3;4;5];; - : int = 0 # numzeros [1;2;0;4;0];; - : int = 2 Özyeğin University CS 321 Programming Languages 22

Exercise # let rec numzeros lst = [] -> 0 0::xs -> 1 + numzeros xs _::xs -> numzeros xs;; val numzeros : int list -> int # numzeros [];; - : int = 0 # numzeros [1;2;3;4;5];; - : int = 0 # numzeros [1;2;0;4;0];; - : int = 2 Özyeğin University CS 321 Programming Languages 22 Functions on lists (* Alternatively *) # let rec numzeros lst = [] -> 0 x::xs -> (if x = 0 then 1 else 0) + numzeros xs;; val numzeros : int list -> int Özyeğin University CS 321 Programming Languages 23

Functions on lists (* Yet another alternative *) # let rec numzeros lst = [] -> 0 x::xs when x = 0 -> 1 + numzeros xs x::xs -> numzeros xs;; val numzeros : int list -> int Özyeğin University CS 321 Programming Languages 24 Exercise # let rec doubleup lst =???;; val doubleup : a list -> a list # doubleup [1;2;3;4];; - : int list = [1; 1; 2; 2; 3; 3; 4; 4] Özyeğin University CS 321 Programming Languages 25

Functions on lists # let rec poorrev lst = [] -> [] x::xs -> poorrev xs @ [x];; val poorrev : a list -> a list # poorrev [1;2;3;4;5];; - : int list = [5; 4; 3; 2; 1] Özyeğin University CS 321 Programming Languages 26 Exercise zipadd Define a function zipadd that takes two integer lists, and returns a list that contains the sum of corresponding elements in its input lists. You may assume that the input lists are of the same length. # let rec zipadd lst1 lst2 = val zipadd : lst1:int list -> lst2:int list -> int list # zipadd [1;2;3;4;5] [6;7;8;9;10];; - : int list = [7; 9; 11; 13; 15] Özyeğin University CS 321 Programming Languages 27

Exercise zipadd Define a function zipadd that takes two integer lists, and returns a list that contains the sum of corresponding elements in its input lists. You may assume that the input lists are of the same length. # let rec zipadd lst1 lst2 = (* pattern-match a pair of lists! *) match lst1, lst2 with [],[] -> [] x::xs, y::ys -> (x+y)::zipadd xs ys;; (* Incomplete pattern-matching warning omitted *) val zipadd : lst1:int list -> lst2:int list -> int list # zipadd [1;2;3;4;5] [6;7;8;9;10];; - : int list = [7; 9; 11; 13; 15] Özyeğin University CS 321 Programming Languages 27 Exercise zipadd, Alternative implementation You can write any expression corresponding to the case of a match, including another match. # let rec zipadd lst1 lst2 = match lst1 with [] -> [] x::xs -> (match lst2 with [] -> failwith "This shouldn t happen." y::ys -> (x+y)::zipadd xs ys);; val zipadd : lst1:int list -> lst2:int list -> int list # zipadd [1;2;3;4;5] [6;7;8;9;10];; - : int list = [7; 9; 11; 13; 15] Özyeğin University CS 321 Programming Languages 28

Functions on lists map Define a function map such that map f [x 1 ; x 2 ;...;x n ] computes [f(x 1 ); f(x 2 );...;f(x n )] # let rec map f lst = [] -> [] x::xs -> f x :: map f xs;; val map : ( a -> b) -> a list -> b list Özyeğin University CS 321 Programming Languages 29 Exercise: map # let rec map f lst = [] -> [] x::xs -> f x :: map f xs;; val map : ( a -> b) -> a list -> b list # map (fun n -> n + 2) [1;2;3;4;5];; - : int list = [3; 4; 5; 6; 7] # map (fun s -> s ^ "!") ["a"; "b"; "c"];; - : string list = ["a!"; "b!"; "c!"] # map (???) [1;2;3;4;5];; - : int list = [1; 4; 9; 16; 25] # map (???) [1;-2;3;-4;5;0;-99];; - : int list = [1; 2; 3; 4; 5; 0; 99] (* map is defined in the library s List module *) # List.map abs [1;-2;3;-4;5;0;-99];; - : int list = [1; 2; 3; 4; 5; 0; 99] Özyeğin University CS 321 Programming Languages 30

Iterating over lists fold left # let rec fold_left f a lst = [] -> a x::xs -> fold_left f (f a x) xs;; val fold_left : ( a -> b -> a) -> a -> b list -> a fold left is an extremely important function that is used frequently. fold left f a [x 1 ;x 2 ;...;x n ] computes f(...(f (f a x 1 ) x 2 )...)x n. So, f takes two arguments: (1) the accumulated value over the list from the left, (2) the current element of the list. a is the initial value of accumulation, which is also the result if the list is empty. Özyeğin University CS 321 Programming Languages 31 Exercise: fold left (* tally *) # fold_left (fun acc x -> acc + x) 0 [1;2;3;4;5;6];; - : int = 21 (* Or, also as *) # fold_left (+) 0 [1;2;3;4;5;6];; - : int = 21 (* lengthof *) # fold_left (fun acc x -> acc + 1) 0 [4;9;0;45;3;6];; - : int = 6 (* numzeros *) # fold_left (fun acc x ->??? ) 0 [4;9;0;45;0;0];; - : int = 3 (* fold_left is already defined in the library *) # List.fold_left (fun acc x -> acc + x) 0 [1;2;3;4;5;6];; - : int = 21 Özyeğin University CS 321 Programming Languages 32

Iterating over lists fold right # let rec fold_right f lst a = [] -> a x::xs -> f x (fold_right f xs a);; val fold_right : ( a -> b -> b) -> a list -> b -> b fold right is the other extremely important function that is used frequently to iterate over lists. fold right f [x 1 ;x 2 ;...;x n ] a computes f x 1 (f x 2 (... (f x n a)...)). So, f takes two arguments: (1) the current element of the list, (2) the accumulated value over the list from the right. a is the initial value of accumulation, which is also the result if the list is empty. Özyeğin University CS 321 Programming Languages 33 Exercise: fold right (* tally *) # fold_right (fun x a ->??? ) [1;2;3;4;5;6] 0;; - : int = 21 # fold_right ( * ) [1;2;3;4;5;6] 1;; - : int = 720 (* squareup *) # fold_right (fun x a ->??? ) [1;2;3;4;5;6] [];; - : int list = [1; 4; 9; 16; 25; 36] (* fold_right is already defined in the library *) # List.fold_right;; - : ( a -> b -> b) -> a list -> b -> b = <fun> # List.fold_right (fun x a -> if x=0 then a else x::a) [1;0;3;0;5;6;0] [];; - : int list = [1; 3; 5; 6] Özyeğin University CS 321 Programming Languages 34

Exercise (* Reversing a list, written with fold_left and fold_right *) # fold_left (fun a x ->??? ) [] [1;2;3;4;5;6];; - : int list = [6; 5; 4; 3; 2; 1] # fold_right (fun x a ->??? ) [1;2;3;4;5;6] [];; - : int list = [6; 5; 4; 3; 2; 1] (* Which one is more efficient? *) Özyeğin University CS 321 Programming Languages 35 List functions The List module contains very useful functions. See the API documentation at http://caml.inria.fr/pub/docs/manual-ocaml/libref/ List.html Some important functions, in addition to map, fold left and fold right. E.g.: rev, flatten, mem, filter. Özyeğin University CS 321 Programming Languages 36

Loops Question What happened to while/for loops? Fact In functional programming, you seldomly use while/for loops, which are highly associated with procedural/imperative programming. You excessively use recursion instead. Recursion is more powerful than simple loops, and if you form your recursion right, you don t compromise performance. (will talk about this soon) Özyeğin University CS 321 Programming Languages 37