Programming Languages and Techniques (CIS120)

Size: px
Start display at page:

Download "Programming Languages and Techniques (CIS120)"

Transcription

1 Programming Languages and Techniues (CIS120) Lecture 14 Feb 13, 2012 ImperaBve Queues

2 Homework 4 due at midnight Announcements Homework 5 (ueues) will be available on the web aker the exam It is due Thurs Feb 23 rd at 11:59:59pm Midterm 1 will be in class on Wednesday, February 12 th ROOM: LLAB 10 TIME: 11:00a.m. sharp Covers up to Feb 8 th (no mutable records)

3 Reference Euality Suppose we have two counters. How do we know whether they share the same internal state? type counter = { mutable count : int }! We could increment one and see whether the other s value changes. But we could also just test whether the references alias directly. Ocaml uses == to mean reference euality: two reference values are == if they point to the same data in the heap: r2 == r3! r1 count 0 not (r1 == r2)! r1 = r2! r2 r3 count 0

4 Structural vs. Reference Euality Structural (in)euality: v1 = v2 v1 <> v2! recursively traverses over the structure of the data, comparing the two values components for structural euality funcbon values are never structurally euivalent to anything structural euality can go into an infinite loop (on cyclic structures) use this for comparing immutable datatypes Reference euality: v1 == v2 v1!= v2 Only looks at where the two references point into the heap funcbon values are only eual to themselves euates strictly fewer things than structural euality use this for comparing mutable datatypes

5 A design problem Suppose you are implemen3ng a website to sell 3ckets to a very popular music event. To be fair, you would like to allow people to select seats first come, first served. How would you do it? Understand the problem Some people may visit the website to buy Bckets while others are sbll selecbng their seats Need to remember the order in which people purchase Bckets Define the interface Need a datastructure to store Bcket purchasers Need to add purchasers to the end of the line Need to allow purchasers at the beginning of the line to select seats Needs to be mutable so the state can be shared across web sessions

6 Queue Interface module type QUEUE =! sig! (* type of the data structure *)! type 'a ueue! (* Make a new, empty ueue *)! val create : unit -> 'a ueue! (* Determine if the ueue is empty *)! val is_empty : 'a ueue -> bool! (* Add a value to the tail of the ueue *)! val en : 'a -> 'a ueue -> unit! (* Remove the head value and return it (if any) *)! val de : 'a ueue -> 'a!

7 Define test cases (* Some test cases for the ueue *)! ;; open ListQ! let test () : bool =! let : int ueue = create () in! en 1 ;! en 2 ;! 1 = de! ;; run_test "ueue test 1" test! let test () : bool =! let : int ueue = create () in! en 1 ;! en 2 ;! let _ = de in! 2 = de! ;; run_test "ueue test 2" test!

8 Implement the behavior module ListQ : QUEUE = struct! type 'a ueue = { mutable contents : a list }! let create () : 'a ueue =! { contents = [] }! let is_empty (:'a ueue) : bool =!.contents = []! let en (x:'a) (:'a ueue) : unit =!.contents <- [x])! let de (:'a ueue) : 'a =! begin match.contents with! [] -> failwith "de called on empty ueue"! x::tl ->.contents <- tl; x! Here we are using type abstracbon to protect the state. Outside of the module, no one knows that ueues are implemented with references. So, only these funcbons can modify the state of a ueue.

9 A Beder ImplementaBon ImplementaBon is slow because of [x] copies the enbre list each Bme As the ueue gets longer, it takes longer to add data Only has a single reference to the beginning of the list Let's do it again with TWO references, one to the beginning (head) and one to the end (tail). Deueue by updabng the head reference (as before) Enueue by updabng the tail of the list The list itself must be mutable because we add to one end and remove from the other Step 1: Understand the problem

10 Data Structure for Mutable Queues type 'a node = {! v: 'a;! mutable next : 'a node option! }! type 'a ueue = { mutable head : 'a node option;! mutable tail : 'a node option }! There are two parts to a mutable ueue: the internal nodes of the ueue with links from one to the next the head and tail references themselves All of the references are opbons so that the ueue can be empty.

11 Queues in the Heap None! None! An empty ueue Some! Some! None! A ueue with one element Some! Some! Some! None! A ueue with two elements

12 Visual Shorthand: AbbreviaBng OpBons Val! means Some! Val! An empty ueue means None! A ueue with one element A ueue with two elements

13 Bogus values of type int ueue! head is None, tail is Some n head is Some, tail is None tail is not reachable from the head

14 More bogus int ueues! tail doesn t point to the last element of the ueue the links contain a cycle!

15 Queue Invariants Just as we imposed some restricbons on which trees are legibmate Binary Search Trees, Queues must also sabsfy some invariants: Either: (1) head and tail are both None (i.e. the ueue is empty) or (2) head is Some n1, tail is Some n2 and - n2 is reachable from n1 by following next pointers - n2.next is None! We can check that these properbes rule out all of the bogus examples. A ueue operabon may assume that these ueue invariants hold of its inputs, so long as it ensures that the invariants hold when it s done.

16 create and is_empty (* create an empty ueue *)! let create () : 'a ueue =! { head = None;! tail = None }! (* determine whether a ueue is empty *)! let is_empty (:'a ueue) : bool =!.head = None! create establishes the ueue invariants both head and tail are None is_empty assumes the ueue invariants it doesn t have to check that.tail is None

17 en! (* add an element to the tail of a ueue *)! let en (x: 'a) (: 'a ueue) : unit =! in! begin match.tail with! None ->! (* Note that the invariant tells us! that.head is also None *)!.head <- Some newnode;!.tail <- Some newnode! Some n ->! n.next <- Some newnode;!.tail <- Some newnode! The code for en is informed by the ueue invariant: either the ueue is empty, and we just update head and tail, or the ueue is non- empty, in which case we have to patch up the next link of the old tail node to maintain the ueue invariant.

