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

Similar documents
COSE212: Programming Languages. Lecture 3 Functional Programming in OCaml

Homework 3 COSE212, Fall 2018

CS205: Scalable Software Systems

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

F28PL1 Programming Languages. Lecture 14: Standard ML 4

PROGRAMMING IN HASKELL. CS Chapter 6 - Recursive Functions

School of Computer Science

CSE 341 Lecture 5. efficiency issues; tail recursion; print Ullman ; 4.1. slides created by Marty Stepp

COSE212: Programming Languages. Lecture 0 Course Overview

CS205: Scalable Software Systems

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

Thinking Induc,vely. COS 326 David Walker Princeton University

Poly-HO! COS 326 David Walker Princeton University. polymorphic, higher-order programming

CMSC 330, Fall 2013, Practice Problems 3

Using Scala in CS241

Today s Plan. Programming Languages. Example : Factorial. Recursion. CSE 130 : Spring Lecture 6: Higher-Order Functions

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

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

4. Functional Programming Language-Oriented Programming

Example Scheme Function: equal

Chapter 12 Supplement: Recursion with Java 1.5. Mr. Dave Clausen La Cañada High School

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

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

Some Advanced ML Features

FUNCTIONAL PROGRAMMING

Introduction to OCaml

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

Chapter 15. Functional Programming Languages

CS Lectures 2-3. Introduction to OCaml. Polyvios Pratikakis

CPS 506 Comparative Programming Languages. Programming Language Paradigm

Problem Set CVO 103, Spring 2018

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

CSCI 2041: Tail Recursion and Activation Records

Programming Languages. Tail Recursion. CSE 130: Winter Lecture 8: NOT TR. last thing function does is a recursive call

Functional Programming. Introduction To Cool

Lecture 35: Analysis Review & Tail Recursion 10:00 AM, Nov 28, 2018

The Haskell HOP: Higher-order Programming

CSE 341: Programming Languages

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

MP 2 Continuation-Passing Style

Introduction to Functional Programming

Interpreters and Tail Calls Fall 2017 Discussion 8: November 1, 2017 Solutions. 1 Calculator. calc> (+ 2 2) 4

CS3110 Spring 2016 Lecture 6: Modules

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

INF121: Functional Algorithmic and Programming

Programming Languages

Fall 2017 December 4, Scheme. Instructions

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

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

Introduction to OCaml

CSE11 Lecture 20 Fall 2013 Recursion

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

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

Fun facts about recursion

Abstraction and Specification

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

CS 321 Programming Languages

Programming Languages

Cunning Plan. One-Slide Summary. Functional Programming. Functional Programming. Introduction to COOL #1. Classroom Object-Oriented Language

Structure and Interpretation of Computer Programs

Programming Languages

CS 11 Ocaml track: lecture 2

CMSC 330, Fall 2013, Practice Problem 3 Solutions

Week - 03 Lecture - 18 Recursion. For the last lecture of this week, we will look at recursive functions. (Refer Slide Time: 00:05)

COMP-202: Foundations of Programming. Lecture 13: Recursion Sandeep Manjanna, Summer 2015

CMSC330 Spring 2014 Midterm 2 Solutions

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

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

Shell CSCE 314 TAMU. Functions continued

CS159. Nathan Sprague. November 9, 2015

CMSC330 Fall 2014 Midterm 2 Solutions

CSCI 2041: Lists and Recursion

INTERPRETERS 8. 1 Calculator COMPUTER SCIENCE 61A. November 3, 2016

TAIL CALLS, ITERATORS, AND GENERATORS 11

Recursion. Garfield AP Computer Science. As usual, significant borrowings from Stuart Reges and Marty Stepp at UW -- thanks!!

CSE 143 Lecture 10. Recursion

Thinking Induc,vely. COS 326 David Walker Princeton University

Higher-Order Functions

Recursion. What is Recursion? Simple Example. Repeatedly Reduce the Problem Into Smaller Problems to Solve the Big Problem

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

CSE341: Programming Languages Lecture 6 Nested Patterns Exceptions Tail Recursion. Zach Tatlock Winter 2018

ALGORITHM 2-1 Solution for Exercise 4

Begin at the beginning

Introduction to Delimited Continuations. Typing Printf. Printf. Kenichi Asai. Ochanomizu University. April 13, 2008

Programming Languages

Name: Pennkey: CIS 120 Midterm II November 18, 2011

Lecture 19: Recursion

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

Recursion & Iteration

