PROGRAMMING IN HASKELL. CS Chapter 6 - Recursive Functions

Similar documents
Shell CSCE 314 TAMU. Functions continued

PROGRAMMING IN HASKELL. Chapter 5 - List Comprehensions

Introduction to Programming: Lecture 6

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

Programming Languages 3. Definition and Proof by Induction

Programming in Haskell Aug-Nov 2015

Informatics 1 Functional Programming Lecture 11. Data Representation. Don Sannella University of Edinburgh

Topic 5: Examples and higher-order functions

Informatics 1 Functional Programming Lecture 12. Data Abstraction. Don Sannella University of Edinburgh

The University of Nottingham

Functional Programming in Haskell Part I : Basics

Week 5 Tutorial Structural Induction

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

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

CSCE 314 Programming Languages

An introduction introduction to functional functional programming programming using usin Haskell

A general introduction to Functional Programming using Haskell

Shell CSCE 314 TAMU. Haskell Functions

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

Shell CSCE 314 TAMU. Higher Order Functions

More fun with recursion

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

Standard prelude. Appendix A. A.1 Classes

PROGRAMMING IN HASKELL. Chapter 2 - First Steps

INTRODUCTION TO FUNCTIONAL PROGRAMMING

Introduction to Programming, Aug-Dec 2006

Foundations of Computation

Programming Language Concepts: Lecture 14

Patterns The Essence of Functional Programming

Haskell 101. (Version 1 (July 18, 2012)) Juan Pedro Villa Isaza

School of Computer Science

INTRODUCTION TO HASKELL

Advanced Type System Features Tom Schrijvers. Leuven Haskell User Group

1 The smallest free number

Lecture 2: List algorithms using recursion and list comprehensions

QuickCheck, SmallCheck & Reach: Automated Testing in Haskell. Tom Shackell

Practical Haskell. An introduction to functional programming. July 21, Practical Haskell. Juan Pedro Villa-Isaza. Introduction.

Lecture 19: Functions, Types and Data Structures in Haskell

CS 457/557: Functional Languages

Haskell: From Basic to Advanced. Part 2 Type Classes, Laziness, IO, Modules

CS 340 Spring 2019 Midterm Exam

It is better to have 100 functions operate one one data structure, than 10 functions on 10 data structures. A. Perlis

CS 320: Concepts of Programming Languages

Haskell through HUGS THE BASICS

CSc 520 Principles of Programming Languages. Currying Revisited... Currying Revisited. 16: Haskell Higher-Order Functions

CSc 372. Comparative Programming Languages. 11 : Haskell Higher-Order Functions. Department of Computer Science University of Arizona

Part 20. Sorting and Selection

CSc 372 Comparative Programming Languages

Logic - CM0845 Introduction to Haskell

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

CS 320 Midterm Exam. Fall 2018

Solution sheet 1. Introduction. Exercise 1 - Types of values. Exercise 2 - Constructors

General Computer Science (CH ) Fall 2016 SML Tutorial: Practice Problems

Reasoning about programs. Chapter 9 of Thompson

Functional Programming and Haskell

CSc 372. Comparative Programming Languages. 15 : Haskell List Comprehension. Department of Computer Science University of Arizona

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

CS 320: Concepts of Programming Languages

Functional Programming Mid-term exam Tuesday 3/10/2017

Haskell Overview II (2A) Young Won Lim 8/9/16

Haskell Types, Classes, and Functions, Currying, and Polymorphism

Then you were asked to paint another fence that is 2m high and 400m long for the price of $80. Should you accept it?

An Introduction to Programming in Haskell. Gabriel J. Ferrer Hendrix College

Extended Static Checking for Haskell (ESC/Haskell)

List Functions, and Higher-Order Functions

(ii) Define a function ulh that takes a list xs, and pairs each element with all other elements in xs.

EDAF40. 2nd June :00-19:00. WRITE ONLY ON ONE SIDE OF THE PAPER - the exams will be scanned in and only the front/ odd pages will be read.

Efficient Mergesort. Christian Sternagel. August 28, 2014

PROGRAMMING IN HASKELL. Chapter 2 - First Steps

Defining Functions. CSc 372. Comparative Programming Languages. 5 : Haskell Function Definitions. Department of Computer Science University of Arizona

PROGRAMMING IN HASKELL. Chapter 2 - First Steps

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

FUNCTIONAL PEARLS The countdown problem

List Processing in Ocaml

Introduction. chapter Functions

Functional Programming TDA 452, DIT 142

Searching in General

Advanced features of Functional Programming (Haskell)

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

CSci 450: Org. of Programming Languages Overloading and Type Classes

Haskell An Introduction

301AA - Advanced Programming

! " Haskell Expressions are treated as: !" Thunks. !" Closures. !" Delayed Computations. !" Suspensions. ! " Expressions are evaluated: !

CS 209 Functional Programming

Combining Static and Dynamic Contract Checking for Curry

Haskell Overview II (2A) Young Won Lim 8/23/16

CSCE 314 Programming Languages

Recursion defining an object (or function, algorithm, etc.) in terms of itself. Recursion can be used to define sequences

Introduction to Haskell

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

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

State Monad (3D) Young Won Lim 9/25/17