18 Calling En on a non- empty ueue en 2! en fun (x: 'a) (: 'a ueue) ->! in begin match.tail with!

19 Calling En on a non- empty ueue en 2! en fun (x: 'a) (: 'a ueue) ->! in begin match.tail with!

20 Calling En on a non- empty ueue 2! en fun (x: 'a) (: 'a ueue) ->! in begin match.tail with!

21 Calling En on a non- empty ueue 2! en fun (x: 'a) (: 'a ueue) ->! in begin match.tail with!

22 Calling En on a non- empty ueue 2! en fun (x: 'a) (: 'a ueue) ->! in begin match.tail with!

23 Calling En on a non- empty ueue ( 2 )! en fun (x: 'a) (: 'a ueue) ->! in begin match.tail with!

24 Calling En on a non- empty ueue in! begin match.tail with! None ->!.head <- Some newnode;!.tail <- Some newnode! Some n ->! n.next <- Some newnode;!.tail <- Some newnode! en fun (x: 'a) (: 'a ueue) ->! in begin match.tail with!

25 Calling En on a non- empty ueue in! begin match.tail with! None ->!.head <- Some newnode;!.tail <- Some newnode! Some n ->! n.next <- Some newnode;!.tail <- Some newnode! en fun (x: 'a) (: 'a ueue) ->! in begin match.tail with!

26 Calling En on a non- empty ueue let newnode = {v=2; next=none} in! begin match.tail with! None ->!.head <- Some newnode;!.tail <- Some newnode! Some n ->! n.next <- Some newnode;!.tail <- Some newnode! en fun (x: 'a) (: 'a ueue) ->! in begin match.tail with!

27 Calling En on a non- empty ueue let newnode = {v=2; next=none} in! begin match.tail with! None ->!.head <- Some newnode;!.tail <- Some newnode! Some n ->! n.next <- Some newnode;!.tail <- Some newnode! en fun (x: 'a) (: 'a ueue) ->! in begin match.tail with!

28 Calling En on a non- empty ueue let newnode = in! begin match.tail with! None ->!.head <- Some newnode;!.tail <- Some newnode! Some n ->! n.next <- Some newnode;!.tail <- Some newnode! en fun (x: 'a) (: 'a ueue) ->! in begin match.tail with! Note: there is no Some bubble this is a node not a node opbon.

29 Calling En on a non- empty ueue let newnode = in! begin match.tail with! None ->!.head <- Some newnode;!.tail <- Some newnode! Some n ->! n.next <- Some newnode;!.tail <- Some newnode! en fun (x: 'a) (: 'a ueue) ->! in begin match.tail with!

30 Calling En on a non- empty ueue begin match.tail with! None ->!.head <- Some newnode;!.tail <- Some newnode! Some n ->! n.next <- Some newnode;!.tail <- Some newnode! en fun (x: 'a) (: 'a ueue) ->! in begin match.tail with! newnode

31 Calling En on a non- empty ueue begin match.tail with! None ->!.head <- Some newnode;!.tail <- Some newnode! Some n ->! n.next <- Some newnode;!.tail <- Some newnode! en fun (x: 'a) (: 'a ueue) ->! in begin match.tail with! newnode

32 Calling En on a non- empty ueue begin match.tail with! None ->!.head <- Some newnode;!.tail <- Some newnode! Some n ->! n.next <- Some newnode;!.tail <- Some newnode! en fun (x: 'a) (: 'a ueue) ->! in begin match.tail with! newnode

33 Calling En on a non- empty ueue begin match.tail with! None ->!.head <- Some newnode;!.tail <- Some newnode! Some n ->! n.next <- Some newnode;!.tail <- Some newnode! en fun (x: 'a) (: 'a ueue) ->! in begin match.tail with! newnode

34 Calling En on a non- empty ueue begin match with! None ->!.head <- Some newnode;!.tail <- Some newnode! Some n ->! n.next <- Some newnode;!.tail <- Some newnode! en fun (x: 'a) (: 'a ueue) ->! in begin match.tail with! newnode

35 Calling En on a non- empty ueue begin match with! None ->!.head <- Some newnode;!.tail <- Some newnode! Some n ->! n.next <- Some newnode;!.tail <- Some newnode! en fun (x: 'a) (: 'a ueue) ->! in begin match.tail with! newnode

36 Calling En on a non- empty ueue? begin match with! None ->!.head <- Some newnode;!.tail <- Some newnode! Some n ->! n.next <- Some newnode;!.tail <- Some newnode! en fun (x: 'a) (: 'a ueue) ->! in begin match.tail with! newnode

37 Calling En on a non- empty ueue? begin match with! None ->!.head <- Some newnode;!.tail <- Some newnode! Some n ->! n.next <- Some newnode;!.tail <- Some newnode! en fun (x: 'a) (: 'a ueue) ->! in begin match.tail with! newnode

38 Calling En on a non- empty ueue n.next <- Some newnode;!.tail <- Some newnode! en fun (x: 'a) (: 'a ueue) ->! in begin match.tail with! Note: n points to a node, not a node opbon. newnode n

39 Calling En on a non- empty ueue n.next <- Some newnode;!.tail <- Some newnode! en fun (x: 'a) (: 'a ueue) ->! in begin match.tail with! newnode n

40 Calling En on a non- empty ueue.next <- Some newnode;!.tail <- Some newnode! en fun (x: 'a) (: 'a ueue) ->! in begin match.tail with! newnode n

41 Calling En on a non- empty ueue.next <- Some newnode;!.tail <- Some newnode! en fun (x: 'a) (: 'a ueue) ->! in begin match.tail with! newnode n

