03-25-planned.ml Tue Mar 25 06:32: (* Lecture 14: Infinite data structures *) type a stream = Cons of a * a stream
|
|
- Julianna Phillips
- 5 years ago
- Views:
Transcription
1 03-25-planned.ml Tue Mar 25 06:32: (* Lecture 14: Infinite data structures *) type a stream = Cons of a * a stream let head (Cons(x, _) : a stream) : a = x let tail (Cons(_, r) : a stream) : a stream = r let rec ones = Cons(1, ones) assert( phys_equal ones (tail ones) ) assert( phys_equal ones (tail (tail ones)) ) Cons(n, count_from (n + 1)) (* This is gonna blow the stack: *) let rec nats = count_from 0 let rec map (Cons(x, r) : a stream) (f: a -> b) : b stream = Cons(f x, map r f) (* Not allowed by OCaml: *) (* let rec nats = Cons(1, map f:((+) 1) nats) *) (* Diverges: *) let twos = map f:((+) 1) ones (** SECOND ATTEMPT: delay computing the tail **) (* Doesn t type check: *) (* let rec map (Cons(x, r)) f = Cons(f x, fun () -> map r f) *) type a stream = unit -> a str and a str = Cons of a * a stream type a stream = unit -> a str let rec ones : int stream = fun () -> { hd = 1; tl = ones } let head (s : a stream) : a = (s ()).hd let tail (s : a stream) : a stream = (s ()).tl assert( phys_equal ones (tail ones) ) assert( phys_equal (tail ones) (tail ones) ) fun () -> { hd = f (head s); tl = map (tail s) f } let twos = map ones f:((+) 1)
2 03-25-planned.ml Tue Mar 25 06:32: assert( head twos = 2 ) assert( head (tail twos) = 2 ) assert( head (tail (tail twos)) = 2 ) assert( not (phys_equal twos (tail twos)) ) assert( not (phys_equal (tail twos) (tail twos)) ) if n > 0 then head s :: take (tail s) (n - 1) assert( take twos 5 = [2; 2; 2; 2; 2] ) fun () -> { hd = n; tl = count_from (n + 1) } let nats = count_from 0 (* Or: *) let rec nats = fun () -> { hd = 0; tl = map f:((+) 1) nats } (* Map over two streams item-wise: *) let rec map2 (s1 : a stream) (s2 : b stream) (f : a -> b -> c) : c stream = fun () -> { hd = f (head s1) (head s2); tl = map2 (tail s1) (tail s2) f } assert( take (map2 f:(+) ones twos) 5 = [3; 3; 3; 3; 3] ) let rec fibs = fun () -> { hd = 0; tl = fun () -> { hd = 1; tl = map2 f:(+) fibs (tail fibs) } } assert( take fibs 10 = [0; 1; 1; 2; 3; 5; 8; 13; 21; 34] ) let _ = take fibs 17 (* Exponential time? *) (** THIRD ATTEMPT: memoize **) type a promise = Unevaluated of (unit -> a) Evaluated of a type a stream = a str promise ref let rec ones = ref (Unevaluated (fun () -> { hd = 1; tl = ones })) let rec head (s : a stream) : a = match!s with Evaluated str -> str.hd let str = f () in s := Evaluated str; str.hd let rec tail (s : a stream) : a stream = match!s with Evaluated str -> str.tl let str = f () in
3 03-25-planned.ml Tue Mar 25 06:32: s := Evaluated str; str.tl type a my_lazy_t = a promise ref let force (pr : a my_lazy_t) : a = match!pr with Evaluated v -> v let v = f () in pr := Evaluated v; v type a stream = a str my_lazy_t let rec ones = ref (Unevaluated (fun () -> { hd = 1; tl = ones })) let rec head (s : a stream) : a = (force s).hd let rec tail (s : a stream) : a stream = (force s).tl if n > 0 then head s :: take (tail s) (n - 1) assert( take ones 5 = [1; 1; 1; 1; 1] ) ref (Unevaluated (fun () -> { hd = f (head s); tl = map (tail s) f })) let twos = map f:((+) 1) ones assert( take twos 5 = [2; 2; 2; 2; 2] ) (** FOURTH (FINAL) ATTEMPT **) type a stream = a str lazy_t let head (s : a stream) : a = (Lazy.force s).hd let tail (s : a stream) : a stream = (Lazy.force s).tl lazy { hd = f (head s); tl = map (tail s) f } if n > 0 then head s :: take (tail s) (n - 1) let rec ones = lazy { hd = 1; tl = ones } assert( take ones 5 = [1; 1; 1; 1; 1] ) lazy { hd = n;
4 03-25-planned.ml Tue Mar 25 06:32: tl = count_from (n + 1) } let nats = count_from 0 let rec nats = lazy { hd = 0; tl = map f:((+) 1) nats } let rec map2 (s1 : a stream) (s2 : b stream) (f : a -> b -> c) : c stream = lazy { hd = f (head s1) (head s2); tl = map2 (tail s1) (tail s2) f } let rec fibs = lazy { hd = 0; tl = lazy { hd = 1; tl = map2 f:(+) fibs (tail fibs) } } assert( take fibs 10 = [0; 1; 1; 2; 3; 5; 8; 13; 21; 34] ) let _ = take fibs 40 (* This version is insufficiently lazy: *) let rec filter (s : a stream) (f : a -> bool) : a stream = if f (head s) then lazy { hd = head s; tl = filter (tail s) f } else filter (tail s) f let rec filter (s : a stream) (f : a -> bool) : a stream = let rec loop r = if f (head r) then { hd = head r; tl = lazy (loop (tail r)) } else loop (tail r) in lazy (loop s) let even n = n mod 2 = 0 let odd n = not (even n) let evens = filter nats f:even assert( take evens 5 = [0; 2; 4; 6; 8] ) let odds = filter nats f:odd assert( take odds 5 = [1; 3; 5; 7; 9] ) (* Sieve of Eratosthenes *) let not_div_by m n = n mod m <> 0 let rec sieve (s : int stream) : int stream = lazy { hd = head s; tl = sieve (filter (tail s) f:(not_div_by (head s))) } let primes = sieve (tail (tail nats)) assert( take primes 10 = [2; 3; 5; 7; 11; 13; 17; 19; 23; 29] ) (* Approximating Taylor series *) (* e = 1/0! + 1/1! + 1/2! + 1/3! +... *)
5 03-25-planned.ml Tue Mar 25 06:32: let rec fact_f n = if n <= 0 then 1.0 else Float.of_int n *. fact_f (n - 1) let e_series = map2 f:(/.) (map f:float.of_int ones) (map f:fact_f nats) let sum_up_to_n : float stream -> int -> float = let rec loop acc s n = if n <= 0 then acc else loop (acc +. head s) (tail s) (n - 1) in loop 0.0 let _ = sum_up_to_n e_series 1 let _ = sum_up_to_n e_series 5 let _ = sum_up_to_n e_series 10 let _ = sum_up_to_n e_series 100 let _ = sum_up_to_n e_series 1000 (* pi = 4/1-4/3 + 4/5-4/7 + 4/ *) let rec alt_fours = lazy { hd = 4.; tl = lazy { hd = -4.; tl = alt_fours } } let pi_series = map2 f:(/.) alt_fours (map f:float.of_int odds) let _ = sum_up_to_n pi_series 1 let _ = sum_up_to_n pi_series 5 let _ = sum_up_to_n pi_series 10 let _ = sum_up_to_n pi_series 100 let _ = sum_up_to_n pi_series 1000 let _ = sum_up_to_n pi_series let _ = sum_up_to_n pi_series (* Automatic numeric integration *) let trap_area (f : float -> float) (a : float) (b : float) : float = (f a +. f b) *. (b -. a) /. 2. let mid (a : float) (b : float) : float = (a +. b) /. 2. let rec integrate (f : float -> float) (a : float) (b : float) : float stream = lazy { hd = trap_area f a b; tl = map2 f:(+.) (integrate f a (mid a b)) (integrate f (mid a b) b) } let rec within eps s = if Float.abs (head s -. head (tail s)) < eps then head s else within eps (tail s) let integral (f : float -> float) (a : float) (b : float) (eps : float) : float = within eps (integrate f a b) let _ = integral (fun x -> x *. x) let _ = integral (fun x -> x *. x) let _ = integral (fun x -> x *. x) let _ = integral (fun x -> x *. x)
COS 326 David Walker Princeton University
Functional Abstractions over Imperative Infrastructure and Lazy Evaluation COS 326 David Walker Princeton University slides copyright 2013-2015 David Walker and Andrew W. Appel permission granted to reuse
More informationSometimes it s good to be lazy
Chapter 8 Sometimes it s good to be lazy Typically, languages such as OCaml (SML, Lisp or Scheme) evaluate expressions by a call-by-value discipline. All variables in OCaml are bound by value, which means
More informationFall 2018 Lecture N
15-150 Fall 2018 Lecture N Tuesday, 20 November I m too lazy to figure out the correct number Stephen Brookes midterm2 Mean: 79.5 Std Dev: 18.4 Median: 84 today or, we could procrastinate lazy functional
More informationNormal Order (Lazy) Evaluation SICP. Applicative Order vs. Normal (Lazy) Order. Applicative vs. Normal? How can we implement lazy evaluation?
Normal Order (Lazy) Evaluation Alternative models for computation: Normal (Lazy) Order Evaluation Memoization Streams Applicative Order: evaluate all arguments, then apply operator Normal Order: pass unevaluated
More information6.037 Lecture 7B. Scheme Variants Normal Order Lazy Evaluation Streams
6.037 Lecture 7B Scheme Variants Normal Order Lazy Evaluation Streams Edited by Mike Phillips & Ben Vandiver Original Material by Eric Grimson & Duane Boning Further Variations on a Scheme Beyond Scheme
More informationITERATORS AND STREAMS 9
ITERATORS AND STREAMS 9 COMPUTER SCIENCE 61A November 12, 2015 1 Iterators An iterator is an object that tracks the position in a sequence of values. It can return an element at a time, and it is only
More informationCS Lectures 2-3. Introduction to OCaml. Polyvios Pratikakis
CS 490.40 Lectures 2-3 Introduction to OCaml Polyvios Pratikakis Based on slides by Jeff Foster History ML: Meta Language 1973, University of Edinburg Used to program search tactics in LCF theorem prover
More informationCMSC 330: Organization of Programming Languages. OCaml Expressions and Functions
CMSC 330: Organization of Programming Languages OCaml Expressions and Functions CMSC330 Spring 2018 1 Lecture Presentation Style Our focus: semantics and idioms for OCaml Semantics is what the language
More informationCS 312. Lecture April Lazy Evaluation, Thunks, and Streams. Evaluation
CS 312 Lecture 27 29 April 2008 Lazy Evaluation, Thunks, and Streams Evaluation SML as you know it (substitution semantics) if true then e 1 else e 2 e 1 if false then e 1 else e 2 e 2 if eagerly evaluates
More informationML Type Inference and Unification. Arlen Cox
ML Type Inference and Unification Arlen Cox Research Goals Easy to use, high performance parallel programming Primary contributions in backend and runtime Need a front end to target backend ML offers ease
More informationStreams and Evalutation Strategies
Data and Program Structure Streams and Evalutation Strategies Lecture V Ahmed Rezine Linköpings Universitet TDDA69, VT 2014 Lecture 2: Class descriptions - message passing ( define ( make-account balance
More informationIntroduction to ML. Mooly Sagiv. Cornell CS 3110 Data Structures and Functional Programming
Introduction to ML Mooly Sagiv Cornell CS 3110 Data Structures and Functional Programming The ML Programming Language General purpose programming language designed by Robin Milner in 1970 Meta Language
More informationCITS3211 FUNCTIONAL PROGRAMMING. 7. Lazy evaluation and infinite lists
CITS3211 FUNCTIONAL PROGRAMMING 7. Lazy evaluation and infinite lists Summary: This lecture introduces lazy evaluation and infinite lists in functional languages. cs123 notes: Lecture 19 R.L. While, 1997
More informationForward recursion. CS 321 Programming Languages. Functions calls and the stack. Functions calls and the stack
Forward recursion CS 321 Programming Languages Intro to OCaml Recursion (tail vs forward) Baris Aktemur Özyeğin University Last update made on Thursday 12 th October, 2017 at 11:25. Much of the contents
More informationProgramming Languages
CSE 130: Fall 2009 Programming Languages Lecture 2: A Crash Course in ML News On webpage: Suggested HW #1 PA #1 (due next Fri 10/9) Technical issues installing Ocaml - should be resolved soon! Ranjit Jhala
More informationLecture 5: Lazy Evaluation and Infinite Data Structures
Lecture 5: Lazy Evaluation and Infinite Data Structures Søren Haagerup Department of Mathematics and Computer Science University of Southern Denmark, Odense October 3, 2017 How does Haskell evaluate a
More informationIntroduction to Functional Programming: Lecture 7 2 Functions as innite data structures Ordinary ML data structures are always nite. We can, however,
Introduction to Functional Programming: Lecture 7 1 Introduction to Functional Programming John Harrison University of Cambridge Lecture 7 Innite data structures Topics covered: Functions as innite data
More informationCOSE212: Programming Languages. Lecture 4 Recursive and Higher-Order Programming
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
More informationCall-by-name evaluation
Lecture IX Keywords: call-by-value, call-by-name, and call-by-need evaluation; lazy datatypes: sequences, streams, trees; lazy evaluation; sieve of Eratosthenes; breadth-first and depth-first traversals.
More informationIntroduction to ML. Mooly Sagiv. Cornell CS 3110 Data Structures and Functional Programming
Introduction to ML Mooly Sagiv Cornell CS 3110 Data Structures and Functional Programming The ML Programming Language General purpose programming language designed by Robin Milner in 1970 Meta Language
More informationOCaml. ML Flow. Complex types: Lists. Complex types: Lists. The PL for the discerning hacker. All elements must have same type.
OCaml The PL for the discerning hacker. ML Flow Expressions (Syntax) Compile-time Static 1. Enter expression 2. ML infers a type Exec-time Dynamic Types 3. ML crunches expression down to a value 4. Value
More informationFunctional Programming. Overview. Topics. Definition n-th Fibonacci Number. Graph
Topics Functional Programming Christian Sternagel Harald Zankl Evgeny Zuenko Department of Computer Science University of Innsbruck WS 2017/2018 abstract data types, algebraic data types, binary search
More informationStreams, Delayed Evaluation and a Normal Order Interpreter. CS 550 Programming Languages Jeremy Johnson
Streams, Delayed Evaluation and a Normal Order Interpreter CS 550 Programming Languages Jeremy Johnson 1 Theme This lecture discusses the stream model of computation and an efficient method of implementation
More informationCSCI 2041: Lazy Evaluation
CSCI 2041: Lazy Evaluation Chris Kauffman Last Updated: Wed Dec 5 12:32:32 CST 2018 1 Logistics Reading Module Lazy on lazy evaluation Module Stream on streams Lambdas/Closures Briefly discuss these as
More informationDesign Principles of Programming Languages. Recursive Types. Zhenjiang Hu, Haiyan Zhao, Yingfei Xiong Peking University, Spring Term
Design Principles of Programming Languages Recursive Types Zhenjiang Hu, Haiyan Zhao, Yingfei Xiong Peking University, Spring Term Review: what have we learned so far? λ-calculus: function and data can
More informationStreams. CS21b: Structure and Interpretation of Computer Programs Spring Term, 2004
Streams CS21b: Structure and Interpretation of Computer Programs Spring Term, 2004 We ve already seen how evaluation order can change behavior when we program with state. Now we want to investigate how
More informationCOSE212: Programming Languages. Lecture 3 Functional Programming in OCaml
COSE212: Programming Languages Lecture 3 Functional Programming in OCaml Hakjoo Oh 2017 Fall Hakjoo Oh COSE212 2017 Fall, Lecture 3 September 18, 2017 1 / 44 Why learn ML? Learning ML is a good way of
More informationProgramming Languages
CSE 130 : Fall 2008 Programming Languages Lecture 2: A Crash Course in ML Ranjit Jhala UC San Diego News On webpage: Suggested HW #1, sample for Quiz #1 on Thu PA #1 (due next Fri 10/10) No make-up quizzes
More informationDialects of ML. CMSC 330: Organization of Programming Languages. Dialects of ML (cont.) Features of ML. Functional Languages. Features of ML (cont.
CMSC 330: Organization of Programming Languages OCaml 1 Functional Programming Dialects of ML ML (Meta Language) Univ. of Edinburgh,1973 Part of a theorem proving system LCF The Logic of Computable Functions
More informationIntroduction to ML. Mooly Sagiv. Cornell CS 3110 Data Structures and Functional Programming
Introduction to ML Mooly Sagiv Cornell CS 3110 Data Structures and Functional Programming Typed Lambda Calculus Chapter 9 Benjamin Pierce Types and Programming Languages Call-by-value Operational Semantics
More informationProgramming Languages
CSE 130: Spring 2010 Programming Languages Lecture 2: A Crash Course in ML Ranjit Jhala UC San Diego News On webpage: Suggested HW #1 PA #1 (due next Wed 4/9) Please post questions to WebCT Today: A crash
More informationIntroduction to OCaml
Introduction to OCaml Jed Liu Department of Computer Science Cornell University CS 6110 Lecture 26 January 2009 Based on CS 3110 course notes and an SML tutorial by Mike George Jed Liu Introduction to
More informationCMSC 330: Organization of Programming Languages. Functional Programming with Lists
CMSC 330: Organization of Programming Languages Functional Programming with Lists CMSC330 Spring 2018 1 Lists in OCaml The basic data structure in OCaml Lists can be of arbitrary length Implemented as
More informationNews. Programming Languages. Complex types: Lists. Recap: ML s Holy Trinity. CSE 130: Spring 2012
News CSE 130: Spring 2012 Programming Languages On webpage: Suggested HW #1 PA #1 (due next Fri 4/13) Lecture 2: A Crash Course in ML Please post questions to Piazza Ranjit Jhala UC San Diego Today: A
More informationOCaml. History, Variants. ML s holy trinity. Interacting with ML. Base type: Integers. Base type: Strings. *Notes from Sorin Lerner at UCSD*
OCaml 1. Introduction Rapid introduction to what s in OCaml 2. Focus on Features Individually as Needed as Semester Progresses *Notes from Sorin Lerner at UCSD* History, Variants Meta Language Designed
More informationCS 11 Ocaml track: lecture 2
Today: CS 11 Ocaml track: lecture 2 comments algebraic data types more pattern matching records polymorphic types ocaml libraries exception handling Previously... ocaml interactive interpreter compiling
More informationOption Values, Arrays, Sequences, and Lazy Evaluation
Option Values, Arrays, Sequences, and Lazy Evaluation Björn Lisper School of Innovation, Design, and Engineering Mälardalen University bjorn.lisper@mdh.se http://www.idt.mdh.se/ blr/ Option Values, Arrays,
More informationPlan (next 4 weeks) 1. Fast forward. 2. Rewind. 3. Slow motion. Rapid introduction to what s in OCaml. Go over the pieces individually
Plan (next 4 weeks) 1. Fast forward Rapid introduction to what s in OCaml 2. Rewind 3. Slow motion Go over the pieces individually History, Variants Meta Language Designed by Robin Milner @ Edinburgh Language
More information6.001 Notes: Section 17.5
6.001 Notes: Section 17.5 Slide 17.5.1 Now, let's look at one example in which changing the evaluation model allows us to explore a very different kind of computational problem. Our goal is to show how
More information02157 Functional Programming. Michael R. Ha. Sequences and Sequence Expressions. Michael R. Hansen
Sequences and Sequence Expressions nsen 1 DTU Compute, Technical University of Denmark Sequences and Sequence Expressions MRH 14/11/2018 Sequence: a possibly infinite, ordered collection of elements, where
More informationProblem Set CVO 103, Spring 2018
Problem Set CVO 103, Spring 2018 Hakjoo Oh Due: 06/12 (in class) Problem 1 The Fibonacci numbers can be defined as follows: 0 if n = 0 fib(n) = 1 if n = 1 fib(n 1) + fib(n 2) otherwise Write in OCaml the
More informationOzyegin University CS 321 Programming Languages Sample Problems 05
Page 1 of 7 Ozyegin University CS 321 Programming Languages Sample Problems 05 1. (PLC Ex. 7.2.(i)) Write a C program containing a function void arrsum(int n, int arr[], int *sump) that computes and returns
More informationCS61A Notes Disc 11: Streams Streaming Along
CS61A Notes Disc 11: Streams Streaming Along syntax in lecture and in the book, so I will not dwell on that. Suffice it to say, streams is one of the most mysterious topics in CS61A, trust than whatever
More informationHard deadline: 3/28/15 1:00pm. Using software development tools like source control. Understanding the environment model and type inference.
CS 3110 Spring 2015 Problem Set 3 Version 0 (last modified March 12, 2015) Soft deadline: 3/26/15 11:59pm Hard deadline: 3/28/15 1:00pm Overview In this assignment you will implement several functions
More informationFundamentals of Programming (Python) Getting Started with Programming
Fundamentals of Programming (Python) Getting Started with Programming Ali Taheri Sharif University of Technology Some slides have been adapted from Python Programming: An Introduction to Computer Science
More informationWhich of the following expressions has the type int list?
Which of the following expressions has the type int list? 1) [3; true] 2) [1;2;3]::[1;2] 3) []::[1;2]::[] 4) (1::2)::(3::4)::[] 5) [1;2;3;4] Answer: 5 Which of the following expressions has the type (int
More informationThe University of Nottingham
The University of Nottingham SCHOOL OF COMPUTER SCIENCE A LEVEL 2 MODULE, SPRING SEMESTER 2013-2014 G54FOP FOUNDATIONS OF PROGRAMMING Time allowed 2 hours Candidates may complete the front cover of their
More informationDiscussion 11. Streams
Discussion 11 Streams A stream is an element and a promise to evaluate the rest of the stream. You ve already seen multiple examples of this and its syntax in lecture and in the books, so I will not dwell
More informationBackground. CMSC 330: Organization of Programming Languages. Useful Information on OCaml language. Dialects of ML. ML (Meta Language) Standard ML
CMSC 330: Organization of Programming Languages Functional Programming with OCaml 1 Background ML (Meta Language) Univ. of Edinburgh, 1973 Part of a theorem proving system LCF The Logic of Computable Functions
More informationObject-Oriented Design Lecture 13 CSU 370 Fall 2008 (Pucella) Friday, Oct 31, 2008
Object-Oriented Design Lecture 13 CSU 370 Fall 2008 (Pucella) Friday, Oct 31, 2008 Laziness For this lecture, I want to return to something that came up during the last homework, the third homework where
More informationCMSC 330: Organization of Programming Languages. Functional Programming with Lists
CMSC 330: Organization of Programming Languages Functional Programming with Lists 1 Lists in OCaml The basic data structure in OCaml Lists can be of arbitrary length Implemented as a linked data structure
More informationHaskell: From Basic to Advanced. Part 2 Type Classes, Laziness, IO, Modules
Haskell: From Basic to Advanced Part 2 Type Classes, Laziness, IO, Modules Qualified types In the types schemes we have seen, the type variables were universally quantified, e.g. ++ :: [a] -> [a] -> [a]
More informationDesign Principles of Programming Languages. Recursive Types. Zhenjiang Hu, Haiyan Zhao, Yingfei Xiong Peking University, Spring Term, 2014
Design Principles of Programming Languages Recursive Types Zhenjiang Hu, Haiyan Zhao, Yingfei Xiong Peking University, Spring Term, 2014 Review: Why learn type theories? Art vs. Knowledge Art cannot be
More informationSNU Programming Language Theory
SNU 4541.574 Programming Language Theory Polymorphism Polymorphism We encountered the concept of polymorphism very briefly last time. Let s look at it now in a bit more detail. # let rec last l = match
More informationFunctional Programming in Haskell Part 2 : Abstract dataypes and infinite structures
Functional Programming in Haskell Part 2 : Abstract dataypes and infinite structures Madhavan Mukund Chennai Mathematical Institute 92 G N Chetty Rd, Chennai 600 017, India madhavan@cmi.ac.in http://www.cmi.ac.in/
More informationCS Lecture 6: Map and Fold. Prof. Clarkson Spring Today s music: Selections from the soundtrack to 2001: A Space Odyssey
CS 3110 Lecture 6: Map and Fold Prof. Clarkson Spring 2015 Today s music: Selections from the soundtrack to 2001: A Space Odyssey Review Course so far: Syntax and semantics of (most of) OCaml Today: No
More informationCIS 120 Midterm I October 2, Name (printed): Username (login id):
CIS 120 Midterm I October 2, 2015 Name (printed): Username (login id): My signature below certifies that I have complied with the University of Pennsylvania s Code of Academic Integrity in completing this
More informationChapter 15. Functional Programming Languages
Chapter 15 Functional Programming Languages Copyright 2009 Addison-Wesley. All rights reserved. 1-2 Chapter 15 Topics Introduction Mathematical Functions Fundamentals of Functional Programming Languages
More informationMacros & Streams Spring 2018 Discussion 9: April 11, Macros
CS 61A Macros & Streams Spring 2018 Discussion 9: April 11, 2018 1 Macros So far, we ve mostly explored similarities between the Python and Scheme languages. For example, the Scheme list data structure
More informationhttps://lambda.mines.edu 1 Recursion is a beautiful way to express many algorithms, as it typically makes our algorithm easier to prove. 2 Calling a function requires space on the call stack for the variables,
More informationCS 320: Concepts of Programming Languages
CS 320: Concepts of Programming Languages Wayne Snyder Computer Science Department Boston University Lecture 04: Basic Haskell Continued o Polymorphic Types o Type Inference with Polymorphism o Standard
More informationLazy Evaluation in Scala
Lazy Evaluation Lazy Evaluation The proposed implementation suffers from a serious potential performance problem: If tail is called several times, the corresponding stream will be recomputed each time.
More informationHomework 12: Tail Recursion and Analysis Due: 11:59 PM, Dec 4, 2018
Integrated Introduction to Computer Science Klein Homework 12: Tail Recursion and Analysis Due: 11:59 PM, Dec 4, 2018 Contents 1 Tail Recursion 2 1.1 Harmonic Numbers.....................................
More informationParametric types. map: ( a (a b) a list b list. filter: ( a bool) a list a list. Why? a and b are type variables!
What is the deal with a? Parametric types aka: what s up with those a? These meta-functions have strange types: map: ( a b) a list b list filter: ( a bool) a list a list Why? Polymorphism Poly = many,
More informationFunctional Programming for Imperative Programmers
Functional Programming for Imperative Programmers R. Sekar This document introduces functional programming for those that are used to imperative languages, but are trying to come to terms with recursion
More informationAny questions. Say hello to OCaml. Say hello to OCaml. Why readability matters. History, Variants. Plan (next 4 weeks)
Any questions Say hello to OCaml? void sort(int arr[], int beg, int end){ if (end > beg + 1){ int piv = arr[beg]; int l = beg + 1; int r = end; while (l!= r-1){ if(arr[l]
More informationCPS 506 Comparative Programming Languages. Programming Language Paradigm
CPS 506 Comparative Programming Languages Functional Programming Language Paradigm Topics Introduction Mathematical Functions Fundamentals of Functional Programming Languages The First Functional Programming
More informationPutting Laziness to Work
Putting Laziness to Work Why use laziness Laziness has lots of interesting uses Build cyclic structures. Finite representations of infinite data. Do less work, compute only those values demanded by the
More informationn n Try tutorial on front page to get started! n spring13/ n Stack Overflow!
Announcements n Rainbow grades: HW1-6, Quiz1-5, Exam1 n Still grading: HW7, Quiz6, Exam2 Intro to Haskell n HW8 due today n HW9, Haskell, out tonight, due Nov. 16 th n Individual assignment n Start early!
More informationProgramming Languages. Thunks, Laziness, Streams, Memoization. Adapted from Dan Grossman s PL class, U. of Washington
Programming Languages Thunks, Laziness, Streams, Memoization Adapted from Dan Grossman s PL class, U. of Washington You ve been lied to Everything that looks like a function call in Racket is not necessarily
More informationSome Advanced ML Features
Some Advanced ML Features Mooly Sagiv Michael Clarkson, Cornell CS 3110 Data Structures and Functional Programming University of Washington: Dan Grossman ML is small Small number of powerful constructs
More informationMidterm 1. CMSC 430 Introduction to Compilers Spring Instructions Total 100. Name: March 14, 2012
Name: Midterm 1 CMSC 430 Introduction to Compilers Spring 2012 March 14, 2012 Instructions This exam contains 8 pages, including this one. Make sure you have all the pages. Write your name on the top of
More informationFunction definitions. CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists. Example, extended. Some gotchas. Zach Tatlock Winter 2018
Function definitions CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists Zach Tatlock Winter 2018 Functions: the most important building block in the whole course Like Java methods, have arguments
More informationCSE341: Programming Languages Lecture 2 Functions, Pairs, Lists. Zach Tatlock Winter 2018
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists Zach Tatlock Winter 2018 Function definitions Functions: the most important building block in the whole course Like Java methods, have arguments
More informationCS61A Summer 2010 George Wang, Jonathan Kotker, Seshadri Mahalingam, Eric Tzeng, Steven Tang
CS61A Notes Week 6B: Streams Streaming Along A stream is an element and a promise to evaluate the rest of the stream. You ve already seen multiple examples of this and its syntax in lecture and in the
More informationFunctional Programming. Pure Functional Programming
Functional Programming With examples in F# Pure Functional Programming Functional programming involves evaluating expressions rather than executing commands. Computation is largely performed by applying
More informationProgramming Languages
CSE 130: Spring 2010 Programming Languages Lecture 3: Epressions and Types Ranjit Jhala UC San Diego A Problem fun -> +1 Can functions only have a single parameter? A Solution: Simultaneous Binding Parameter
More informationBegin at the beginning
Begin at the beginning Expressions (Syntax) Exec-time Dynamic Values (Semantics) Compile-time Static Types 1. Programmer enters expression 2. ML checks if expression is well-typed Using a precise set of
More informationSCHEME 10 COMPUTER SCIENCE 61A. July 26, Warm Up: Conditional Expressions. 1. What does Scheme print? scm> (if (or #t (/ 1 0)) 1 (/ 1 0))
SCHEME 0 COMPUTER SCIENCE 6A July 26, 206 0. Warm Up: Conditional Expressions. What does Scheme print? scm> (if (or #t (/ 0 (/ 0 scm> (if (> 4 3 (+ 2 3 4 (+ 3 4 (* 3 2 scm> ((if (< 4 3 + - 4 00 scm> (if
More informationWellesley College CS251 Programming Languages Spring, 2000 FINAL EXAM REVIEW PROBLEM SOLUTIONS
Wellesley College CS251 Programming Languages Spring, 2000 FINAL EXAM REVIEW PROBLEM SOLUTIONS This document contains solutions to the problems on the final exam review problems posted earlier except for
More informationCSc 372. Comparative Programming Languages. 13 : Haskell Lazy Evaluation. Department of Computer Science University of Arizona
1/16 CSc 372 Comparative Programming Languages 13 : Haskell Lazy Evaluation Department of Computer Science University of Arizona collberg@gmail.com Copyright c 2013 Christian Collberg 2/16 Lazy evaluation
More information1 Lazy List Implementation
February 3, 2004 lazy.nw 1 1 Lazy List Implementation 1.1 A Simple Implementation lazylist.r Cons
More informationExtra Lecture #7: Defining Syntax
Extra Lecture #7: Defining Syntax In effect, function and class definitions extend the Python language by adding new commands and data types. However, these are highly constrained extensions. For example,
More informationFunctional Programming. Introduction To Cool
Functional Programming Introduction To Cool #1 Cunning Plan ML Functional Programming Fold Sorting Cool Overview Syntax Objects Methods Types #2 This is my final day... as your... companion... through
More informationTopic 8: Lazy Evaluation
Topic 8: Lazy Evaluation 1 Recommended Exercises and Readings From Haskell: The craft of functional programming (3 rd Ed.) Exercises: 17.1, 17.2, 17.4, 17.8, 17.23, 17.25, 17.28, 17.29 Readings: Chapter
More informationCMSC330 Fall 2016 Midterm #1 2:00pm/3:30pm
CMSC330 Fall 2016 Midterm #1 2:00pm/3:30pm Name: Discussion Time: 10am 11am 12pm 1pm 2pm 3pm TA Name (Circle): Alex Austin Ayman Brian Damien Daniel K. Daniel P. Greg Tammy Tim Vitung Will K. Instructions
More informationProblem solving paradigms
Problem solving paradigms Bjarki Ágúst Guðmundsson Tómas Ken Magnússon Árangursrík forritun og lausn verkefna School of Computer Science Reykjavík University Today we re going to cover Problem solving
More informationStreams and Lazy Evaluation in Lisp
Streams and Lazy Evaluation in Lisp Overview Different models of expression evaluation Lazy vs. eager evaluation Normal vs. applicative order evaluation Computing with streams in Lisp Motivation Unix Pipes
More informationCS450 - Structure of Higher Level Languages
Spring 2018 Streams February 24, 2018 Introduction Streams are abstract sequences. They are potentially infinite we will see that their most interesting and powerful uses come in handling infinite sequences.
More informationExample Scheme Function: equal
ICOM 4036 Programming Languages Functional Programming Languages Mathematical Functions Fundamentals of Functional Programming Languages The First Functional Programming Language: LISP Introduction to
More informationFUNCTIONAL PROGRAMMING
FUNCTIONAL PROGRAMMING Map, Fold, and MapReduce Prof. Clarkson Summer 2015 Today s music: Selections from the soundtrack to 2001: A Space Odyssey Review Yesterday: Lists: OCaml's awesome built-in datatype
More informationMPRI course 2-4 Functional programming languages Exercises
MPRI course 2-4 Functional programming languages Exercises Xavier Leroy October 13, 2016 Part I: Interpreters and operational semantics Exercise I.1 (**) Prove theorem 2 (the unique decomposition theorem).
More informationLazy Modules. Keiko Nakata Institute of Cybernetics at Tallinn University of Technology
Lazy Modules Keiko Nakata Institute of Cybernetics at Tallinn University of Technology Constrained lazy initialization for modules Plan of my talk Lazy initialization in practice Constrained laziness Or,
More informationFunctional Programming
Functional Programming Overview! Functional vs. imperative programming! Fundamental concepts! Evaluation strategies! Pattern matching! Higher order functions! Lazy lists References! Richard Bird, Introduction
More information# true;; - : bool = true. # false;; - : bool = false 9/10/ // = {s (5, "hi", 3.2), c 4, a 1, b 5} 9/10/2017 4
Booleans (aka Truth Values) Programming Languages and Compilers (CS 421) Sasa Misailovic 4110 SC, UIUC https://courses.engr.illinois.edu/cs421/fa2017/cs421a # true;; - : bool = true # false;; - : bool
More informationRecursion and Induction
Recursion and Induction Paul S. Miner NASA Langley Formal Methods Group p.s.miner@nasa.gov 28 November 2007 Outline Recursive definitions in PVS Simple inductive proofs Automated proofs by induction More
More information! " Haskell Expressions are treated as: !" Thunks. !" Closures. !" Delayed Computations. !" Suspensions. ! " Expressions are evaluated: !
CS 410/10: d Programming Lecture 7: Hamming, Closures, Laziness Mark P Jones Portland State University The Hamming Set: hamming = { 1! { 2 * x x " hamming! { * x x " hamming! { * x x " hamming hamming
More informationProgramming Languages
CSE 130 : Fall 2008 Programming Languages Lecture 3: Epressions and Types Ranjit Jhala UC San Diego News PA 1 due (net) Fri 10/10 5pm PA 2 out today or tomorrow Office hours posted on Webpage: Held in
More informationProgramming Languages
CSE 130 : Spring 2011 Programming Languages Lecture 3: Crash Course Ctd, Expressions and Types Ranjit Jhala UC San Diego A shorthand for function binding # let neg = fun f -> fun x -> not (f x); # let
More informationMini-ML. CS 502 Lecture 2 8/28/08
Mini-ML CS 502 Lecture 2 8/28/08 ML This course focuses on compilation techniques for functional languages Programs expressed in Standard ML Mini-ML (the source language) is an expressive core subset of
More information