Comp 205: Comparative Programming Languages Lazy Evaluation and Infinite Lists Lecture notes, exercises, etc., can be found at: www.csc.liv.ac.uk/~grant/Teaching/COMP205/

Slides:



Advertisements
Similar presentations
Modules Program is built out of components. Each component defines a set of logically related entities (strong internal coupling) A component has a public.
Advertisements

Logic Programming (cont’d): Lists Lists in Prolog are represented by a functor (similar to cons in Scheme): 1.The empty list is represented by the constant.
Comp 205: Comparative Programming Languages Functional Programming Languages: More Haskell Nested definitions Lists Lecture notes, exercises, etc., can.
Getting started with ML ML is a functional programming language. ML is statically typed: The types of literals, values, expressions and functions in a.
Definition of a Prime Number
Types of Number
0 PROGRAMMING IN HASKELL Chapter 12 – Lazy evaluation and infinite lists Slides not from Hutton.
0 PROGRAMMING IN HASKELL Chapter 7 - Higher-Order Functions.
Laziness and Infinite Datastructures Koen Lindström Claessen.
Comp 205: Comparative Programming Languages User-Defined Types Enumerated types Parameterised types Recursive types Lecture notes, exercises, etc., can.
0 PROGRAMMING IN HASKELL Chapter 6 - Recursive Functions Most of this should be review for you.
0 PROGRAMMING IN HASKELL Chapter 6 - Recursive Functions.
Comp 205: Comparative Programming Languages Functional Programming Languages: More Haskell Lecture notes, exercises, etc., can be found at:
Comp 205: Comparative Programming Languages Lazy Evaluation Errors Evaluation Strategies Infinite Lists…. Lecture notes, exercises, etc., can be found.
CSC 160 Computer Programming for Non-Majors Lecture #7: Variables Revisited Prof. Adam M. Wittenstein
Chapter 5 Polymorphic and Higher-Order Functions.
Cs776 (Prasad)L15strm1 Reasoning with Functional Programs Optimization by source to source transformation Well-founded induction Streams : Infinite lists.
Cse536 Functional Programming 1 7/14/2015 Lecture #2, Sept 29, 2004 Reading Assignments –Begin Chapter 2 of the Text Home work #1 can be found on the webpage,
Comp 205: Comparative Programming Languages Functional Programming Languages: More Lists Recursive definitions List comprehensions Lecture notes, exercises,
Section 1-5 Algebra: Variables and Expressions. Vocabulary Algebra: Is a language of symbols, including variables Variable: Is a symbol, usually a letter,
0 PROGRAMMING IN HASKELL Typeclasses and higher order functions Based on lecture notes by Graham Hutton The book “Learn You a Haskell for Great Good” (and.
Functional Programming in Haskell Motivation through Concrete Examples Adapted from Lectures by Simon Thompson.
Programming for Engineers in Python Sawa 2015 Lecture 2: Lists and Loops 1.
0 PROGRAMMING IN HASKELL Chapter 7 - Defining Functions, List Comprehensions.
S. Haridi and P. Van Roy1 Lazy Execution Seif Haridi KTH Peter Van Roy UCL.
Advanced Functional Programming 2009 Ulf Norell (lecture by Jean-Philippe Bernardy)
Sieve of Eratosthenes. The Sieve of Eratosthenes is a method that.
Lee CSCE 314 TAMU 1 CSCE 314 Programming Languages Haskell: Types and Classes Dr. Hyunyoung Lee.
Sequences & Series MATH Precalculus S. Rook.
Lee CSCE 314 TAMU 1 CSCE 314 Programming Languages Haskell: Higher-order Functions Dr. Hyunyoung Lee.
Com Functional Programming Lazy Evaluation Marian Gheorghe Lecture 13 Module homepage Mole & ©University of Sheffieldcom2010.
Lee CSCE 314 TAMU 1 CSCE 314 Programming Languages Haskell: More on Functions and List Comprehensions Dr. Hyunyoung Lee.
Discrete Mathematics Lecture # 22 Recursion.  First of all instead of giving the definition of Recursion we give you an example, you already know the.
1 CMSC 250 Chapter 3, Number Theory. 2 CMSC 250 Introductory number theory l A good proof should have: –a statement of what is to be proven –"Proof:"
Haskell Chapter 4. Recursion  Like other languages  Base case  Recursive call  Author programs a number of built-in functions as examples.
Haskell Chapter 5, Part II. Topics  Review/More Higher Order Functions  Lambda functions  Folds.
List Operations CSCE 314 Spring CSCE 314 – Programming Studio Tuple and List Patterns Pattern matching with wildcards for tuples fst (a, _) = a.
CS321 Functional Programming 2 © JAS Programming with Streams A stream is never finite and could be defined as special polymorphic type data stream.
Chapter 4 With Question/Answer Animations 1. Chapter Summary Divisibility and Modular Arithmetic - Sec 4.1 – Lecture 16 Integer Representations and Algorithms.
Lecture 16: Advanced Topic: Functional Programming CS5363 Compiler and Programming Languages.
Review Simplify Expressions Distributing and Combining Like Terms.
5 Day Forecast Mon Tues Wed Thu Fri.
Lecture 16 Streams continue Infinite Streams מבוא מורחב - שיעור 16.
Functional Programming Lecture 12 - more higher order functions
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
GANTT CHARTS Example Example Example Example text Tasks Example 1
Streams Sections 3.5.1,3.5.2 Pages
MON TUE WED THU
Lecture 18 Infinite Streams and
L1-6 Notes: Algebra: Variables and Expressions
Logo Calendar – January 2012 TO DO LIST 01/04/2012 Example TO DO LIST
January Sun Mon Tue Wed Thu Fri Sat
Types and Classes in Haskell
CSCE 314: Programming Languages Dr. Dylan Shell
2008 Calendar.
PROGRAMMING IN HASKELL
Lazy Programming Lazy evaluation:
Sun Mon Tue Wed Thu Fri Sat
CSE 3302 Programming Languages
CSCE 314: Programming Languages Dr. Dylan Shell
Sun Mon Tue Wed Thu Fri Sat
1/○~1/○ weekly schedule MON TUE WED THU FRI SAT SUN MEMO
2016 | 10 OCT SUN MON TUE WED THU FRI SAT
Sun Mon Tue Wed Thu Fri Sat
Streams Contract is the same as pairs...
1 January 2018 Sun Mon Tue Wed Thu Fri Sat
GC16/3011 Functional Programming Lecture 9 Higher Order Functions
2008 Calendar.
Presentation transcript:

Comp 205: Comparative Programming Languages Lazy Evaluation and Infinite Lists Lecture notes, exercises, etc., can be found at:

Never-Ending Recursion The expression [n..] can be implemented generally by a function: natsfrom :: num -> [num] natsfrom n = n : natsfrom (n+1) Main> natsfrom 0 [0,1,2,3,.... This function can be invoked in the usual way:

Example: Indexing Lists Suppose we want a function that will index the elements in a list, i.e., number the elements: indexList :: [a] -> [(Int,a)] such that, for example, indexList ["Those", "are", "pearls"] = [(1, "Those"), (2, "are"), (3, "pearls")]

One Way... One way of implementing this is with a recursive definition: indexList xs = ind 1 xs where ind n [] = [] ind n (x:xs) = (n,x) : ind (n+1) xs

... Or... Another way of implementing this is to use the built-in function zip : zip :: [a] -> [b] -> [(a,b)] zip xs [] = [] zip [] ys = [] zip (x:xs) (y:ys) = (x,y) : zip xs ys Main> zip [1,2,3] ["Those", "are"] [(1,"Those"),(2,"are")]

... Another Using an infinite list ensures there are enough indices: indexList xs = zip [1..] xs Here, zip is a selector.

Recursive Definitions The Fibonacci sequence can be recursively defined as follows: fibs m n = m : fibs n (m+n) (cf. previous definitions using pairs). Main> fibs 1 1 [1,1,2,3,5,8,13,21,34,55,89,144,….

Defining Constants Constants are functions of no arguments (they vary over nothing…) days = ["Mon", "Tue", "Wed", "Thu", "Fri"] -- NB: days is a constant, -- not a "variable": -- we can't now write days = ["M", "T", "W", "Th", "F"]

Using Constants Main> days ++ ["Sat"] ["Mon","Tue","Wed","Thu","Fri","Sat"] Constants can be used in expressions, but they cannot be redefined: We cannot write (in the script file): days = days ++ ["Sat", "Sun"]

Recursive Constants allOnes = 1 : allOnes Constants can be recursively defined: Main> allOnes [1,1,1,1,1,1,1,1,1,1,1,1,1,... Main> zip allOnes [1,2] [(1,1),(1,2)]

Recursive Constants fibs = 1 : 1 : fplus fibs (tl fibs) where fplus (l:ls) (m:ms) = l + m : fplus ls ms Constants can be recursively defined with local definitions:

Eratosthenes' Sieve A number is prime iff it is divisible only by 1 and itself it is at least 2 The sieve: start with all the numbers from 2 on delete all multiples of the first number from the remainder of the list repeat

Eratosthenes' Sieve I primes = eratosthenate [2..] To generate a list of all the prime numbers:

Eratosthenes' Sieve II sieve n = filter (/=0).(`mod` n) To remove multiples of a number n from a list l:

Eratosthenes' Sieve III next n ps = n : sieve n ps To generate one prime : To generate all primes : eratosthenate = foldr next []

Eratosthenes' Sieve IV primes = sieveAll [2..] where sieveAll (p:ns) = p : sieveAll (sieve p ns) To generate all primes :

Summary Infinite lists and lazy evaluation give a powerful combination for recursive definitions (sometimes hard to follow) Next: Introducing OBJ