42 Calling En on a non- empty ueue.next <- Some ;!.tail <- Some newnode! en fun (x: 'a) (: 'a ueue) ->! in begin match.tail with! newnode n

43 Calling En on a non- empty ueue.next <- Some ;!.tail <- Some newnode! en fun (x: 'a) (: 'a ueue) ->! in begin match.tail with! newnode n

44 Calling En on a non- empty ueue.next <- ;!.tail <- Some newnode! en fun (x: 'a) (: 'a ueue) ->! in begin match.tail with! newnode n

45 Calling En on a non- empty ueue.next <- ;!.tail <- Some newnode! en fun (x: 'a) (: 'a ueue) ->! in begin match.tail with! newnode n

46 Calling En on a non- empty ueue ();!.tail <- Some newnode! en fun (x: 'a) (: 'a ueue) ->! in begin match.tail with! newnode n

47 Calling En on a non- empty ueue ();!.tail <- Some newnode! en fun (x: 'a) (: 'a ueue) ->! in begin match.tail with! newnode n

48 Calling En on a non- empty ueue.tail <- Some newnode! en fun (x: 'a) (: 'a ueue) ->! in begin match.tail with! newnode n

49 Calling En on a non- empty ueue.tail <- Some newnode! en fun (x: 'a) (: 'a ueue) ->! in begin match.tail with! newnode n

50 Calling En on a non- empty ueue.tail <- Some newnode! en fun (x: 'a) (: 'a ueue) ->! in begin match.tail with! newnode n

51 Calling En on a non- empty ueue.tail <- Some newnode! en fun (x: 'a) (: 'a ueue) ->! in begin match.tail with! newnode n

52 Calling En on a non- empty ueue.tail <- Some! en fun (x: 'a) (: 'a ueue) ->! in begin match.tail with! newnode n

53 Calling En on a non- empty ueue.tail <- Some.! en fun (x: 'a) (: 'a ueue) ->! in begin match.tail with! newnode n

54 Calling En on a non- empty ueue.tail <-! en fun (x: 'a) (: 'a ueue) ->! in begin match.tail with! newnode n

55 Calling En on a non- empty ueue.tail <-.! en fun (x: 'a) (: 'a ueue) ->! in begin match.tail with! newnode n

56 Calling En on a non- empty ueue ()! en fun (x: 'a) (: 'a ueue) ->! in begin match.tail with! newnode n

57 Calling En on a non- empty ueue ()! en fun (x: 'a) (: 'a ueue) ->! in begin match.tail with! POP! newnode n

58 Calling En on a non- empty ueue ()! en fun (x: 'a) (: 'a ueue) ->! in begin match.tail with! DONE!

59 Calling En on a non- empty ueue ()! en fun (x: 'a) (: 'a ueue) ->! in begin match.tail with! Notes: - the en funcbon imperabvely updated the structure of - the new structure sbll sabsfies the ueue invariants

60 de! (* remove an element from the head of the ueue *)! let de (: 'a ueue) : 'a =! begin match.head with! None ->! failwith "de called on empty ueue"! Some n ->!.head <- n.next;! if n.next = None then.tail <- None;! n.v! The code for de must also patch pointers to maintain the ueue invariant: The head pointer is always updated to the next element in the ueue. If the removed node was the last one in the ueue, the tail pointer must be updated to None

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120) Programming Languages and Techniques () Lecture 11 Feb 12, 2014 OpBons and Unit Announcements Homework 4 available on the web today due Tuesday, Feb 17 th n- body physics simulabon start early; see Piazza

More information

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120) Programming Languages and Techniques (CIS120) Lecture 11 February 5, 2018 Review: Abstract types Finite Maps Homework 3 due tomorrow at 11:59:59pm Announcements (Homework 4 is due Tuesday, 2/20, and won

More information

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120) Programming Languages and Techniques (CIS120) Lecture 10 February 2, 2018 Abstract types: Sets Chapter 10 Announcements Homework 3 due Tuesday at 11:59:59pm Midterm 1 Friday, February 9, in class Covers

More information

CIS 120 Midterm II March 29, 2013 SOLUTIONS

CIS 120 Midterm II March 29, 2013 SOLUTIONS CIS 120 Midterm II March 29, 2013 SOLUTIONS 1 1. Java True/False (20 points) Circle T or F. a. T F In the Java ASM, object values are stored in the heap. b. T F In the Java ASM, method definitions are

More information

CIS 120 Midterm I February 16, 2015 SOLUTIONS

CIS 120 Midterm I February 16, 2015 SOLUTIONS CIS 120 Midterm I February 16, 2015 SOLUTIONS 1 1. Substitution semantics (18 points) Circle the final result of simplifying the following OCaml expressions, or Infinite loop if there is no final answer.

More information

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120) Programming Languages and Techniques () Lecture 12 February 7, 2018 Partiality, Sequencing, Records Chapters 12, 13 Midterm 1 This Friday in class Review session Tonight, 6 7:30 PM DRLB A1 Announcements

More information

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120) Programming Languages and Techniques (CIS120) Lecture 10 February 5 th, 2016 Abstract types: sets Lecture notes: Chapter 10 What is the value of this expresssion? let f (x:bool) (y:int) : int = if x then

More information

CIS 120 Midterm I October 2, Name (printed): Username (login id):

CIS 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 information

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120) Programming Languages and Techniques (CIS120) Lecture 26 Mar 21, 2012 EncapsulaBon & Queues CIS120 / Spring 2012 EncapsulaBon Object encapsulabon EncapsulaBon preserves invariants about the state of the

More information

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120) Programming Languages and Techniques (CIS120) Lecture 7 January 29 th, 2016 Binary Search Trees (Lecture notes Chapter 7) let rec height (t:tree) : int = begin match t with Empty -> 0 Node (left, _, right)

