Lecture 7: General Computation Models
|
|
- Charlotte Stanley
- 6 years ago
- Views:
Transcription
1 Lecture 7: General Computation Models Declarative Programming Techniques (Part 2) November 15, 2017
2 Difference lists Difference list: pair L1#L2 of two lists, such that L2 is a suffix of the first list: L2 may have unbound tail. L1#L2 represents the list obtained by dropping suffix L2 from L1. Examples of difference lists X#X % Represents the empty list nil#nil % idem [a]#[a] % idem (a b c X)#X % represents [a b c] (a b c d X)#(d X) % idem [a b c d]#[d] % idem
3 Features and limitations of difference lists If S1#E1 is a difference list with E1 an unbound variable, we can app another difference list S2#E2 to S1#E1 in constant time: declare fun {AppD D1 D2} S1#E1 = D1 S2#E2 = D2 in E1 = S2 S1#E2 Example local X Y in {Browse {AppD (1 2 3 X)#X (4 5 Y)#Y}} displays the value of ( Y)#Y LIMITATIONS: difference lists can be apped only once they can be used only in special circumstances REMARK: Difference lists originate from Prolog and LP, and are the basis of many advanced Prolog programming techniques.
4 Programming techniques with difference lists Flattening a nested list: Implementation 1 The flattening of a nested list has all elements of the nested list but is no longer nested. We can reason by induction on the syntax (BNF) of nested lists: Flatten of nil is nil. Flatten of X Xr where X is a nested list, is Z where flatten of X is Y, flatten of Xr is Yr, and app Y and Yr to get Z. Flatten of X Xr where X is a not a list, is Z where flatten of Xr is Yr, and Z is X Yr fun {Flatten Xs} case Xs of nil then nil [] X Xr andthen {IsList X} then {App {Flatten X} {Flatten Xr}} [] X Xr then X {Flatten Xr} % this call will display [a b c d e f] {Browse {Flatten [[a b] [[c] [d]] nil [e [f]]]}}
5 Programming techniques with difference lists Flattening a nested list: Implementation 2 (with difference lists) Flatten of nil is X#X (empty difference list). Flatten of X Xr where X is a nested list, is Y1 Y4 where flatten of X is Y1#Y2, flatten of Xr is Y3#Y4, and equate Y2 and Y3 to app the difference lists. Flatten of X Xr where X is a not a list, is (X Y1)#Y2 where flatten of Xr is Y1#Y2 fun {Flatten Xs} proc {FlattenD Xs?Ds} case Xs of nil then Y in Ds=Y#Y [] X Xr andthen {IsList X} then Y1 Y2 Y4 in Ds=Y1#Y4 {FlattenD X Y1#Y2} {FlattenD Xr Y2#Y4} [] X Xr then Y1 Y2 in Ds=(X Y1)#Y2 {FlattenD Xr Y1#Y2} Ys in {FlattenD Xs Ys#nil} Ys
6 Programming techniques with difference lists Flattening a nested list: Remarks about implementation 2 It is efficient. The difference list returned by FlattenD is converted into a regular list by binding its second arg. to nil. FlattenD is written as proc (instead of fun) because we need only part of its output argument (which is the last argument). We can also represent difference lists by two arguments slightly faster implementation: fun {Flatten Xs} proc {FlattenD Xs?S E} case Xs of nil then S=E [] X Xr andthen {IsList X} then Y2 in {FlattenD X S Y2} {FlattenD Xr Y2 E} [] X Xr then Y1 in S=X Y1 {FlattenD Xr Y1 E} Ys in {FlattenD Xs Ys nil} Ys
7 Programming techniques with difference lists Flattening a nested list: a final improvement We can write FlattenD as a function, by making parameter S the output: fun {Flatten Xs} fun {FlattenD Xs E} case Xs of nil then E [] X Xr andthen {IsList X} then {FlattenD X {FlattenD Xr E}} [] X Xr then X {FlattenD Xr E} in {FlattenD Xs nil} REMARK: During the call {FlattenD Xs E}, the parameter E gives the rest of the output after the flattening of the elements from Xs is exhausted.
8 Programming techniques with difference lists Reversing a list The naive version with difference lists: Reverse of nil is X#X (empty difference list). Reverse of X Xs is Z, where reverse of Xs is Y1#Y2 and app Y1#Y2 and (X Y)#Y together to get Z. The last (recursive) case can be rewritten as follows: Reverse of X Xs is Y1#Y, where reverse of Xs is Y1#Y2 and equate Y2 and (X Y). or, even better: Reverse of X Xs is Y1#Y, where reverse of Xs is Y1#(X Y) fun {Reverse Xs} % final version of Reverse proc {ReverseD Xs?Y1 Y} case Xs of nil then Y1=Y [] X Xr then {ReverseD Xr Y1 X Y} Y1 in {ReverseD Xs Y1 nil} Y1
9 Programming techniques with difference lists Queues: Implementation 1 (naìve) A queue (FIFO) is a sequence of elements with an insert and a delete operation elements are inserted at one of the queue, and deleted from the other A naive (and slow) implementation, using list L to represent the queue content: inserting element X gives new queue X L deleting X from non-empty queue L is done by proc {ButLast L?X? L1} % L1 will be the new queue case L of [Y] then X=Y L1=nil [] Y L2 then L3 in L1=Y L3 {ButLast L2 X L3}
10 Programming techniques with difference lists Implementation 2: Queues with amortized constant-time operations Main idea: represent the content of the queue by a pair q(f R), where F, R are lists: F represents the front of the queue, and R represents the back of the queue in reversed form NOTE: the queue content is always {App F {Reverse R}}. declare NewQueue Insert Delete IsEmpty fun {NewQueue} q(nil nil) % create an empty queue fun {Check Q} % move elements from back to front case Q of q(nil R) then q({reverse R} nil) else Q fun {Insert Q X} % insert X to the back of queue case Q of q(f R) then {Check q(f X R)} fun {Delete Q X} % delete element from front of queue case Q of q(f R) then F1 in F=X F1 {Check q(f1 R)} fun {IsEmpty Q} % check if the queue is empty case Q of q(f R) then F==nil
11 Programming techniques with difference lists Implementation 3: Queues with worst-case constant-time operations Main idea: represent the queue by a triple q(n S E) where E is an unbound variable: The content of the queue is given by the difference list S#E N is the number of elements in the queue The queue operations are defined as follows: declare NewQueue Insert Delete IsEmpty fun {NewQueue} X in q(0 X X) % create an empty queue fun {Insert Q X} % insert X in queue Q case Q of q(n S E) then E1 in E=X E1 q(n+1 S E1) fun {Delete Q?X} % delete X from queue Q case Q of q(n S E) then S1 in S=X S1 q(n-1 S1 E) fun {IsEmpty Q} % check if queue Q is empty case Q of q(n S E) then N==0
12 Programming techniques with difference lists Similarities of the last two implementations (1) This example works for both implementations: declare Q1 Q2 Q3 Q4 Q5 Q6 Q7 Q1={NewQueue} Q2={Insert Q1 peter} Q3={Insert Q2 paul} local X in Q4={Delete Q3 X} {Browse X} Q5={Insert Q4 mary} local X in Q6={Delete Q5 X} {Browse X} local X in Q7={Delete Q6 X} {Browse X} For example, if we use implementation 2 (with amortized cost), then declare creates store variables q 1,...,q 7 and makes every Qi refer to q i The next 3 statements bind q 1 to q(nil nil), q 2 to q([peter] nil), and q 3 to q([peter] [paul]) Next: X refers to newly created store variable x 1, x 1 is bound to peter, and q 4 is bound to q([paul] nil) Next: q 5 si bound to q([paul] [mary]) Next: X refers to newly created store variable x 2, x 2 is bound to paul, and q 6 is bound to q([mary] nil) Finally, X refers to newly created store variable x 3, x 3 is bound to mary, and q 7 is bound to q(nil nil)
13 Programming techniques with difference lists Similarities of the last two implementations (2) declare Q1 Q2 Q3 Q4 Q5 Q6 Q7 Q1={NewQueue} Q2={Insert Q1 peter} Q3={Insert Q2 paul} local X in Q4={Delete Q3 X} {Browse X} Q5={Insert Q4 mary} local X in Q6={Delete Q5 X} {Browse X} local X in Q7={Delete Q6 X} {Browse X} If we use implementation 3 (with difference lists), then declare creates store variables q 1,...,q 7 and makes every Qi refer to q i Next statement binds q 1 to q(0 x x). Next statement binds x to peter e 1, and q 2 to q(1 peter e 1 e 1 ) Next statement binds e 1 to paul e 2, and q 3 to q(2 peter paul e 2 e 2 ). Next: X refers to newly created store variable x 1, x 1 is bound to peter, and q 4 is bound to q(1 paul e 2 e 2 ). Next statement binds e 2 to mary e 3, and q 5 to q(2 paul mary e 3 e 3 ). Next: X refers to newly created store variable x 2, x 2 is bound to paul, and q 6 is bound to q(1 mary e 3 e 3 ). Next: X refers to newly created store variable x 2, x 2 is bound to mary, and q 7 is bound to q(0 e 3 e 3 ).
14 Programming techniques with difference lists Differences between the last two implementations If we try to delete an element from an empty stack Q, by feeding declare X Q1 Q1={Delete Q X} {Browse X} then Oz fails for implementation 2 works for implementation 3, because: Note that initially Q refers to a value of the form q(0 x x), which represents an empty stack Q1={Delete Q X} creates two new store variables: x 1 for X, and s 1 ; binds x to x 1 s 1 and returns the value q(-1 s 1 x 1 s 1 ) for Q1. The element X deleted from an empty queue refers to an unbound variable x 1. x 1 will get bound to the first value which will be inserted in queue Q1: declare Q2 Q2 = {Insert Q1 bill} binds x 1 to bill, and makes Q2 refer to q(0 s 1 s 1 ).
15 Persistent and ephemeral data structures A data structure is persistent if its internal representation is not affected by the operations performed on it directly or indirectly: there can be only one version in use at any time. A data structure which is not presistent is called ephemeral. Implementation 2 provides a persistent queue. The following example illustrates the fact that we can safely work with multiple references to the same queue: declare Q1 Q2 Q3 Q4 Q5 Q6 Q1={NewQueue} Q2={Insert Q1 jon} Q3={Insert Q2 paul} Q4={Insert Q2 mary} local X in Q5={Delete Q3 X} {Browse X} local X in Q6={Delete Q4 X} {Browse X} Both Q3 and Q4 refer to Q2. After feeding the first four variable bindings, the environment is E = {Q1 q 1, Q2 q 2, Q3 q 2, Q4 q 4 }, and the store is: q 3 q q 2 q q 1 q q 4 q q 5 unbound q 6 unbound [jon] [paul] nil [mary] Subsequent operations do not affect the values of variables q 1, q 2, q 3, q 4, identified by Q1, Q2, Q3, Q4.
16 Persistent and ephemeral data structures Implementation 3 (with difference lists) is faster, because Insert and Delete are constant-time. Unfortunately, such queues are ephemeral. For example: declare Q1 Q2 Q3 Q4 Q5 Q6 Q1={NewQueue} Q2={Insert Q1 jon} Q3={Insert Q2 paul} Q4={Insert Q2 mary} declare creates six unbound store variables q 1, q 2, q 3, q 4, q 5, q 6 and adds Qi q i to the environment (1 i 6) Q1={NewQueue} binds q 1 to q(0 x x), where is a new unbound variable. Q2={Insert Q1 jon} binds x to jon y, and q 2 to q(1 jon y y). Q3={Insert Q2 paul} binds y to paul z, and q 3 to q(1 jon paul z z) side-effect: q 2 gets bound to q(1 jon paul z paul z) and the assumption that the third component of the queue representation is an unbound variable does not hold anymore Q4={Insert Q2 mary} fails because it tries to make compatible paul z with a value of the form mary e 1.
17 Queues (implementation with difference lists) How to work safely with ephemeral queues (implementation 3)? Workaround: define a Fork operation that takes as input an ephemeral data structure, and creates two identical copies of it. Examples proc {ForkD D?E?F} D1#nil=D E1#E0=E {App D1 E0 E1} F1#F0=F {App D1 F0 F1} in skip {ForkD D E F} binds E and F to two fresh copies of a difference list D whose last component is an unbound variable. D is consumed: it can not be used afterward in the same way (why?). proc {ForkQ Q?Q1?Q2} % forks an ephemeral queue (implem. 3) q(n S E)=Q q(n S1 E1)=Q1 q(n S2 E2)=Q2 in {ForkD S#E S1#E1 S2#E2}
18 Working with trees and ordered binary trees Recursive data structures: Tree ::= leaf tree( Value Tree 1... Tree n ) OBTree ::= leaf tree( OValue Value Tree 1 Tree 2 ) EXTRA ASSUMPTIONS FOR OBTree : OBValue is a subtype of Value that is totally ordered (e.g., numbers or strings). for each non-leaf node, all keys in the first subtree are less than the node key, and all keys in the second subtree are greater than the node key. Operations well supported by instances of OBTree : lookup insertion deletion
19 Operations on ordered binary trees Lookup and Insert fun {Lookup X T} case T of leaf then notfound [] tree(y V T1 T2) then if X<Y then {Lookup X T1} elseif X>Y then {Lookup X T2} else found(v) fun {Insert X V T} case T of leaf then tree(x V leaf leaf) [] tree(y W T1 T2) andthen X==Y then tree(x V T1 T2) [] tree(y W T1 T2) andthen X<Y then tree(y W {Insert X V T1} T2) [] tree(y W T1 T2) andthen X>Y then tree(y W T1 {Insert X V T2})
20 Operations on ordered binary trees Node deletion To delete a node Y form ordered binary tree T, we distinguish two cases: 1 At least one subtree of T is a leaf (easy case): Y T T leaf 2 Neither subtree of T is a leaf: To implement this case, we need an auxiliary function {RemoveSmallest T} which returns the content of the node Yp with minimum key in a tree T, and T without node Yp.
21 Operations on ordered binary trees RemoveSmallest and Delete fun {RemoveSmallest T} case T of leaf then none [] tree(y V T1 T2) then case {RemoveSmallest T1} of none then Y#V#T2 [] Yp#Vp#Tp then Yp#Vp#tree(Y V Tp T2) fun {Delete X T} case T of leaf then leaf [] tree(y W T1 T2) andthen X==Y then case {RemoveSmallest T2} of none then T1 [] Yp#Vp#Tp then tree(yp Vp T1 Tp) [] tree(y W T1 T2) andthen X<Y then tree(y W {Delete X T1} T2) [] tree(y W T1 T2) andthen X>Y then tree(y W T1 {Delete X T2})
22 Tree traversal Tree traversal = perform an operation on its nodes in some well-defined order. Depth-first traversal visits first the node itself, then the leftmost subtree, and then the rightmost subtree. the simplest form of tree traversal Example (print all key/value pairs of tree nodes) proc {DFS T} case T of leaf then skip [] tree(key Val L R) then {Browse Key#Val} {DFS L} {DFS R}
23 Depth-first tree traversal with result calculation Calculation of the list of all key/value pairs stored in the nodes of a tree, in the order given by depth-first traversal: proc {DFSAccLoop T S1?Sn} case T of leaf then Sn=S1 [] tree(key Val L R) then S2 S3 in S2=(Key#Val) S1 {DFSAccLoop L S2 S3} {DFSAccLoop R S3 Sn} fun {DFSAcc T} {Reverse {DFSAccLoop T nil $}} We can also calculate this list in the right order directly: proc {DFSAccLoop2 T S1?Sn} case T of leaf then S1=Sm [] tree(key Val L R) then S2 S3 in S1=(Key#Val) S2 {DFSAccLoop L S2 S3} {DFSAccLoop R S3 Sn} fun {DFSAcc2 T} {DFSAccLoop T $ nil}
24 Breadth-first tree traversal This traversal first traverses all nodes at depth 0, then all nodes at depth 1, and so forth, going one level deeper at a time. At each level, it traverses the nodes from left to right. To implement it, we need a queue to keep track of all the nodes at a given depth. proc {BFS T} fun {TreeInsert Q T} if T\=leaf then {Insert Q T} else Q proc {BFSQueue Q1} if {IsEmpty Q1} then skip else X Q2 Key Val L R in Q2={Delete Q1 X} tree(key Val L R)=X {Browse Key#Val} {BFSQueue {TreeInsert {TreeInsert Q2 L} R}} in {BFSQueue {TreeInsert {NewQueue} T}}
25 Depth-first tree traversal with result calculation Calculation of the list of all key/value pairs stored in the nodes of a tree, in the order given by breadth-first traversal: fun {BFSAcc T} fun {TreeInsert Q T} if T\=leaf then {Insert Q T} else Q proc {BFSQueue Q1?S1 Sn} if {IsEmpty Q1} then S1=Sn else X Q2 Key Val L R S2 in Q2={Delete Q1 X} tree(key Val L R)=X S1=Key#Val S2 {BFSQueue {TreeInsert {TreeInsert Q2 L} R} S2 Sn} in {BFSQueue {TreeInsert {NewQueue} T} $ nil}
26 References Peter van Roy, Seif Haridi: Concepts, Techniques, and Models of Computer Programming. The MIT Press, General Computation Models: Chapter 3: Declarative Programming Techniques. 3.4.
Programming Language Concepts, CS2104 Lecture 7
Reminder of Last Lecture Programming Language Concepts, CS2104 Lecture 7 Tupled Recursion Exceptions Types, ADT, Haskell, Components 5th Oct 2007 CS2104, Lecture 7 1 Overview 5th Oct 2007 CS2104, Lecture
More informationDeclarative Computation Model
Declarative Computation Model Kernel language semantics revisited (VRH 2.4.5) From kernel to practical language (VRH 2.6) Exceptions (VRH 2.7) Carlos Varela RPI March 19, 2012 Adapted with permission from:
More informationProgramming Language Concepts, cs2104 Lecture 09 ( )
Programming Language Concepts, cs2104 Lecture 09 (2003-10-10) Seif Haridi Department of Computer Science, NUS haridi@comp.nus.edu.sg 2003-10-03 S. Haridi, CS2104, L09 (slides: C. Schulte, S. Haridi) 1
More informationDeclarative Concurrency (CTM 4)
Declarative Concurrency (CTM 4) Carlos Varela Rensselaer Polytechnic Institute Adapted with permission from: Seif Haridi KTH Peter Van Roy UCL November 21, 2014 C. Varela; Adapted with permission from
More informationA Second Look At ML. Chapter Seven Modern Programming Languages, 2nd ed. 1
A Second Look At ML Chapter Seven Modern Programming Languages, 2nd ed. 1 Outline Patterns Local variable definitions A sorting example Chapter Seven Modern Programming Languages, 2nd ed. 2 Two Patterns
More informationCarlos Varela RPI September 19, Adapted with permission from: Seif Haridi KTH Peter Van Roy UCL
Higher-Order Programming: Closures, procedural abstraction, genericity, instantiation, embedding. Control abstractions: iterate, map, reduce, fold, filter (CTM Sections 1.9, 3.6, 4.7) Carlos Varela RPI
More informationDeclarative Concurrency (CTM 4)
Declarative Concurrency (CTM 4) Carlos Varela Rensselaer Polytechnic Institute Adapted with permission from: Seif Haridi KTH Peter Van Roy UCL October 30, 2015 C. Varela; Adapted with permission from S.
More informationExam for 2G1512 DatalogiII, Apr 12th
Exam for 2G1512 DatalogiII, Apr 12th 2002 09.00 14.00 Dept. of Microelectronics and Information Technology April 11, 2002 Allowed materials You are allowed to bring the course book and all handouts to
More informationProgramming Language Concepts, cs2104 Lecture 04 ( )
Programming Language Concepts, cs2104 Lecture 04 (2003-08-29) Seif Haridi Department of Computer Science, NUS haridi@comp.nus.edu.sg 2003-09-05 S. Haridi, CS2104, L04 (slides: C. Schulte, S. Haridi) 1
More informationCS 457/557: Functional Languages
CS 457/557: Functional Languages Lists and Algebraic Datatypes Mark P Jones Portland State University 1 Why Lists? Lists are a heavily used data structure in many functional programs Special syntax is
More informationState, Object-Oriented Programming Explicit State, Polymorphism (CTM ) Objects, Classes, and Inheritance (CTM )
State, Object-Oriented Programming Explicit State, Polymorphism (CTM 6.1-6.4.4) Objects, Classes, and Inheritance (CTM 7.1-7.2) Carlos Varela Rensselaer Polytechnic Institute October 27, 2017 Adapted with
More informationOn Academic Dishonesty. Declarative Computation Model. Single assignment store. Single assignment store (2) Single assignment store (3)
Declarative Computation Model Single assignment store (VRH 2.2) Kernel language syntax (VRH 2.3) Carlos Varela RPI October 6, 2009 Adapted with permission from: Seif Haridi KTH Peter Van Roy UCL On Academic
More informationDeclarative Programming Techniques
Declarative Programming Techniques Declarativeness, iterative computation (CTM 3.1-3.2) Higher-order programming (CTM 3.6) Abstract data types (CTM 3.7) Carlos Varela Rensselaer Polytechnic Institute April
More informationLecture 5: Declarative Programming. The Declarative Kernel Language Machine. September 12th, 2011
Lecture 5: Declarative Programming. The Declarative Kernel Language Machine September 12th, 2011 1 Lecture Outline Declarative Programming contd Dataflow Variables contd Expressions and Statements Functions
More informationLogic Programming (CTM ) Constraint Programming: Constraints and Computation Spaces
Logic Programming (CTM 12.3-12.5) Constraint Programming: Constraints and Computation Spaces Carlos Varela Rennselaer Polytechnic Institute December 4, 2015 C. Varela 1 Generate and Test Example We can
More informationPROGRAMMING IN HASKELL. CS Chapter 6 - Recursive Functions
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
More informationHomework 4: Declarative Concurrency
COP 4020 Programming Languages 1 March 31, 2008 Homework 4: Declarative Concurrency Due: problems 3 4, 8 Monday, March 24, 2007 at 11pm; problems 5, 7 Wednesday, March 26, 2007 at 11pm; problems 9 17,
More informationLazy evaluation. Lazy evaluation (3) Lazy evaluation (2) Lazy execution. Example. Declarative Concurrency. Lazy Execution (VRH 4.
Declarative Concurrency Lazy Execution (VRH 4.5) Carlos Varela RPI Lazy evaluation The deault unctions in Oz are evaluated eagerly (as soon as they are called) Another way is lazy evaluation where a computation
More informationLecture 19: Functions, Types and Data Structures in Haskell
The University of North Carolina at Chapel Hill Spring 2002 Lecture 19: Functions, Types and Data Structures in Haskell Feb 25 1 Functions Functions are the most important kind of value in functional programming
More informationTypes in Programming Languages Dynamic and Static Typing, Type Inference (CTM 2.8.3, EPL* 4) Abstract Data Types (CTM 3.7) Monads (GIH** 9)
Types in Programming Languages Dynamic and Static Typing, Type Inference (CTM 2.8.3, EPL* 4) Abstract Data Types (CTM 3.7) Monads (GIH** 9) Carlos Varela Rensselaer Polytechnic Institute September 23,
More informationDeclarative concurrency. March 3, 2014
March 3, 2014 (DP) what is declarativeness lists, trees iterative comutation recursive computation (DC) DP and DC in Haskell and other languages 2 / 32 Some quotes What is declarativeness? ness is important
More informationDATABASE THEORY. Lecture 12: Evaluation of Datalog (2) TU Dresden, 30 June Markus Krötzsch
DATABASE THEORY Lecture 12: Evaluation of Datalog (2) Markus Krötzsch TU Dresden, 30 June 2016 Overview 1. Introduction Relational data model 2. First-order queries 3. Complexity of query answering 4.
More informationProgramming Language Concepts, cs2104 Lecture 01 ( )
Programming Language Concepts, cs2104 Lecture 01 (2003-08-15) Seif Haridi Department of Computer Science, NUS haridi@comp.nus.edu.sg 2002-08-15 S. Haridi, CS2104, L01 (slides: C. Schulte, S. Haridi) 1
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 informationLecture 21: Relational Programming II. November 15th, 2011
Lecture 21: Relational Programming II November 15th, 2011 Lecture Outline Relational Programming contd The Relational Model of Computation Programming with choice and Solve Search Strategies Relational
More informationDisk Accesses. CS 361, Lecture 25. B-Tree Properties. Outline
Disk Accesses CS 361, Lecture 25 Jared Saia University of New Mexico Consider any search tree The number of disk accesses per search will dominate the run time Unless the entire tree is in memory, there
More informationCMSC 330, Fall 2013, Practice Problem 3 Solutions
CMSC 330, Fall 2013, Practice Problem 3 Solutions 1. OCaml and Functional Programming a. Define functional programming Programs are expression evaluations b. Define imperative programming Programs change
More informationFinal Examination: Topics and Sample Problems
Computer Science 52 Final Examination: Topics and Sample Problems Spring Semester, 2015 In examinations the foolish ask questions that the wise cannot answer. Oscar Wilde, 1894 Time and Place Wednesday,
More informationTheorem Proving Principles, Techniques, Applications Recursion
NICTA Advanced Course Theorem Proving Principles, Techniques, Applications Recursion 1 CONTENT Intro & motivation, getting started with Isabelle Foundations & Principles Lambda Calculus Higher Order Logic,
More informationTopic 5: Examples and higher-order functions
CITS 3242 Programming Paradigms Topic 5: Examples and higher-order functions This lecture includes some larger examples in F# and introduces higher-functions. 1 Examples: append Now that we have lists,
More informationCS61B Lecture #20: Trees. Last modified: Mon Oct 8 21:21: CS61B: Lecture #20 1
CS61B Lecture #20: Trees Last modified: Mon Oct 8 21:21:22 2018 CS61B: Lecture #20 1 A Recursive Structure Trees naturally represent recursively defined, hierarchical objects with more than one recursive
More informationShell CSCE 314 TAMU. Functions continued
1 CSCE 314: Programming Languages Dr. Dylan Shell Functions continued 2 Outline Defining Functions List Comprehensions Recursion 3 A Function without Recursion Many functions can naturally be defined in
More informationCS24 Week 8 Lecture 1
CS24 Week 8 Lecture 1 Kyle Dewey Overview Tree terminology Tree traversals Implementation (if time) Terminology Node The most basic component of a tree - the squares Edge The connections between nodes
More informationCS 240 Fall Mike Lam, Professor. Priority Queues and Heaps
CS 240 Fall 2015 Mike Lam, Professor Priority Queues and Heaps Priority Queues FIFO abstract data structure w/ priorities Always remove item with highest priority Store key (priority) with value Store
More informationProgramming II (CS300)
1 Programming II (CS300) Chapter 12: Heaps and Priority Queues MOUNA KACEM mouna@cs.wisc.edu Fall 2018 Heaps and Priority Queues 2 Priority Queues Heaps Priority Queue 3 QueueADT Objects are added and
More informationProgramming Paradigms
PP 2017/18 Unit 12 Functions and Data Types in Haskell 1/45 Programming Paradigms Unit 12 Functions and Data Types in Haskell J. Gamper Free University of Bozen-Bolzano Faculty of Computer Science IDSE
More informationWeek 5 Tutorial Structural Induction
Department of Computer Science, Australian National University COMP2600 / COMP6260 Formal Methods in Software Engineering Semester 2, 2016 Week 5 Tutorial Structural Induction You should hand in attempts
More informationProgramming Language Concepts, CS2104 Lab/Assignment 0 (Lab Session, 3-6pm, 24 th Aug 2007) Deadline for Lab0 : 5pm 28Aug 2007 Tue (submit via IVLE)
Programming Language Concepts, CS2104 Lab/Assignment 0 (Lab Session, 3-6pm, 24 th Aug 2007) Deadline for Lab0 : 5pm 28Aug 2007 Tue (submit via IVLE) CS2104 is a 4 credit points module (written final exam
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 informationDATABASE THEORY. Lecture 15: Datalog Evaluation (2) TU Dresden, 26th June Markus Krötzsch Knowledge-Based Systems
DATABASE THEORY Lecture 15: Datalog Evaluation (2) Markus Krötzsch Knowledge-Based Systems TU Dresden, 26th June 2018 Review: Datalog Evaluation A rule-based recursive query language father(alice, bob)
More informationCMSC 330, Fall 2013, Practice Problems 3
CMSC 330, Fall 2013, Practice Problems 3 1. OCaml and Functional Programming a. Define functional programming b. Define imperative programming c. Define higher-order functions d. Describe the relationship
More informationStandard ML. Data types. ML Datatypes.1
Standard ML Data types ML Datatypes.1 Concrete Datatypes The datatype declaration creates new types These are concrete data types, not abstract Concrete datatypes can be inspected - constructed and taken
More informationTrees. Eric McCreath
Trees Eric McCreath 2 Overview In this lecture we will explore: general trees, binary trees, binary search trees, and AVL and B-Trees. 3 Trees Trees are recursive data structures. They are useful for:
More informationAn Introduction to Functions
Chapter 4 An Introduction to Functions Through the agency of with, we have added identifiers and the ability to name expressions to the language. Much of the time, though, simply being able to name an
More informationLecture 6: The Declarative Kernel Language Machine. September 13th, 2011
Lecture 6: The Declarative Kernel Language Machine September 13th, 2011 Lecture Outline Computations contd Execution of Non-Freezable Statements on the Abstract Machine The skip Statement The Sequential
More informationFoundations of Computation
The Australian National University Semester 2, 2018 Research School of Computer Science Tutorial 5 Dirk Pattinson Foundations of Computation The tutorial contains a number of exercises designed for the
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 informationINTRODUCTION TO HASKELL
INTRODUCTION TO HASKELL PRINCIPLES OF PROGRAMMING LANGUAGES Norbert Zeh Winter 2018 Dalhousie University 1/81 HASKELL: A PURELY FUNCTIONAL PROGRAMMING LANGUAGE Functions are first-class values: Can be
More informationLecture 9 March 4, 2010
6.851: Advanced Data Structures Spring 010 Dr. André Schulz Lecture 9 March 4, 010 1 Overview Last lecture we defined the Least Common Ancestor (LCA) and Range Min Query (RMQ) problems. Recall that an
More informationChapter 6 Abstract Datatypes
Plan Chapter 6 Abstract Datatypes (Version of 17 November 2005) 1. Application: correctly parenthesised texts...... 6.2 2. An abstract datatype for stacks................ 6.6 3. Realisation of the stack
More informationCSE-321 Programming Languages 2012 Midterm
Name: Hemos ID: CSE-321 Programming Languages 2012 Midterm Prob 1 Prob 2 Prob 3 Prob 4 Prob 5 Prob 6 Total Score Max 14 15 29 20 7 15 100 There are six problems on 24 pages in this exam. The maximum score
More information301AA - Advanced Programming
301AA - Advanced Programming Lecturer: Andrea Corradini andrea@di.unipi.it h;p://pages.di.unipi.it/corradini/ Course pages: h;p://pages.di.unipi.it/corradini/dida@ca/ap-18/ AP-2018-19: Algebraic Datatypes
More informationCSc 372 Comparative Programming Languages
CSc 372 Comparative Programming Languages 8 : Haskell Function Examples Christian Collberg collberg+372@gmail.com Department of Computer Science University of Arizona Copyright c 2005 Christian Collberg
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 informationCSc 372. Comparative Programming Languages. 8 : Haskell Function Examples. Department of Computer Science University of Arizona
1/43 CSc 372 Comparative Programming Languages 8 : Haskell Function Examples Department of Computer Science University of Arizona collberg@gmail.com Copyright c 2013 Christian Collberg Functions over Lists
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 information1 Binary trees. 1 Binary search trees. 1 Traversal. 1 Insertion. 1 An empty structure is an empty tree.
Unit 6: Binary Trees Part 1 Engineering 4892: Data Structures Faculty of Engineering & Applied Science Memorial University of Newfoundland July 11, 2011 1 Binary trees 1 Binary search trees Analysis of
More informationCSE341, Fall 2011, Midterm Examination October 31, 2011
CSE341, Fall 2011, Midterm Examination October 31, 2011 Please do not turn the page until the bell rings. Rules: The exam is closed-book, closed-note, except for one side of one 8.5x11in piece of paper.
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 informationLecture 6: Sequential Sorting
15-150 Lecture 6: Sequential Sorting Lecture by Dan Licata February 2, 2012 Today s lecture is about sorting. Along the way, we ll learn about divide and conquer algorithms, the tree method, and complete
More informationVerification of an ML compiler. Lecture 3: Closures, closure conversion and call optimisations
Verification of an ML compiler Lecture 3: Closures, closure conversion and call optimisations Marktoberdorf Summer School MOD 2017 Magnus O. Myreen, Chalmers University of Technology Implementing the ML
More informationPrinciples of Programming Languages
Principles of Programming Languages Lesson 14 Type Checking Collaboration and Management Dana Fisman www.cs.bgu.ac.il/~ppl172 1 Type Checking We return to the issue of type safety we discussed informally,
More information15 212: Principles of Programming. Some Notes on Continuations
15 212: Principles of Programming Some Notes on Continuations Michael Erdmann Spring 2011 These notes provide a brief introduction to continuations as a programming technique. Continuations have a rich
More informationCSC148 Week 7. Larry Zhang
CSC148 Week 7 Larry Zhang 1 Announcements Test 1 can be picked up in DH-3008 A1 due this Saturday Next week is reading week no lecture, no labs no office hours 2 Recap Last week, learned about binary trees
More informationBooleans (aka Truth Values) Programming Languages and Compilers (CS 421) Booleans and Short-Circuit Evaluation. Tuples as Values.
Booleans (aka Truth Values) Programming Languages and Compilers (CS 421) Elsa L Gunter 2112 SC, UIUC https://courses.engr.illinois.edu/cs421/fa2017/cs421d # true;; - : bool = true # false;; - : bool =
More informationHigher-Order Functions
Higher-Order Functions Tjark Weber Functional Programming 1 Based on notes by Pierre Flener, Jean-Noël Monette, Sven-Olof Nyström Tjark Weber (UU) Higher-Order Functions 1 / 1 Tail Recursion http://xkcd.com/1270/
More informationCSCE 314 TAMU Fall CSCE 314: Programming Languages Dr. Flemming Andersen. Haskell Functions
1 CSCE 314: Programming Languages Dr. Flemming Andersen Haskell Functions 2 Outline Defining Functions List Comprehensions Recursion 3 Conditional Expressions As in most programming languages, functions
More informationLecture 8: Recursion and Iteration. Exceptions. Declarative Programming.
Lecture 8: Recursion and Iteration. Exceptions. Declarative Programming. September 26th, 2010 Lecture 8: Recursion and Iteration. Exceptions. Declarative Programming. (1/48) Lecture Outline Memory Management,
More informationCSE 341 : Programming Languages Midterm, Spring 2015
Name: CSE 341 : Programming Languages Midterm, Spring 2015 Please do not turn the page until 12:30. Rules: Closed book, closed note, except for one side of one 8.5x11in piece of paper. Please stop promptly
More informationData Structures. Datatype. Data structure. Today: Two examples. A model of something that we want to represent in our program
Datastructures Data Structures Datatype A model of something that we want to represent in our program Data structure A particular way of storing data How? Depending on what we want to do with the data
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 informationCSE341 Spring 2017, Midterm Examination April 28, 2017
CSE341 Spring 2017, Midterm Examination April 28, 2017 Please do not turn the page until 12:30. Rules: The exam is closed-book, closed-note, etc. except for one side of one 8.5x11in piece of paper. Please
More informationCSE341 Spring 2016, Midterm Examination April 29, 2016
CSE341 Spring 2016, Midterm Examination April 29, 2016 Please do not turn the page until 10:30. Rules: The exam is closed-book, closed-note, etc. except for one side of one 8.5x11in piece of paper. Please
More informationCSE341 Spring 2016, Midterm Examination April 29, 2016
CSE341 Spring 2016, Midterm Examination April 29, 2016 Please do not turn the page until 10:30. Rules: The exam is closed-book, closed-note, etc. except for one side of one 8.5x11in piece of paper. Please
More informationUNIVERSITETET I OSLO
Exam in INF3110, December 12, 2013 Page 1 UNIVERSITETET I OSLO Det matematisk-naturvitenskapelige fakultet Exam in: INF3110 Programming Languages Day of exam: December 12, 2013 Exam hours: 14:30 18:30
More informationCSE 341 : Programming Languages
CSE 341 : Programming Languages Lecture 9 Lexical Scope, Closures Zach Tatlock Spring 2014 Very important concept We know function bodies can use any bindings in scope But now that functions can be passed
More informationLists. Adrian Groza. Department of Computer Science Technical University of Cluj-Napoca
Lists Adrian Groza Department of Computer Science Technical University of Cluj-Napoca Recall... Parameter evaluation Call-by-value Call-by-name Call-by-need Functions Infix operators Local declarations,
More informationAdvanced Algorithm Design and Analysis (Lecture 12) SW5 fall 2005 Simonas Šaltenis E1-215b
Advanced Algorithm Design and Analysis (Lecture 12) SW5 fall 2005 Simonas Šaltenis E1-215b simas@cs.aau.dk Range Searching in 2D Main goals of the lecture: to understand and to be able to analyze the kd-trees
More informationIsabelle s meta-logic. p.1
Isabelle s meta-logic p.1 Basic constructs Implication = (==>) For separating premises and conclusion of theorems p.2 Basic constructs Implication = (==>) For separating premises and conclusion of theorems
More informationCS558 Programming Languages
CS558 Programming Languages Winter 2017 Lecture 6a Andrew Tolmach Portland State University 1994-2017 Iteration into Recursion Any iteration can be written as a recursion, e.g. while (c) {e Scala is equivalent
More informationApplicative, traversable, foldable
Applicative, traversable, foldable Advanced functional programming - Lecture 3 Wouter Swierstra 1 Beyond the monad So far, we have seen how monads define a common abstraction over many programming patterns.
More informationStacks, Queues & Trees. The Stack Interface. The Stack Interface. Harald Gall, Prof. Dr.
Stacks, Queues & Trees Harald Gall, Prof. Dr. Institut für Informatik Universität Zürich http://seal.ifi.uzh.ch The Stack Interface Stack Data structure holding several items. New items added to the top
More informationConsider the following EBNF definition of a small language:
xercise 1: Consider the following BNF definition of a small language: program ::= + decl ::= vardecl ::= "var" ":" "=" ";" procdecl ::= "procedure"
More informationLambda Calculus and Type Inference
Lambda Calculus and Type Inference Björn Lisper Dept. of Computer Science and Engineering Mälardalen University bjorn.lisper@mdh.se http://www.idt.mdh.se/ blr/ October 13, 2004 Lambda Calculus and Type
More informationBinary Search Trees. What is a Binary Search Tree?
Binary Search Trees What is a Binary Search Tree? A binary tree where each node is an object Each node has a key value, left child, and right child (might be empty) Each node satisfies the binary search
More informationAlgorithms: Lecture 10. Chalmers University of Technology
Algorithms: Lecture 10 Chalmers University of Technology Today s Topics Basic Definitions Path, Cycle, Tree, Connectivity, etc. Graph Traversal Depth First Search Breadth First Search Testing Bipartatiness
More informationProject 2: Scheme Interpreter
Project 2: Scheme Interpreter CSC 4101, Fall 2017 Due: 12 November 2017 For this project, you will implement a simple Scheme interpreter in C++ or Java. Your interpreter should be able to handle the same
More informationCS 340 Spring 2019 Midterm Exam
CS 340 Spring 2019 Midterm Exam Instructions: This exam is closed-book, closed-notes. Electronic devices of any kind are not permitted. Write your final answers, tidily, in the boxes provided. Scratch
More informationA general introduction to Functional Programming using Haskell
A general introduction to Functional Programming using Haskell Matteo Rossi Dipartimento di Elettronica e Informazione Politecnico di Milano rossi@elet.polimi.it 1 Functional programming in a nutshell
More informationProgramming II (CS300)
1 Programming II (CS300) Chapter 10: Search and Heaps MOUNA KACEM mouna@cs.wisc.edu Spring 2018 Search and Heaps 2 Linear Search Binary Search Introduction to trees Priority Queues Heaps Linear Search
More informationAdvanced features of Functional Programming (Haskell)
Advanced features of Functional Programming (Haskell) Polymorphism and overloading January 10, 2017 Monomorphic and polymorphic types A (data) type specifies a set of values. Examples: Bool: the type of
More informationParallel and Sequential Data Structures and Algorithms Lecture (Spring 2012) Lecture 25 Suffix Arrays
Lecture 25 Suffix Arrays Parallel and Sequential Data Structures and Algorithms, 15-210 (Spring 2012) Lectured by Kanat Tangwongsan April 17, 2012 Material in this lecture: The main theme of this lecture
More information/633 Introduction to Algorithms Lecturer: Michael Dinitz Topic: Priority Queues / Heaps Date: 9/27/17
01.433/33 Introduction to Algorithms Lecturer: Michael Dinitz Topic: Priority Queues / Heaps Date: 9/2/1.1 Introduction In this lecture we ll talk about a useful abstraction, priority queues, which are
More informationIntroduction to the Lambda Calculus
Introduction to the Lambda Calculus Overview: What is Computability? Church s Thesis The Lambda Calculus Scope and lexical address The Church-Rosser Property Recursion References: Daniel P. Friedman et
More informationDDS Dynamic Search Trees
DDS Dynamic Search Trees 1 Data structures l A data structure models some abstract object. It implements a number of operations on this object, which usually can be classified into l creation and deletion
More informationFall Lecture 13 Thursday, October 11
15-150 Fall 2018 Lecture 13 Thursday, October 11 n queens One s favorite ipod app n queens Queens attack on row, column, or diagonal Task: put n queens safely on an n-by-n board British Museum algorithm
More informationTyped Racket: Racket with Static Types
Typed Racket: Racket with Static Types Version 5.0.2 Sam Tobin-Hochstadt November 6, 2010 Typed Racket is a family of languages, each of which enforce that programs written in the language obey a type
More information1 Dynamic Programming
Recitation 13 Dynamic Programming Parallel and Sequential Data Structures and Algorithms, 15-210 (Fall 2013) November 20, 2013 1 Dynamic Programming Dynamic programming is a technique to avoid needless
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 informationInformatics 1 Functional Programming Lecture 11. Data Representation. Don Sannella University of Edinburgh
Informatics 1 Functional Programming Lecture 11 Data Representation Don Sannella University of Edinburgh Part I Complexity t = n vs t = n 2 10.4 9.6 8.8 8 7.2 6.4 5.6 4.8 4 3.2 2.4 1.6 0.8 0 0.8 1.6 2.4
More information