CSCC24 Functional Programming Scheme Part 2

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

G Programming Languages - Fall 2012

Functional Programming. Pure Functional Programming

Introduction to ML. Mooly Sagiv. Cornell CS 3110 Data Structures and Functional Programming

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

Lecture: Functional Programming

List Processing in Ocaml

Recursion: Factorial (1) Recursion. Recursion: Principle. Recursion: Factorial (2) Recall the formal definition of calculating the n factorial:

Fall 2017 December 4, 2017

Transcription:

COSE212: Programming Languages Lecture 4 Recursive and Higher-Order Programming Hakjoo Oh 2016 Fall Hakjoo Oh COSE212 2016 Fall, Lecture 4 September 27, 2016 1 / 21

Recursive and Higher-Order Programming Heavily used in functional programming languages: Recursive programming provides a powerful problem-solving method. Higher-order programming provides expressiveness. Hakjoo Oh COSE212 2016 Fall, Lecture 4 September 27, 2016 2 / 21

Why Recursive Thinking? Describe an algorithm to draw the following: Hakjoo Oh COSE212 2016 Fall, Lecture 4 September 27, 2016 3 / 21

Recursive Problem-Solving Strategy If the problem is sufficiently small, directly solve the problem. Otherwise, 1 Split the problem up into smaller problems with the same structure as original. 2 Solve each of those smaller problems. 3 Combine the results to get the overall solution. Hakjoo Oh COSE212 2016 Fall, Lecture 4 September 27, 2016 4 / 21

Example: list length If the list is empty, the length is 0. Otherwise, In OCaml: 1 The list can be split into its head and tail. 2 Compute the lengths of the tail. 3 The overall solution is the length of the tail plus one. let rec length l = [] -> 0 hd::tl -> 1 + length tl Hakjoo Oh COSE212 2016 Fall, Lecture 4 September 27, 2016 5 / 21

Exercise 1: append Write a function that appends two lists: # append [1; 2; 3] [4; 5; 6; 7];; - : int list = [1; 2; 3; 4; 5; 6; 7] # append [2; 4; 6] [8; 10];; - : int list = [2; 4; 6; 8; 10] Hakjoo Oh COSE212 2016 Fall, Lecture 4 September 27, 2016 6 / 21

Exercise 2: reverse Write a function that reverses a given list: val reverse : a list -> a list = <fun> # reverse [1; 2; 3];; - : int list = [3; 2; 1] # reverse ["C"; "Java"; "OCaml"];; - : string list = ["OCaml"; "Java"; "C"] Hakjoo Oh COSE212 2016 Fall, Lecture 4 September 27, 2016 7 / 21

Exercise 3: nth-element Write a function that computes nth element of a list: # nth [1;2;3] 0;; - : int = 1 # nth [1;2;3] 1;; - : int = 2 # nth [1;2;3] 2;; - : int = 3 # nth [1;2;3] 3;; Exception: Failure "list is too short". let rec nth l n = [] -> raise (Failure "list is too short") hd::tl -> (*... *) Hakjoo Oh COSE212 2016 Fall, Lecture 4 September 27, 2016 8 / 21

Exercise 4: remove-first Write a function that removes the first occurrence of an element from a list: # remove_first 2 [1; 2; 3];; - : int list = [1; 3] # remove_first 2 [1; 2; 3; 2];; - : int list = [1; 3; 2] # remove_first 4 [1;2;3];; - : int list = [1; 2; 3] # remove_first [1; 2] [[1; 2; 3]; [1; 2]; [2; 3]];; - : int list list = [[1; 2; 3]; [2; 3]] let rec remove_first a l = [] -> [] hd::tl -> (*... *) Hakjoo Oh COSE212 2016 Fall, Lecture 4 September 27, 2016 9 / 21

Exercise 5: insert Write a function that inserts an element to a sorted list: # insert 2 [1;3];; - : int list = [1; 2; 3] # insert 1 [2;3];; - : int list = [1; 2; 3] # insert 3 [1;2];; - : int list = [1; 2; 3] # insert 4 [];; - : int list = [4] let rec insert a l = [] -> [a] hd::tl -> (*... *) Hakjoo Oh COSE212 2016 Fall, Lecture 4 September 27, 2016 10 / 21