More information

Name: Pennkey: CIS 120 Midterm II November 18, 2011

Name: Pennkey: CIS 120 Midterm II November 18, 2011 Name: Pennkey: CIS 120 Midterm II November 18, 2011 1 /20 2 /20 3 /20 4 /20 5 /20 Total /100 Do not begin the exam until you are told to do so. You have 50 minutes to complete the exam. There are 100 total

More information

CIS 120 Midterm II November 7, Name (printed): Pennkey (login id):

CIS 120 Midterm II November 7, Name (printed): Pennkey (login id): CIS 120 Midterm II November 7, 2014 Name (printed): Pennkey (login id): My signature below certifies that I have complied with the University of Pennsylvania s Code of Academic Integrity in completing

More information

CIS 120 Midterm II November 8, 2013 SOLUTIONS

CIS 120 Midterm II November 8, 2013 SOLUTIONS CIS 120 Midterm II November 8, 2013 SOLUTIONS 1 1. Facts about OCaml and Java (15 points) For each part, circle true or false. a. T F The.equals method in Java is roughly similar to OCaml s = operator.

More information

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120) Programming Languages and Techniques (CIS120) Lecture 8 Feb 5, 2014 Abstract Types: Sets Modules and Interfaces Homework 3 is available Announcements Due TUESDAY, February 11 th at 11:59:59pm PracSce with

More information

CIS 120 Midterm II November 8, Name (printed): Pennkey (login id):

CIS 120 Midterm II November 8, Name (printed): Pennkey (login id): CIS 120 Midterm II November 8, 2013 Name (printed): Pennkey (login id): My signature below certifies that I have complied with the University of Pennsylvania s Code of Academic Integrity in completing

More information

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120) Programming Languages and Techniques (CIS120) Lecture 5 Jan 18, 2013 Tuples and Lists No class Monday Announcements Homework 1 due Tuesday at midnight Don t wait! 24 people have already submiped See schedule

More information

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120) Programming Languages and Techniques (CIS120) Lecture 22 March 14 th, 2016 Object Oriented Programming in Java Java Bootcamp tonight Announcements Monday, March 14 from 6-8pm in Levine 101 (Wu & Chen)

More information

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120) Programming Languages and Techniques (CIS120) Lecture 26 November 3, 2017 The Java ASM, Java Generics Chapter 24 Announcements HW7: Chat Server Available on Codio / InstrucNons on the web site Due Tuesday,

More information

Which of the following expressions has the type int list?

Which 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 information

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120) Programming Languages and Techniques () Lecture 20 February 28, 2018 Transition to Java Announcements HW05: GUI programming Due: THURSDAY!! at 11:59:59pm Lots of TA office hours today Thursday See Piazza

More information

CIS 120 Midterm II November 16, Name (printed): Pennkey (login id):

CIS 120 Midterm II November 16, Name (printed): Pennkey (login id): CIS 120 Midterm II November 16, 2012 Name (printed): Pennkey (login id): My signature below certifies that I have complied with the University of Pennsylvania s Code of Academic Integrity in completing

More information

CIS 120 Midterm II November 16, 2012 SOLUTIONS

CIS 120 Midterm II November 16, 2012 SOLUTIONS CIS 120 Midterm II November 16, 2012 SOLUTIONS 1 1. Java vs. OCaml (22 points) a. In OCaml, the proper way to check whether two string values s and t are structurally equal is: s == t s = t s.equals(t)

More information

Name: CIS 120e Midterm I Review

Name: CIS 120e Midterm I Review Name: CIS 120e Midterm I Review This is a review for the midterm. These problems are intended to be indicative of the kind that might appear on the exam, though you should, of course, expect variations.

More information

Programming Languages and Techniques (CIS120e)

Programming Languages and Techniques (CIS120e) Programming Languages and Techniques (CIS120e) Lecture 7 Sep 15, 2010 Binary Search Trees (Part II), Generics Announcements Homework 2 is due tonight at 11:59:59pm. Homework 3 will be available on the

More information

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120) Programming Languages and Techniques (CIS120) Lecture 8 February 1, 2016 BST Delete Generics (Chapters 7 & 8) Announcements Read Chapters 7 & 8 (BSTs, generics) of lecture notes Read Chapter 9 of lecture

More information

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120) Programming Languages and Techniques (CIS120) Lecture 9 Jan 30, 2013 Abstract Types: Sets Announcements Homework 3 is available on the web Due MONDAY, February 4 th at 11:59:59pm PracRce with BSTs, generic

More information

Name: Pennkey (eg, sweirich): CIS 120 Final Exam May 8, 2012

Name: Pennkey (eg, sweirich): CIS 120 Final Exam May 8, 2012 Name: Pennkey (eg, sweirich): CIS 120 Final Exam May 8, 2012 My signature below certifies that I have complied with the University of Pennsylvania s Code of Academic Integrity in completing this examination.

More information

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120) Programming Languages and Techniques () Lecture 6 January 24, 2018 Binary Search Trees (Lecture notes Chapter 7) Announcements Homework 2: Computing Human Evolution due Tuesday, September 19 th Reading:

More information

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120) Programming Languages and Techniques (CIS120) Lecture 23 March 16, 2018 Arrays, Java ASM What is the value of ans at the end of this program? int[] a = {1, 2, 3, 4; int[] b = a; b[0] = 0; int ans = a[0];

More information

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120) Programming Languages and Techniques (CIS120) Lecture 27 Mar 23, 2012 Generics, CollecBons and IteraBon Announcements CIS Course Faire TODAY at 3PM, here HW08 is due on Monday, at 11:59:59pm Midterm 2

More information

