ML Sequences.1 Standard ML Sequences. ML Sequences.2 Sequences, Lazy Lists  Characteristics of lazy lists Elements are not evaluated until their values.

Slides:



Advertisements
Similar presentations
Queues Printer queues Several jobs submitted to printer Jobs form a queue Jobs processed in same order as they were received.
Advertisements

More ML Compiling Techniques David Walker. Today More data structures lists More functions More modules.
Programming with Lists
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 [3,4]
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.
Type Variables in ML Until we know the type of a value (perhaps never!), we use a variable for its type Book uses, e.g., t1, tx, tf PL literature uses.
F28PL1 Programming Languages Lecture 14: Standard ML 4.
Writing functions in OCaml. Defining a simple function # let add (x, y) = x + y;; val add : int * int -> int = Notice what this says: –add is a value.
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.
C. Varela; Adapted from S. Haridi and P. Van Roy1 Declarative Programming Techniques Lazy Execution (VRH 4.5) Carlos Varela RPI Adapted with permission.
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 =
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.
0 PROGRAMMING IN HASKELL Chapter 12 – Lazy evaluation and infinite lists Slides not from Hutton.
Lazy lists: Introduction o Lazy lists (or: sequences in ML) are lists whose elements are not explicitly computed. We will use such lists to represent infinite.
CS 312 Spring 2004 Lecture 18 Environment Model. Substitution Model Represents computation as doing substitutions for bound variables at reduction of.
ML: a quasi-functional language with strong typing Conventional syntax: - val x = 5; (*user input *) val x = 5: int (*system response*) - fun len lis =
Queues.
Introduction to ML Last time: Basics: integers, Booleans, tuples,... simple functions introduction to data types This time, we continue writing an evaluator.
CS 312 Spring 2002 Lecture 16 The Environment Model.
מבוא מורחב - שיעור 81 Lecture 8 Lists and list operations (continue).
Cs776 (Prasad)L15strm1 Reasoning with Functional Programs Optimization by source to source transformation Well-founded induction Streams : Infinite lists.
CS 2104 : Prog. Lang. Concepts. Functional Programming I Lecturer : Dr. Abhik Roychoudhury School of Computing From Dr. Khoo Siau Cheng’s lecture notes.
Functional Programming Element of Functional Programming.
The Evolution of Programming Languages Day 2 Lecturer: Xiao Jia The Evolution of PLs1.
Clojure 4 Sequences 20-Oct-15. Clojure errors (NO_SOURCE_FILE:12) Useless--just means you’re running from the REPL shell java.lang.Exception: EOF while.
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)
Practice session #6: 1. Sequence operations 2. Partial evaluation with currying 3. Lazy-lists.
Eager and Lazy Evaluation The evaluation strategies, are differentiated according to the method of passing arguments to functions. Mainly, parameter passing.
Chapter 9: Functional Programming in a Typed Language.
F28PL1 Programming Languages Lecture 13: Standard ML 3.
Error Example - 65/4; ! Toplevel input: ! 65/4; ! ^^ ! Type clash: expression of type ! int ! cannot have type ! real.
PPL Lazy Lists. Midterm 2012 (define sum-vals (λ (ts) (if (ts-simple? ts) (ts-val ts) (accumulate + 0 (map ts-val (ts-inner-slots ts))))))
CS April 2002 Lazy Evaluation, Thunks, and Streams.
CSED101 INTRODUCTION TO COMPUTING FUNCTION ( 함수 ) 유환조 Hwanjo Yu.
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.
Lesson 10.1, page 926 Sequences and Summation Notation Objective: To find terms of sequences given the nth term and find and evaluate a series.
Comparative Programming Languages Language Comparison: Scheme, Smalltalk, Python, Ruby, Perl, Prolog, ML, C++/STL, Java.
CS321 Functional Programming 2 © JAS Programming with Streams A stream is never finite and could be defined as special polymorphic type data stream.
Principles of programming languages 12: Functional programming
ML: a quasi-functional language with strong typing
Lecture 16 Streams continue Infinite Streams מבוא מורחב - שיעור 16.
6.001 SICP Variations on a Scheme
PPL Lazy Lists.
(defmacro (delay x) (list 'lambda () x))
CSE 341 Lecture 5 efficiency issues; tail recursion; print
CS2210:0001Discrete Structures Induction and Recursion
Closures and Streams cs784(Prasad) L11Clos
Streams Sections 3.5.1,3.5.2 Pages
Clojure 4 Sequences 27-Nov-18.
Lecture 18 Infinite Streams and
FP Foundations, Scheme In Text: Chapter 14.
Clojure 4 Sequences 5-Dec-18.
Lecture 18.
Functions, Patterns and Datatypes
Lecture #8 מבוא מורחב.
6.001 SICP Streams – the lazy way
Functions, Patterns and Datatypes
Functions, Patterns and Datatypes
Lazy Programming Lazy evaluation:
CSE-321 Programming Languages Introduction to Functional Programming
List and list operations (continue).
Functions, Patterns and Datatypes
Clojure 3 1-Jun-19.
Streams Contract is the same as pairs...
Functions, Patterns and Datatypes
Programming Languages Dan Grossman 2013
Streams… …row, row, row your boat….
Presentation transcript:

ML Sequences.1 Standard ML Sequences

ML Sequences.2 Sequences, Lazy Lists  Characteristics of lazy lists Elements are not evaluated until their values are required May be infinite  Expressing lazy lists in ML datatype 'a seq = Nil | Cons of 'a * (unit-> 'a seq); fun head(Cons(x,_)) = x; > val head = fn : 'a seq -> 'a fun tail(Cons(_,xf)) = xf(); > val tail = fn : 'a seq -> 'a seq  ML evaluates the E expression in Cons(x, E ), so to obtain lazy evaluation we must write Cons(x,fn()=> E ).

ML Sequences.3 Examples Of Sequences  An increasing sequence of integers fun from k = Cons(k,fn()=>from(k+1)); > val from = fn : int -> int seq from 1; > val it = Cons (1, fn) : int seq tail it; > val it = Cons (2, fn) : int seq  A sequence of squared integers fun squares Nil : int seq = Nil | squares (Cons(x,xf)) = Cons(x*x, fn()=> squares (xf())); > val squares = fn : int seq -> int seq squares (from 1); > val it = Cons (1, fn) : int seq head(tail(tail(tail(tail it)))) > val it = 25 : int

ML Sequences.4 Elementary Sequence Processing  Adding two sequences fun addq (Cons(x,xf), Cons(y,yf))= Cons(x+y, fn()=>addq(xf(),yf())) | addq _ : int seq = Nil; > val addq = fn: int seq * int seq -> int seq  Appending two sequences fun appendq (Nil, yq) = yq | appendq (Cons(x,xf), yq) = Cons(x,fn()=>appendq(xf(),yq)); > val appendq = fn : 'a seq * 'a seq -> 'a seq appendq (xq, yq) : No elements of yq appear in the output unless xq is finite !

ML Sequences.5 Functionals for Sequences  Interleaving two sequences fun interleaving (Nil, yq) = yq | interleaving (Cons(x,xf),yq) = Cons(x, fn()=>interleaving(yq,xf())); > val interleaving = fn: 'a seq * 'a seq -> 'a seq  mapq and filterq fun mapq f Nil = Nil | mapq f (Cons(x,xf)) = Cons( f(x), fn()=>mapq f (xf()) ); > val mapq = fn : ('a ->'b) -> 'a seq -> 'b seq fun filterq pred Nil = Nil | filterq pred (Cons(x,xf)) = if pred x then Cons(x,fn()=>filterq pred (xf())) else filterq pred (xf()); > val filterq = fn : ('a ->bool) -> 'a seq -> 'a seq

ML Sequences.6 Example: Prime numbers  fun notDivide p = filterq ( fn n => n mod p <> 0 ); > val notDivide = fn : int -> int seq -> int seq notDivide p builds a filter that removes all numbers that are divisible by p.  fun sieve (Cons(p,nf)) = Cons(p, fn () => sieve(notDivide p (nf()))); > val sieve = fn : int seq -> int seq Sieve receives a sequence and returns a sequence in which every head places a notDivide filter on the tail; therefore the tail never has numbers that are divisible by any number that has ever been in the head.  val primes = sieve (from 2); > val primes = Cons (2, fn) : int seq