Exercise 6: insertion sort Write a function that performs insertion sort: let rec sort l = [] -> [] hd::tl -> insert hd (sort tl) cf) Compare with C-style non-recursive version: for (c = 1 ; c <= n - 1; c++) { d = c; while ( d > 0 && array[d] < array[d-1]) { t = array[d]; } } array[d] array[d-1] = t; d--; = array[d-1]; Hakjoo Oh COSE212 2016 Fall, Lecture 4 September 27, 2016 11 / 21

Recursion in ML is Not Expensive In languages like C, recursion should be avoided because function call consumes additional memory: void f() { f(); } /* stack overflow */ The same program in ML iterates forever: let rec f () = f () Hakjoo Oh COSE212 2016 Fall, Lecture 4 September 27, 2016 12 / 21

Tail-Recursive Functions More precisely, tail-recursive functions are not expensive in ML. A recursive call is a tail call if there is nothing to do after the function returns. let rec last l = [a] -> a _::tl -> last tl let rec factorial a = if a = 1 then 1 else a * factorial (a - 1) Languages like ML, Scheme, Scala, and Haskell do tail-call optimization, so that tail-recursive calls do not consume additional amount of memory. Hakjoo Oh COSE212 2016 Fall, Lecture 4 September 27, 2016 13 / 21

Transforming to Tail-Recursive Functions Non-tail-recursive factorial: let rec factorial a = if a = 1 then 1 else a * factorial (a - 1) Tail-recursive version: let rec fact product counter maxcounter = if counter > maxcounter then product else fact (product * counter) (counter + 1) maxcounter let factorial n = fact 1 1 n Hakjoo Oh COSE212 2016 Fall, Lecture 4 September 27, 2016 14 / 21

Higher-Order Functions Higher-order functions: functions that take other functions or return functions as results a powerful tool for code reuse Hakjoo Oh COSE212 2016 Fall, Lecture 4 September 27, 2016 15 / 21

Example 1: map Three similar functions: let rec inc_all l = [] -> [] hd::tl -> (hd+1)::(inc_all tl) let rec square_all l = [] -> [] hd::tl -> (hd*hd)::(square_all tl) let rec cube_all l = [] -> [] hd::tl -> (hd*hd*hd)::(cube_all tl) Hakjoo Oh COSE212 2016 Fall, Lecture 4 September 27, 2016 16 / 21

Example 1: map The code pattern can be captured by the higher-order function map: let rec map f l = [] -> [] hd::tl -> (f hd)::(map f tl) With map, the functions can be defined as follows: let inc x = x + 1 let inc_all l = map inc l let square x = x * x let square_all l = map square l let cube x = x * x * x let cube_all l = map cube l Or, using nameless functions: let inc_all l = map (fun x -> x + 1) l let square_all l = map (fun x -> x * x) l let cub_all l = map (fun x -> x * x * x) l Hakjoo Oh COSE212 2016 Fall, Lecture 4 September 27, 2016 17 / 21

Example 2: fold Two similar functions: let rec sum l = [] -> 0 hd::tl -> hd + (sum tl) let rec prod l = [] -> 1 hd::tl -> hd * (prod tl) # sum [1; 2; 3; 4];; - : int = 10 # prod [1; 2; 3; 4];; - : int = 24 Hakjoo Oh COSE212 2016 Fall, Lecture 4 September 27, 2016 18 / 21

Example 2: fold The two functions have the following form: sum [x1; x2;...; xn] = x1 + (x2 + (... + (xn + 0))) prod [x1; x2;...; xn] = x1 * (x2 * (... * (xn * 1))) This pattern is captured by fold: fold f [x1; x2;...; xn] a = f x1 (f x2 (... (f xn a))) let sum lst = fold (fun x y -> x + y) lst 0 let prod lst = fold (fun x y -> x * y) lst 1 or, let sum l = fold (+) l 0 let prod l = fold (*) l 1 Hakjoo Oh COSE212 2016 Fall, Lecture 4 September 27, 2016 19 / 21

Example 2: fold The definition of fold: let rec fold f l a = [] -> a hd::tl -> f hd (fold f tl a) Hakjoo Oh COSE212 2016 Fall, Lecture 4 September 27, 2016 20 / 21

Exercises Re-write the following functions in one-line using fold: let rec length l = [] -> 0 hd::tl -> 1 + length tl let rec reverse l = [] -> [] hd::tl -> (reverse tl) @ [hd] let rec is_all_pos l = [] -> true hd::tl -> (hd > 0) && (is_all_pos tl) Hakjoo Oh COSE212 2016 Fall, Lecture 4 September 27, 2016 21 / 21