CMSC 330: Organization of Programming Languages. Functional Programming with Lists

CMSC 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 information

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120) Programming Languages and Techniques (CIS120) Lecture 3 September 5, 2018 Value-Oriented Programming (continued) Lists and Recursion CIS 120 Announcements Homework 1: OCaml Finger Exercises Due: Tuesday

More information

CSE 341 Section 5. Winter 2018

CSE 341 Section 5. Winter 2018 CSE 341 Section 5 Winter 2018 Midterm Review! Variable Bindings, Shadowing, Let Expressions Boolean, Comparison and Arithmetic Operations Equality Types Types, Datatypes, Type synonyms Tuples, Records

More information

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120) Programming Languages and Techniques (CIS120) Lecture 7 January 26, 2018 Binary Search Trees: Inserting and Deleting (Chapters 7 & 8) Announcements Read Chapters 7 & 8 Binary Search Trees Next up: generics

More information

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120) Programming Languages and Techniques (CIS120) Lecture 24 March 18, 2016 The Java ASM What is the value of ans at the end of this program? Counter[] a = { new Counter(), new Counter() ; Counter[] b = {

More information

CIS 120 Midterm II March 31, 2017 SOLUTIONS

CIS 120 Midterm II March 31, 2017 SOLUTIONS CIS 120 Midterm II March 31, 2017 SOLUTIONS 1 1. OCaml and Java (14 points) Check one box for each part. a. The following OCaml function will terminate by exhausting stack space if called as loop 10: let

More information

Principles of Functional Programming

Principles of Functional Programming 15-150 Principles of Functional Programming Some Slides for Lecture 16 Modules March 20, 2018 Michael Erdmann Signatures & Structures A signature specifies an interface. A structure provides an implementation.

More information

l Determine if a number is odd or even l Determine if a number/character is in a range - 1 to 10 (inclusive) - between a and z (inclusive)

l Determine if a number is odd or even l Determine if a number/character is in a range - 1 to 10 (inclusive) - between a and z (inclusive) Final Exam Exercises Chapters 1-7 + 11 Write C++ code to: l Determine if a number is odd or even CS 2308 Fall 2016 Jill Seaman l Determine if a number/character is in a range - 1 to 10 (inclusive) - between

More information

9/26/2018 Data Structure & Algorithm. Assignment04: 3 parts Quiz: recursion, insertionsort, trees Basic concept: Linked-List Priority queues Heaps

9/26/2018 Data Structure & Algorithm. Assignment04: 3 parts Quiz: recursion, insertionsort, trees Basic concept: Linked-List Priority queues Heaps 9/26/2018 Data Structure & Algorithm Assignment04: 3 parts Quiz: recursion, insertionsort, trees Basic concept: Linked-List Priority queues Heaps 1 Quiz 10 points (as stated in the first class meeting)

More information

Chapter 20: Binary Trees

Chapter 20: Binary Trees Chapter 20: Binary Trees 20.1 Definition and Application of Binary Trees Definition and Application of Binary Trees Binary tree: a nonlinear linked list in which each node may point to 0, 1, or two other

More information

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120) Programming Languages and Techniques (CIS120) Lecture 7 January 30 th, 2015 Binary Search Trees (Lecture notes Chapter 7) ! Homework #1 feedback dope assignment, got a 96.4 the first time, careless mistake,

More information

CIS 120 Final Exam 19 December 2014 SOLUTIONS

CIS 120 Final Exam 19 December 2014 SOLUTIONS CIS 120 Final Exam 19 December 2014 SOLUTIONS 1 1. Java Concepts: True or False (10 points) All of the following questions pertain to Java. a. T F If s and t are variables of type String such that s ==

More information

You must include this cover sheet. Either type up the assignment using theory4.tex, or print out this PDF.

You must include this cover sheet. Either type up the assignment using theory4.tex, or print out this PDF. 15-122 Assignment 4 Page 1 of 12 15-122 : Principles of Imperative Computation Fall 2012 Assignment 4 (Theory Part) Due: Thursday, October 18, 2012 at the beginning of lecture Name: Andrew ID: Recitation:

More information

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120) Programming Languages and Techniques () Lecture 13 February 12, 2018 Mutable State & Abstract Stack Machine Chapters 14 &15 Homework 4 Announcements due on February 20. Out this morning Midterm results

More information

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120) Programming Languages and Techniques (CIS120) Lecture 8 Jan 28, 2013 BSTs II and Generic Types Homework 2 due tomorrow Announcements Key insight: Binary Search Trees (BST) We can use an ordering on the

More information

introduction to Programming in C Department of Computer Science and Engineering Lecture No. #40 Recursion Linear Recursion

introduction to Programming in C Department of Computer Science and Engineering Lecture No. #40 Recursion Linear Recursion introduction to Programming in C Department of Computer Science and Engineering Lecture No. #40 Recursion Linear Recursion Today s video will talk about an important concept in computer science which is

More information

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120) Programming Languages and Techniques (CIS120) Lecture 27 March 23 rd, 2016 Generics and Collections Chapter 25 HW #6 due Tuesday Announcements I will be away all next week (FP workshop in Germany) Monday's

More information

Data Structures. Alice E. Fischer. Lecture 4, Fall Alice E. Fischer Data Structures L4... 1/19 Lecture 4, Fall / 19

Data Structures. Alice E. Fischer. Lecture 4, Fall Alice E. Fischer Data Structures L4... 1/19 Lecture 4, Fall / 19 Data Structures Alice E. Fischer Lecture 4, Fall 2018 Alice E. Fischer Data Structures L4... 1/19 Lecture 4, Fall 2018 1 / 19 Outline 1 Ordered Lists 2 Sorted Lists Tail Pointers 3 Doubly Linked Lists

