RECURSIVE FUNCTIONS. A recursive function is a function that calls itself. It will normally have two parts: 1. A basis for sufficiently small arguments.

Slides:



Advertisements
Similar presentations
More ML Compiling Techniques David Walker. Today More data structures lists More functions More modules.
Advertisements

Modern Programming Languages, 2nd ed.
ML Lists.1 Standard ML Lists. ML Lists.2 Lists  A list is a finite sequence of elements. [3,5,9] ["a", "list" ] []  Elements may appear more than once.
A First Look at ML.
A Third Look At ML 1. Outline More pattern matching Function values and anonymous functions Higher-order functions and currying Predefined higher-order.
ML Exceptions.1 Standard ML Exceptions. ML Exceptions.2 Exceptions – The Need  An extensive part of the code is error handling  A function can return.
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists Dan Grossman Winter 2013.
F28PL1 Programming Languages Lecture 14: Standard ML 4.
ML Lists.1 Standard ML Lists. ML Lists.2 Lists  A list is a finite sequence of elements. [3,5,9] ["a", "list" ] []  ML lists are immutable.  Elements.
Cs776 (Prasad)L4Poly1 Polymorphic Type System. cs776 (Prasad)L4Poly2 Goals Allow expression of “for all types T” fun I x = x I : ’a -> ’a Allow expression.
ML Datatypes.1 Standard ML Data types. ML Datatypes.2 Concrete Datatypes  The datatype declaration creates new types  These are concrete data types,
Getting started with ML ML is a functional programming language. ML is statically typed: The types of literals, values, expressions and functions in a.
ML: a quasi-functional language with strong typing Conventional syntax: - val x = 5; (*user input *) val x = 5: int (*system response*) - fun len lis =
Chapter ElevenModern Programming Languages1 A Fourth Look At ML.
A Fourth Look At ML Chapter ElevenModern Programming Languages, 2nd ed.1.
A First Look at ML Chapter FiveModern Programming Languages, 2nd ed.1.
Patterns in ML functions. Formal vs. actual parameters Here's a function definition (in C): –int add (int x, int y) { return x + y; } –x and y are the.
5/10/20151 GC16/3011 Functional Programming Lecture 13 Example Programs 1. Evaluating arithmetic expressions 2. lists as functions.
5/11/2015IT 3271 Types in ML (Ch 11) datatype bool = true | false; datatype 'element list = nil | :: of 'element * 'element list n Predefined, but not.
ML: a quasi-functional language with strong typing Conventional syntax: - val x = 5; (*user input *) val x = 5: int (*system response*) - fun len lis =
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
Introduction to ML Last time: Basics: integers, Booleans, tuples,... simple functions introduction to data types This time, we continue writing an evaluator.
Recursion.
Functional Design and Programming Lecture 4: Sorting.
Recursion Chapter 7. Chapter 7: Recursion2 Chapter Objectives To understand how to think recursively To learn how to trace a recursive method To learn.
CHAPTER 10 Recursion. 2 Recursive Thinking Recursion is a programming technique in which a method can call itself to solve a problem A recursive definition.
Recursion.
Recursion Chapter 7. Chapter Objectives  To understand how to think recursively  To learn how to trace a recursive method  To learn how to write recursive.
ML Datatypes.1 Standard ML Data types. ML Datatypes.2 Concrete Datatypes  The datatype declaration creates new types  These are concrete data types,
CSE-321 Programming Languages Introduction to Functional Programming (Part II) POSTECH March 13, 2006 박성우.
Fixed Point Illustrations. Simple Examples f : int -> int f(n) = 5 n = 5 is a unique fixed point. f(n) = n^2 – 2 n = 2 and n = -1 are both fixed points.
Recursion Concepts Implementation Data Structures and Algorithms in Java, Third EditionCh05 – 1.
Introduction to Functional Programming and ML CS 331 Principles of Programming Languages.
CSC 580 – Theory of Programming Languages, Spring, 2009 Week 9: Functional Languages ML and Haskell, Dr. Dale E. Parson.
Chapter 9: Functional Programming in a Typed Language.
Cs7120 (prasad)L7-TDEF1 Type Definitions. cs7120 (prasad)L7-TDEF2 Concrete Types Primitive types ( int, bool, char, string, etc ) Type constructors (
A Second Look At ML 1. Outline Patterns Local variable definitions A sorting example 2.
PZ03EX Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ03EX - ML Programming Language Design and Implementation.
A Third Look At ML Chapter NineModern Programming Languages, 2nd ed.1.
1 Alex Proctor and Brian Lee for CSCI 431 at UNCA, Fall 2002 ML (Meta Language) a brief introduction Alex Proctor and Brian Lee For CSCI 431 at the University.
Chapter SevenModern Programming Languages1 A Second Look At ML.
Advanced Functional Programming Tim Sheard 1 Lecture 17 Advanced Functional Programming Tim Sheard Oregon Graduate Institute of Science & Technology Lecture:
A FIRST LOOK AT ML Chapter Five Modern Programming Languages 1.
Cs776(Prasad)L6sml971 SML-97 Specifics SML/NJ 110.
1.SML Docs Standard Basis 2.First-Class Functions Anonymous Style Points Higher-Order 3.Examples Agenda.
11/2/00SEM107 © Kamin & ReddyClass 6 - Lists - 1 Class 6 - Recursion on Lists r More recursive methods on lists.
Chapter 6 (Lafore’s Book) Recursion Hwajung Lee.  Definition: An algorithmic technique. To solve a problem on an instance of size n, the instance is:
Recursion Function calling itself
Class 2 – Recursion on Lists
OBJECT ORIENTED PROGRAMMING II LECTURE 23 GEORGE KOUTSOGIANNAKIS
ML: a quasi-functional language with strong typing
CSE 341 Lecture 5 efficiency issues; tail recursion; print
ML Again ( Chapter 7) Patterns Local variable definitions
Type Definitions cs776 (prasad) L8tdef.
ML Programming Language Design and Implementation (4th Edition)
Discrete Structures for Computer Science
Control Flow Chapter 6.
Agenda SML Docs First-Class Functions Examples Standard Basis
Functions, Patterns and Datatypes
ML’s Type Inference and Polymorphism
Functions, Patterns and Datatypes
Functions, Patterns and Datatypes
CSE 341 Section 3 Nick Mooney Spring 2017.
ML’s Type Inference and Polymorphism
CSE-321 Programming Languages Introduction to Functional Programming
Functions, Patterns and Datatypes
ML’s Type Inference and Polymorphism
ML’s Type Inference and Polymorphism
Functions, Patterns and Datatypes
Recursion.
Presentation transcript:

RECURSIVE FUNCTIONS

A recursive function is a function that calls itself. It will normally have two parts: 1. A basis for sufficiently small arguments where we do not have to call the function again. 2. An inductive step, where for arguments not handled by the basis, we call the function recursively with smaller arguments. By smaller arguments we mean a smaller number or a smaller list such as the tail of a list. We start with simple recursion. - fun reverse(L) = = if L = nil then nil = else [hd(L)]; val reverse = fn : ''a list -> ''a list - reverse([1,2,3]); val it = [3,2,1] : int list - reverse(["dog","cat","bird"]); val it = ["bird","cat","dog"] : string list

Function Execution Lnil L[3] L[2,3] L[1,2,3] reverse definition of reverse Current View Environment Before call

Nonlinear Recursion In the preceding example the program was called only once in each recursive step. We will choose for our example the combinations of m things taken one at a time which can be calculated by the following formula. n C m = n-1 C m + n-1 C m-1 - fun comb(n,m) = (* assumes 0 <= m <= n *) = if m = 0 orelse m = n then 1 = else comb(n-1,m) + comb(n-1,m-1); val comb = fn : int * int -> int - comb(4,2); val it = 6 : int

Structure of Recursive calls for comb(4,2) comb(4) comb(3,2) comb(3,1) comb(2,2) comb(2,1) comb(2,1) comb(2,0) comb(1,1) comb(1,0) comb(1,1) comb(1,0)

Mutual Recursion Suppose we wish to write two functions that call one another, which we call mutual recursion. Most languages do not provide such a mechanism, but ML does. Suppose we wish to write a function that takes a list L as an argument and produces a list of alternate elements of L. There are two(at least) ways to do this. One, which we call take(L), takes the first, third, fifth, etc. The other, which we call skip(L), skips the first and then takes alternate elements of L. - fun take(L) = = if L = nil then nil = else hd(L) ::skip(tl(L)); stdIn: Error: unbound variable or constructor: skip

Correct definition of a mutually recursive function: - fun = take(L) = = if L = nil then nil = else hd(L) :: skip(tl(L)) = GC # : (0 ms) and = skip(L) = = if L = nil then nil = else take(tl(L)); val take = fn : ''a list -> ''a list val skip = fn : ''a list -> ''a list - take([1,2,3,4,5,6]); GC # : (0 ms) val it = [1,3,5] : int list - skip(["a","b","c","d"]); val it = ["b","d"] : string list