Testing. Wouter Swierstra and Alejandro Serrano. Advanced functional programming - Lecture 2. [Faculty of Science Information and Computing Sciences]

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

Functional Programming. Overview. Topics. Definition n-th Fibonacci Number. Graph

Programming Languages Fall 2013

Haskell Revision and Exercises

Let s Talk About Logic

CSCE 314 Programming Languages

Transcription:

PROGRAMMING IN HASKELL CS-205 - Chapter 6 - Recursive Functions 0

Introduction As we have seen, many functions can naturally be defined in terms of other functions. factorial :: Int Int factorial n product [1..n] factorial maps any integer n to the product of the integers between 1 and n. 1

Expressions are evaluated by a stepwise process of applying functions to their arguments. For example: factorial 4 product [1..4] product [1,2,3,4] 1*2*3*4 24 2

Recursive Functions In Haskell, functions can also be defined in terms of themselves. Such functions are called recursive. factorial 0 1 factorial n n * factorial (n-1) factorial maps 0 to 1, and any other integer to the product of itself and the factorial of its predecessor. 3

For example: factorial 3 3 * factorial 2 3 * (2 * factorial 1) 3 * (2 * (1 * factorial 0)) 3 * (2 * (1 * 1)) 3 * (2 * 1) 3 * 2 6 4

Note: factorial 0 1 is appropriate because 1 is the identity for multiplication: 1*x x x*1. The recursive definition diverges on integers 0 because the base case is never reached: > factorial (-1) Exception: stack overflow 5

Why is Recursion Useful? Some functions, such as factorial, are simpler to define in terms of other functions. As we shall see, however, many functions can naturally be defined in terms of themselves. Properties of functions defined using recursion can be proved using the simple but powerful mathematical technique of induction. 6

Recursion on Lists Recursion is not restricted to numbers, but can also be used to define functions on lists. product :: [Int] Int product [] 1 product (n:ns) n * product ns product maps the empty list to 1, and any non-empty list to its head multiplied by the product of its tail. 7

For example: product [2,3,4] 2 * product [3,4] 2 * (3 * product [4]) 2 * (3 * (4 * product [])) 2 * (3 * (4 * 1)) 24 8

Using the same pattern of recursion as in product we can define the length function on lists. length :: [a] Int length [] 0 length (_:xs) 1 + length xs length maps the empty list to 0, and any non-empty list to the successor of the length of its tail. 9

For example: length [1,2,3] 1 + length [2,3] 1 + (1 + length [3]) 1 + (1 + (1 + length [])) 1 + (1 + (1 + 0)) 3 10

Using a similar pattern of recursion we can define the reverse function on lists. reverse :: [a] [a] reverse [] [] reverse (x:xs) reverse xs ++ [x] reverse maps the empty list to the empty list, and any non-empty list to the reverse of its tail appended to its head. 11

For example: reverse [1,2,3] reverse [2,3] ++ [1] (reverse [3] ++ [2]) ++ [1] ((reverse [] ++ [3]) ++ [2]) ++ [1] (([] ++ [3]) ++ [2]) ++ [1] [3,2,1] 12

Multiple Arguments Functions with more than one argument can also be defined using recursion. For example: Zipping the elements of two lists: zip :: [a] [b] [(a,b)] zip [] _ [] zip _ [] [] zip (x:xs) (y:ys) (x,y) : zip xs ys 13

Remove the first n elements from a list: drop :: Int [a] [a] drop 0 xs xs drop _ [] [] drop n (_:xs) drop (n-1) xs Appending two lists: (++) :: [a] [a] [a] [] ++ ys ys (x:xs) ++ ys x : (xs ++ ys) 14

Quicksort The quicksort algorithm for sorting a list of integers can be specified by the following two rules: The empty list is already sorted; Non-empty lists can be sorted by sorting the tail values the head, sorting the tail values the head, and then appending the resulting lists on either side of the head value. 15

Using recursion, this specification can be translated directly into an implementation: Note: qsort :: [Int] [Int] qsort [] [] qsort (x:xs) qsort smaller ++ [x] ++ qsort larger where smaller [a a xs, a x] larger [b b xs, b x] This is probably the simplest implementation of quicksort in any programming language! 16

For example (abbreviating qsort as q): q [3,2,4,1,5] q [2,1] ++ [3] ++ q [4,5] q [1] ++ [2] ++ q [] q [] ++ [4] ++ q [5] [1] [] [] [5] 17

Exercises (1) Without looking at the standard prelude, define the following library functions using recursion: Decide if all logical values in a list are true: and :: [Bool] Bool Concatenate a list of lists: concat :: [[a]] [a] 18

Produce a list with n identical elements: replicate :: Int a [a] Select the nth element of a list: (!!) :: [a] Int a Decide if a value is an element of a list: elem :: Eq a a [a] Bool 19

(2) Define a recursive function merge :: [Int] [Int] [Int] that merges two sorted lists of integers to give a single sorted list. For example: > merge [2,5,6] [1,3,4] [1,2,3,4,5,6] 20

(3) Define a recursive function msort :: [Int] [Int] that implements merge sort, which can be specified by the following two rules: Lists of length 1 are already sorted; Other lists can be sorted by sorting the two halves and merging the resulting lists. 21