More information

PREPARING FOR PRELIM 1

PREPARING FOR PRELIM 1 PREPARING FOR PRELIM 1 CS 1110: FALL 2012 This handout explains what you have to know for the first prelim. There will be a review session with detailed examples to help you study. To prepare for the prelim,

More information

CS61BL. Lecture 5: Graphs Sorting

CS61BL. Lecture 5: Graphs Sorting CS61BL Lecture 5: Graphs Sorting Graphs Graphs Edge Vertex Graphs (Undirected) Graphs (Directed) Graphs (Multigraph) Graphs (Acyclic) Graphs (Cyclic) Graphs (Connected) Graphs (Disconnected) Graphs (Unweighted)

More information

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120) Programming Languages and Techniques (CIS120) Lecture 21 March 2 nd, 2016 GUI: notifiers Transition to Java What is the type of xs? let r = {contents = 3} let xs = [(fun () -> r.contents

More information

61A LECTURE 27 PARALLELISM. Steven Tang and Eric Tzeng August 8, 2013

61A LECTURE 27 PARALLELISM. Steven Tang and Eric Tzeng August 8, 2013 61A LECTURE 27 PARALLELISM Steven Tang and Eric Tzeng August 8, 2013 Announcements Practice Final Exam Sessions Worth 2 points extra credit just for taking it Sign-up instructions on Piazza (computer based

More information

CMSC 330: Organization of Programming Languages. Functional Programming with Lists

CMSC 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 information

! Determine if a number is odd or even. ! Determine if a number/character is in a range. - 1 to 10 (inclusive) - between a and z (inclusive)

! Determine if a number is odd or even. ! Determine if a number/character is in a range. - 1 to 10 (inclusive) - between a and z (inclusive) Final Exam Exercises CS 2308 Spring 2014 Jill Seaman Chapters 1-7 + 11 Write C++ code to: Determine if a number is odd or even Determine if a number/character is in a range - 1 to 10 (inclusive) - between

More information

! Determine if a number is odd or even. ! Determine if a number/character is in a range. - 1 to 10 (inclusive) - between a and z (inclusive)

! Determine if a number is odd or even. ! Determine if a number/character is in a range. - 1 to 10 (inclusive) - between a and z (inclusive) Final Exam Exercises Chapters 1-7 + 11 Write C++ code to:! Determine if a number is odd or even CS 2308 Fall 2018 Jill Seaman! Determine if a number/character is in a range - 1 to 10 (inclusive) - between

More information

Largest Online Community of VU Students

Largest Online Community of VU Students WWW.VUPages.com WWW.VUTUBE.EDU.PK http://forum.vupages.com Largest Online Community of VU Students MIDTERM EXAMINATION SEMESTER FALL 2003 CS301-DATA STRUCTURE Total Marks:86 Duration: 60min Instructions

More information

Mutable Data Types. Prof. Clarkson Fall A New Despair Mutability Strikes Back Return of Imperative Programming

Mutable Data Types. Prof. Clarkson Fall A New Despair Mutability Strikes Back Return of Imperative Programming Mutable Data Types A New Despair Mutability Strikes Back Return of Imperative Programming Prof. Clarkson Fall 2017 Today s music: The Imperial March from the soundtrack to Star Wars, Episode V: The Empire

More information

Dialects of ML. CMSC 330: Organization of Programming Languages. Dialects of ML (cont.) Features of ML. Functional Languages. Features of ML (cont.

Dialects 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 information

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120) Programming Languages and Techniques (CIS120) Lecture 39 April 25, 2018 Semester Recap Announcements 1 Game Project Complete Code Due: TONIGHT at midnight Schedule Grading Demo with your TAs! You should

More information

Verification in Coq. Prof. Clarkson Fall Today s music: Check Yo Self by Ice Cube

Verification in Coq. Prof. Clarkson Fall Today s music: Check Yo Self by Ice Cube Verification in Coq Prof. Clarkson Fall 2017 Today s music: Check Yo Self by Ice Cube Review Previously in 3110: Functional programming in Coq Logic in Coq Curry-Howard correspondence (proofs are programs)

More information

CS251-SE1. Midterm 2. Tuesday 11/1 8:00pm 9:00pm. There are 16 multiple-choice questions and 6 essay questions.

CS251-SE1. Midterm 2. Tuesday 11/1 8:00pm 9:00pm. There are 16 multiple-choice questions and 6 essay questions. CS251-SE1 Midterm 2 Tuesday 11/1 8:00pm 9:00pm There are 16 multiple-choice questions and 6 essay questions. Answer the multiple choice questions on your bubble sheet. Answer the essay questions in the

More information

CSCD 255 HW 2. No string (char arrays) or any other kinds of array variables are allowed

CSCD 255 HW 2. No string (char arrays) or any other kinds of array variables are allowed CSCD 255 HW 2 Design a program called cscd255hw2.c which reads in a strictly positive integer (1 or greater) from the user. The user will then be prompted with a menu of choices (this menu should be repetitively

More information

Programming Languages and Techniques (CIS120e)

Programming Languages and Techniques (CIS120e) Programming Languages and Techniques (CIS120e) Lecture 10 Sep 29, 2010 Abstract Stack Machine & First- Class FuncGons Announcements Homework 3 is due tonight at 11:59:59pm. Homework 4 will be available

More information

CSCE 2014 Final Exam Spring Version A

CSCE 2014 Final Exam Spring Version A CSCE 2014 Final Exam Spring 2017 Version A Student Name: Student UAID: Instructions: This is a two-hour exam. Students are allowed one 8.5 by 11 page of study notes. Calculators, cell phones and computers

More information

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120) Programming Languages and Techniques () Lecture 5 January 22, 2018 Datatypes and Trees Announcements Homework 1 due tomorrow at 11:59pm! Homework 2 available soon due Tuesday, January 30 th Read Chapters

More information

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120) Programming Languages and Techniques () Lecture 5 September 10, 2018 Datatypes and Trees Announcements Homework 1 due tomorrow at 11:59pm! Homework 2 available soon due Tuesday, September 18 th Read Chapters

More information

This is a set of practice questions for the final for CS16. The actual exam will consist of problems that are quite similar to those you have

This is a set of practice questions for the final for CS16. The actual exam will consist of problems that are quite similar to those you have This is a set of practice questions for the final for CS16. The actual exam will consist of problems that are quite similar to those you have encountered on homeworks, the midterm, and on this practice

More information

Balanced Trees. Prof. Clarkson Fall Today s music: Get the Balance Right by Depeche Mode

Balanced Trees. Prof. Clarkson Fall Today s music: Get the Balance Right by Depeche Mode Balanced Trees Prof. Clarkson Fall 2017 Today s music: Get the Balance Right by Depeche Mode Prelim See Piazza post @422 Makeup: Thursday night 5:30 pm Wed noon: All requests for other makeup/conflict

More information

Mutation. COS 326 David Walker Princeton University

Mutation. COS 326 David Walker Princeton University Mutation COS 326 David Walker Princeton University Mutation? 2 Thus far We have considered the (almost) purely functional subset of Ocaml. We ve had a few side effects: printing & raising exceptions. Two

More information

Modular Programming. Prof. Clarkson Fall Today s music: "Giorgio By Moroder" by Daft Punk

Modular Programming. Prof. Clarkson Fall Today s music: Giorgio By Moroder by Daft Punk Modular Programming Prof. Clarkson Fall 2017 Today s music: "Giorgio By Moroder" by Daft Punk Moog modular synthesizer Based in Trumansburg, NY, 1953-1971 Game changing! picked up by the Beatles, the Rolling

More information

One-Slide Summary. Lecture Outine. Automatic Memory Management #1. Why Automatic Memory Management? Garbage Collection.

One-Slide Summary. Lecture Outine. Automatic Memory Management #1. Why Automatic Memory Management? Garbage Collection. Automatic Memory Management #1 One-Slide Summary An automatic memory management system deallocates objects when they are no longer used and reclaims their storage space. We must be conservative and only

More information

CSE341 Spring 2016, Midterm Examination April 29, 2016

CSE341 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 information

Talen en Compilers. Jurriaan Hage , period 2. November 13, Department of Information and Computing Sciences Utrecht University

Talen en Compilers. Jurriaan Hage , period 2. November 13, Department of Information and Computing Sciences Utrecht University Talen en Compilers 2017-2018, period 2 Jurriaan Hage Department of Information and Computing Sciences Utrecht University November 13, 2017 1. Introduction 1-1 This lecture Introduction Course overview

More information

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120) Programming Languages and Techniques (CIS120) Lecture 27 March 28, 2018 Java Generics Collections and Equality Chapters 25 & 26 Announcements HW7: Chat Server Available on Codio / Instructions on the web

More information

CS 101 Spring 2006 Final Exam Name: ID:

CS 101 Spring 2006 Final Exam Name:  ID: This exam is open text book but closed-notes, closed-calculator, closed-neighbor, etc. Unlike the midterm exams, you have a full 3 hours to work on this exam. Please sign the honor pledge here: Page 1

More information

Programming Languages and Techniques (CIS120e)

Programming Languages and Techniques (CIS120e) Programming Languages and Techniques (CIS120e) Lecture 6 Sep 15, 2010 Binary Search Trees Announcements Homework 2 is on the web pages. On- Jme due date: Wednesday 22 Sept. at 11:59:59pm Get started early,

More information

Introduction to Graphs

Introduction to Graphs Introduction to Graphs CSE21 Winter 2017, Day 10 (B00), Day 6-7 (A00) February 1, 2017 http://vlsicad.ucsd.edu/courses/cse21-w17 What is a graph? A (directed) graph G is A nonempty set of vertices V, also

More information

CMSC 330, Fall 2013, Practice Problems 3

CMSC 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 information

Queues. October 20, 2017 Hassan Khosravi / Geoffrey Tien 1

Queues. October 20, 2017 Hassan Khosravi / Geoffrey Tien 1 Queues October 20, 2017 Hassan Khosravi / Geoffrey Tien 1 Queue ADT Queue ADT should support at least the first two operations: enqueue insert an item to the back of the queue dequeue remove an item from

More information

lecture29: Shortest Path Algorithms

lecture29: Shortest Path Algorithms lecture29: Shortest Path Algorithms Largely based on slides by Cinda Heeren CS 225 UIUC 30th July, 2013 Outline 1 Announcements 2 3 4 Announcements lab graphs due Thursday, 8/1 final exam this Friday (8/2),

More information

Announcements. Scope, Function Calls and Storage Management. Block Structured Languages. Topics. Examples. Simplified Machine Model.

Announcements. Scope, Function Calls and Storage Management. Block Structured Languages. Topics. Examples. Simplified Machine Model. CS 242 2007 Scope, Function Calls and Storage Management John Mitchell Announcements Homework Returned in class Wed; afterwards in 4 th Floor Gates Cabinet SCPD students: attach routing slip, will be returned

More information

Lecture 10 Notes Linked Lists

Lecture 10 Notes Linked Lists Lecture 10 Notes Linked Lists 15-122: Principles of Imperative Computation (Summer 1 2015) Frank Pfenning, Rob Simmons, André Platzer 1 Introduction In this lecture we discuss the use of linked lists to

More information

CSE 332 Autumn 2013: Midterm Exam (closed book, closed notes, no calculators)

CSE 332 Autumn 2013: Midterm Exam (closed book, closed notes, no calculators) Name: Email address: Quiz Section: CSE 332 Autumn 2013: Midterm Exam (closed book, closed notes, no calculators) Instructions: Read the directions for each question carefully before answering. We will

More information

CS 314 Principles of Programming Languages. Lecture 16

CS 314 Principles of Programming Languages. Lecture 16 CS 314 Principles of Programming Languages Lecture 16 Zheng Zhang Department of Computer Science Rutgers University Friday 28 th October, 2016 Zheng Zhang 1 CS@Rutgers University Class Information Reminder:

More information

Sample Exam; Solutions

Sample Exam; Solutions Sample Exam; Solutions Michael P. Fourman February 2, 2010 1 Introduction This document contains solutions to the sample questions given in Lecture Note 10 Short Question 5 marks Give the responses of

More information

Reminder of the last lecture. Aliasing Issues: Call by reference, Pointer programs. Introducing Aliasing Issues. Home Work from previous lecture

Reminder of the last lecture. Aliasing Issues: Call by reference, Pointer programs. Introducing Aliasing Issues. Home Work from previous lecture Reminder of the last lecture Aliasing Issues: Call by reference, Pointer programs Claude Marché Cours MPRI 2-36-1 Preuve de Programme 18 janvier 2017 Additional features of the specification language Abstract

More information

UNIVERSITY REGULATIONS

UNIVERSITY REGULATIONS CPSC 221: Algorithms and Data Structures Midterm Exam, 2013 February 15 Name: Student ID: Signature: Section (circle one): MWF(201) TTh(202) You have 60 minutes to solve the 5 problems on this exam. A

More information

Some Advanced ML Features

Some 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 information

CMPT 125: Practice Midterm Answer Key

CMPT 125: Practice Midterm Answer Key CMPT 125, Spring 2017, Surrey Practice Midterm Answer Key Page 1 of 6 CMPT 125: Practice Midterm Answer Key Linked Lists Suppose you have a singly-linked list whose nodes are defined like this: struct

More information

Lists. Michael P. Fourman. February 2, 2010

Lists. Michael P. Fourman. February 2, 2010 Lists Michael P. Fourman February 2, 2010 1 Introduction The list is a fundamental datatype in most functional languages. ML is no exception; list is a built-in ML type constructor. However, to introduce

More information

Queues 4/11/18. Many of these slides based on ones by Cynthia Lee

Queues 4/11/18. Many of these slides based on ones by Cynthia Lee Queues 4/11/18 Many of these slides based on ones by Cynthia Lee Administrivia HW 4 due tomorrow night (list implementations) Reading: For Friday: Beginning of Chapter 7 (pp. 198-211) For Monday: Rest

More information

Lecture 23: Priority Queues, Part 2 10:00 AM, Mar 19, 2018

Lecture 23: Priority Queues, Part 2 10:00 AM, Mar 19, 2018 CS18 Integrated Introduction to Computer Science Fisler, Nelson Lecture 23: Priority Queues, Part 2 10:00 AM, Mar 19, 2018 Contents 1 Heap Implementation 1 1.1 Insert............................................

More information

Introduction Data Structures

Introduction Data Structures Introduction Data Structures This course Lecturer: Nick Smallbone (me) nicsma@chalmers.se, room 5469 Assistant: Alexander Sjösten sjosten@chalmers.se Lectures usually twice a week: Wednesdays 13-15, room

More information

Implementing an abstract datatype. Linked lists and queues

Implementing an abstract datatype. Linked lists and queues Computer Programming Implementing an abstract datatype. Linked lists and queues Marius Minea marius@cs.upt.ro 19 December 2016 Review: compilation basics Briefly: Compiler translates source code to executable

More information

Lecture 10 Notes Linked Lists

Lecture 10 Notes Linked Lists Lecture 10 Notes Linked Lists 15-122: Principles of Imperative Computation (Spring 2016) Frank Pfenning, Rob Simmons, André Platzer 1 Introduction In this lecture we discuss the use of linked lists to

More information

Prelim 2, CS2110. SOLUTION

Prelim 2, CS2110. SOLUTION Prelim 2, CS2110. SOLUTION 7:30 PM, 25 April 2017 1. Name (1 point) Write your name and NetID at the top of every page of this exam. 2. Short Answer (26 points.) (a) Asymptotic complexity. 8 points. Be

More information

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120) Programming Languages and Techniques () Lecture 1 Welcome Introduction to Program Design Introductions Dr. Mitch Marcus Levine 503 http://www.cis.upenn.edu/~mitch/ mitch@cis.upenn.edu Dr. Stephanie Weirich*

More information

! Tree: set of nodes and directed edges. ! Parent: source node of directed edge. ! Child: terminal node of directed edge

! Tree: set of nodes and directed edges. ! Parent: source node of directed edge. ! Child: terminal node of directed edge Trees & Heaps Week 12 Gaddis: 20 Weiss: 21.1-3 CS 5301 Fall 2018 Jill Seaman!1 Tree: non-recursive definition! Tree: set of nodes and directed edges - root: one node is distinguished as the root - Every

More information

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120) Programming Languages and Techniques (CIS120) Lecture 3 Jan 18, 2012 Lists and Recursion Announcements Homework 1: OCaml Finger Exercises Due: Monday, Jan. 23 rd at 11:59:59pm (midnight) Please read Chapter

More information

CSE 373: Data Structures and Algorithms

CSE 373: Data Structures and Algorithms CSE 373: Data Structures and Algorithms Lecture 6: Dijkstra s Algorithm (Graphs) Instructor: Lilian de Greef Quarter: Summer 207 Today Announcements Graph Traversals Continued Remarks on DFS & BFS Shortest

More information