CS 135 Fall 2018 Final Exam Review. CS 135 Fall 2018 Final Exam Review 1

Similar documents
Module 8: Local and functional abstraction

CS 135 Winter 2018 Tutorial 7: Accumulative Recursion and Binary Trees. CS 135 Winter 2018 Tutorial 7: Accumulative Recursion and Binary Trees 1

CS 135 Winter 2018 Tutorial 8: Mutual recursion, nested lists, and local. CS 135 Winter 2018 Tutorial 8: Mutual recursion, nested lists, and local 1

CS115 - Module 10 - General Trees

Local definitions and lexical scope

Local definitions and lexical scope. Local definitions. Motivating local definitions. s(s a)(s b)(s c), where s = (a + b + c)/2.

Generative and accumulative recursion. What is generative recursion? Example revisited: GCD. Readings: Sections 25, 26, 27, 30, 31

Functional abstraction. What is abstraction? Eating apples. Readings: HtDP, sections Language level: Intermediate Student With Lambda

Functional abstraction

Working with recursion. From definition to template. Readings: HtDP, sections 11, 12, 13 (Intermezzo 2).

Working with recursion

CS115 - Module 9 - filter, map, and friends

Module 5: Lists. Readings: HtDP, Sections 9, 10.

Module 10: General trees

CS2500 Exam 2 Fall 2011

Trees. Binary arithmetic expressions. Visualizing binary arithmetic expressions. ((2 6) + (5 2))/(5 3) can be defined in terms of two smaller

Lists. Readings: HtDP, sections 9 and 10. Avoid 10.3 (uses draw.ss). CS 135 Winter : Lists 1

Module 9: Trees. If you have not already, make sure you. Read How to Design Programs Sections 14, 15, CS 115 Module 9: Trees

Module 9: Binary trees

Graphs. Readings: Section 28. CS 135 Fall : Graphs 1

Types of recursion. Structural vs. general recursion. Pure structural recursion. Readings: none. In this module: learn to use accumulative recursion

Graphs. Directed graphs. Readings: Section 28

Trees. Readings: HtDP, sections 14, 15, 16.

Trees. Example: Binary expression trees. Example: Evolution trees. Readings: HtDP, sections 14, 15, 16.

Module 8: Binary trees

How to Design Programs

Assignment: 7. Due: Language level: Allowed recursion:

CS115 - Module 4 - Compound data: structures

CS115 - Module 8 - Binary trees

Types of recursion. Readings: none. In this module: a glimpse of non-structural recursion. CS 135 Winter : Types of recursion 1

Use recursion to write a function that duplicates the following function: (def (f L) (map (lambda (x) (+ (sqr x) x)) L))

ormap, andmap, and filter

Local defini1ons. Func1on mul1ples- of

CSU211 Exam 2 Fall 2007

Module 10: Imperative Programming, Modularization, and The Future

Wellesley College CS251 Programming Languages Spring, 2000 FINAL EXAM REVIEW PROBLEM SOLUTIONS

Functional Programming. Pure Functional Programming

Computer Science 21b (Spring Term, 2015) Structure and Interpretation of Computer Programs. Lexical addressing

Module 3: New types of data

Homework 6: Higher-Order Procedures Due: 11:59 PM, Oct 16, 2018

CSC 533: Programming Languages. Spring 2015

A brief tour of history

Functional Programming. Pure Functional Languages

CMSC 330: Organization of Programming Languages. OCaml Higher Order Functions

(add1 3) 4 (check-expect (add1 3) 4)

Intro. Scheme Basics. scm> 5 5. scm>

Midterm CSE 21 Fall 2012

Functional Programming and Haskell

Style and Submission Guide

Case Study: Undefined Variables

Organization of Programming Languages CS3200/5200N. Lecture 11

Lists of Lists. CS 5010 Program Design Paradigms Bootcamp Lesson 5.3

The Design Recipe Fall 2017

LECTURE 16. Functional Programming

CS 342 Lecture 7 Syntax Abstraction By: Hridesh Rajan

CS 314 Principles of Programming Languages

Lambda the Ultimate. Corky Cartwright Vivek Sarkar Department of Computer Science Rice University

CSE413: Programming Languages and Implementation Racket structs Implementing languages with interpreters Implementing closures

Scheme as implemented by Racket

Homework 6: Higher-Order Procedures Due: 10:00 PM, Oct 17, 2017

Functional Programming. Pure Functional Languages

CS61A Notes 02b Fake Plastic Trees. 2. (cons ((1 a) (2 o)) (3 g)) 3. (list ((1 a) (2 o)) (3 g)) 4. (append ((1 a) (2 o)) (3 g))

A Third Look At ML. Chapter Nine Modern Programming Languages, 2nd ed. 1

Lists of Lists. CS 5010 Program Design Paradigms Bootcamp Lesson 6.5

Module 4: Compound data: structures

CMSC 330: Organization of Programming Languages. OCaml Higher Order Functions

Processadors de Llenguatge II. Functional Paradigm. Pratt A.7 Robert Harper s SML tutorial (Sec II)

CS115 - Module 3 - Booleans, Conditionals, and Symbols

Introduction to Typed Racket. The plan: Racket Crash Course Typed Racket and PL Racket Differences with the text Some PL Racket Examples

CMSC 330: Organization of Programming Languages. OCaml Higher Order Functions

CS 314 Principles of Programming Languages

YOUR NAME PLEASE: *** SOLUTIONS ***

CSE341: Programming Languages Lecture 17 Implementing Languages Including Closures. Dan Grossman Autumn 2018

MoreIntro_annotated.v. MoreIntro_annotated.v. Printed by Zach Tatlock. Oct 04, 16 21:55 Page 1/10

CSE 341: Programming Languages

SCHEME 7. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. October 29, 2015

CS 1101 Exam 3 A-Term 2013

Lecture 4: Higher Order Functions

Fall 2018 Discussion 8: October 24, 2018 Solutions. 1 Introduction. 2 Primitives

CSE341 Spring 2016, Final Examination June 6, 2016

Mini-ML. CS 502 Lecture 2 8/28/08

Functions that return lists

(Provisional) Lecture 08: List recursion and recursive diagrams 10:00 AM, Sep 22, 2017

CS 242. Fundamentals. Reading: See last slide

From Templates to Folds

(add1 3) 4 (check-expect (add1 3) 4)

Introduction to Functional Programming in Haskell 1 / 56

Rewriting your function using map and foldr

Module 4: Compound data: structures

CONCEPTS OF PROGRAMMING LANGUAGES Solutions for Mid-Term Examination

The Design Recipe Fall 2018

Functional Programming and λ Calculus. Amey Karkare Dept of CSE, IIT Kanpur

CSE413 Midterm. Question Max Points Total 100

Principles of Programming Languages Topic: Functional Programming Professor L. Thorne McCarty Spring 2003

UNIVERSITY OF EDINBURGH COLLEGE OF SCIENCE AND ENGINEERING SCHOOL OF INFORMATICS INFR08013 INFORMATICS 1 - FUNCTIONAL PROGRAMMING

CS558 Programming Languages

Assignment 2. Advanced Functional Programming, 2017 (Avancerad funktionell programmering, 2017) due 4 December 2017, 23:59

CS 275 Name Final Exam Solutions December 16, 2016

Functional Programming. Big Picture. Design of Programming Languages

Introduction to Programming, Aug-Dec 2006

Transcription:

CS 135 Fall 2018 Final Exam Review CS 135 Fall 2018 Final Exam Review 1

Final Exam Information The final exam will be held on Saturday, December 15 th 9:00AM - 11:30AM in the PAC Check your exam seating assignment on Odyssey. CS 135 Fall 2018 Final Exam Review 2

ALFs - digits->num Without using explicit recursion, write a function called digits->num that consumes a non-empty list of digits and produces the number those digits form. Include a contract. Try to do it without reversing the list. (digits->num (5)) 5 (digits->num (1 7 4 9 4)) 17494 CS 135 Fall 2018 Final Exam Review 3

ALFs - count Without using explicit recursion, write a function called count that consumes 2 arguments, of which the second one is a list. It produces the number of times the first argument occurs in the list. (count cat (3 "cat" 11)) 0 (count cat ("cat" cat 4 cat 6)) 2 CS 135 Fall 2018 Final Exam Review 4

ALFs - count-chars Without using explicit recursion, write a function called count-chars that consumes a Char and a (listof Any). It should count the number of occurrences of the character in all the strings in the list. You may use count that you wrote in the previous question. (count-chars #\a (a "Aardvark" #\a ("abba" 12) "hagrid!?")) 3 CS 135 Fall 2018 Final Exam Review 5

Nested List - count-chars/nested Write a function called count-chars/nested that consumes a Char and a Nest-List-Any. It should count the number of occurrences of the character in all the strings in the nested list. You can use explicit recursion. You may also use count that you wrote previously. (count-chars/nested #\a (a "Aardvark" (#\a "hag" "ah") "at" ("doggo"))) 5 Recall the data definition for a nested list: ;; A (Nest-List-Any) is one of: ;; * empty ;; * (cons Any Nest-List-Any) ;; * (cons Nest-List-Any Nest-List-Any) CS 135 Fall 2018 Final Exam Review 6

Graphs - delete-node Write a function delete-node that consumes a Node v and a Graph g and produces the graph with v removed. If v does not exist in g, the same graph is produced. (define g1 ((A (B C F)) (B ()) (C (A B)) (D (A C F)) (E (A B C)) (F (A))) (define g1 ((A (B C F)) (B ()) (C (A B)) (D (A C F)) (E (A B C)) (F (A))) (delete-node C g1) ((A (B F)) (B ()) (D (A F)) (E (A B)) (F (A))) CS 135 Fall 2018 Final Exam Review 7

Stepping Lambda: Example Given that the following definition has been processed in the Intermediate Student with Lambda language: (define fun (lambda (x y) (lambda (y z) (lambda (z) (+ x y z))))) Produce a step-by-step evaluation of the following program: (((fun 1 2) 3 4) 5) CS 135 Fall 2018 Final Exam Review 8

Stepping Lambda: Example ((((lambda (x y) (lambda (y z) (lambda (z) (+ x y z)))) 1 2) 3 4) 5) CS 135 Fall 2018 Final Exam Review 9

Stepping Lambda: Example ((((lambda (x y) (lambda (y z) (lambda (z) (+ x y z)))) 1 2) 3 4) 5) (((lambda (y z) (lambda (z) (+ 1 y z))) 3 4) 5) CS 135 Fall 2018 Final Exam Review 10

Stepping Lambda: Example ((((lambda (x y) (lambda (y z) (lambda (z) (+ x y z)))) 1 2) 3 4) 5) (((lambda (y z) (lambda (z) (+ 1 y z))) 3 4) 5) ((lambda (z) (+ 1 3 z)) 5) CS 135 Fall 2018 Final Exam Review 11

Stepping Lambda: Example ((((lambda (x y) (lambda (y z) (lambda (z) (+ x y z)))) 1 2) 3 4) 5) (((lambda (y z) (lambda (z) (+ 1 y z))) 3 4) 5) ((lambda (z) (+ 1 3 z)) 5) (+ 1 3 5) CS 135 Fall 2018 Final Exam Review 12

Stepping Lambda: Example ((((lambda (x y) (lambda (y z) (lambda (z) (+ x y z)))) 1 2) 3 4) 5) (((lambda (y z) (lambda (z) (+ 1 y z))) 3 4) 5) ((lambda (z) (+ 1 3 z)) 5) (+ 1 3 5) 9 CS 135 Fall 2018 Final Exam Review 13

BTs ;; An Integer Binary Tree (IntBT) is one of: ;; * empty ;; * an IntNode (define-struct int-node (key left right)) ;; An IntNode is a (make-int-node Int IntBT IntBT) Note this is different from a BST! CS 135 Fall 2018 Final Exam Review 14

BTs - min-key Write a function, min-key that consumes a non-empty IntBT and produces the minimum key in that tree. 3 (define b1 (make-int-node 3 900 (make-int-node 900 empty (make-int-node 200 empty empty)) (make-int-node 6 (make-int-node 8 empty empty) (make-int-node 10 (make-int-node 2 empty empty) empty)))) (min-key b1) 2 200 6 8 10 2 CS 135 Fall 2018 Final Exam Review 15

Which of the following was one of Grace Hopper s achievements? A) Proof that the Halting Problem is undecidable B) Design for the Analytical Engine C) Creation of the first compiler D) Axiomatization of geometry E) Development of FORTRAN CS 135 Fall 2018 Final Exam Review 16

Mutual recursion - Extended-Factor-Tree For this example, we are going to build an extended factor tree for a positive natural number. The extended factor tree of a number n has the number itself as the root node and all of its factors between 2 and n-1, in the extended factor tree form. Write build-tree that consumes a natural number n and produces the extended factor tree form of n. ;; An extended-factor-tree is a (list Nat (listof extended-factor-tree)) 12 (define sample-tree (list 12 (list (list 2 empty) (list 3 empty) (list 4 (list (list 2 empty))) (list 6 (list (list 2 empty) (list 3 empty)))))) 2 3 4 2 6 2 3 CS 135 Fall 2018 Final Exam Review 17

Who developed the Lisp programming language? A) John von Neumann B) Alonzo Church C) Kurt Gödel D) Grace Hopper E) John McCarthy CS 135 Fall 2018 Final Exam Review 18

Generative Recursion: group-equal-elems Write a function group-equal-elems that consumes an arbitrary list lst and rearranges the elements in lst so that identical elements are all grouped together. The rearranged elements should appear in the the same relative order as the first unique occurrence of each value in the consumed lst when read from left to right. (group-equal-elems (list 3 4 6 0 6 4 5 3 5 6 6 3 0 0 3 6 4 5 6 5)) (list 3 3 3 3 4 4 4 6 6 6 6 6 6 0 0 0 5 5 5 5) CS 135 Fall 2018 Final Exam Review 19

Generative Recursion: group-equal-elems ;; group-equal-elems: (listof X) (listof X) (define (group-equal-elems lst) (cond [(empty? lst) empty] [else (local [(define matches (filter (lambda (x) (equal? x (first lst))) lst)) (define others (filter (lambda (x) (not (equal? x (first lst)))) lst))] (append matches (group-equal-elems others)))])) CS 135 Fall 2018 Final Exam Review 20

What question do you want next? A) Abstract List Function Problems B) Graph Problems C) Tree Problems D) Mutual Recursion Problems E) Other Problems CS 135 Fall 2018 Final Exam Review 21

Generative recursion - count-uniq Write a function count-uniq that consumes a list and produces the number of unique elements in it. Do not use an accumulator. (count-uniq (a a 1 b 1 2)) 4 (count-uniq (a b c)) 3 (count-uniq ()) 0 CS 135 Fall 2018 Final Exam Review 22

ALFs: take-last Write a function take-last that consumes a list and a natural number n, and produces a list containing only the last n elements of the list, or the entire list if it contains fewer than n elements. (take-last empty 1) empty (take-last (b a s e) 2) (s e) (take-last (list 1 1 2) 4) (list 1 1 2) CS 135 Fall 2018 Final Exam Review 23

ALFs: take-last ;; take-last: (listof X) Nat (listof X) (define (take-last lst n) (foldr (lambda (itm rr) (cond [(= n (length rr)) rr] [else (cons itm rr)])) empty lst)) CS 135 Fall 2018 Final Exam Review 24

ALFs: take-last The previous solution calls length over and over again, which makes it very inefficient. Here are some additional approaches to the problem. As an exercise, you should try implementing them in racket: Use foldr to go over the list starting from the back. We keep extra info in our recursive result to tell us how long the current list is. This way, we don t need to compute length over and over again. Reverse the list, take the first n elements, then reverse our answer. We only call reverse twice in this solution. Zip each element with an index (0 referring to the last element), then we cut the list from the front until the index is smaller or equal to n. In the end, remove the index with a map. CS 135 Fall 2018 Final Exam Review 25

Length of a Longest Path Recall from lectures that a sequence of nodes v 1, v 2,..., v k is a path of length k 1 if (v 1, v 2 ), (v 2, v 3 ),..., (v k 1, v k ) are all edges. Directed graphs without cycles are called directed acyclic graphs (DAGs). Write a function max-path-length that consumes a DAG dag and produces the length of a longest path in dag. If dag has no paths, produce 0. CS 135 Fall 2018 Final Exam Review 26

;; max-path-length/list: (listof Node) Graph Nat (define (max-path-length/list l-nds dag) (cond [(empty? l-nds) 0] [else (local [(define nbrs (neighbours (first l-nds) dag))] (cond [(empty? nbrs) (max-path-length/list (rest l-nds) dag)] [else (max (add1 (max-path-length/list nbrs dag)) (max-path-length/list (rest l-nds) dag))]))])) ;; max-path-length: Graph Nat (define (max-path-length dag) (max-path-length/list (map first dag) dag)) CS 135 Fall 2018 Final Exam Review 27

BSTs - keys-in-range Write a function, keys-in-range that consumes 2 numbers, lo and hi, and a BST. It produces a list of all the keys in the BST that are between lo and hi inclusive. You should not try to convert the entire tree into a list first. garbage 3 (keys-in-range 2 7 empty) empty (keys-in-range 2 7 b1) (2 3 4 6) 1 6 (keys-in-range 5 2 b1) empty 2 4 10 8 CS 135 Fall 2018 Final Exam Review 28

Stepping Local When renaming local definitions append 0 if possible, or else 1, 2, etc. Do not recopy any line that is already in simplest form. (define x 5) (define y 10) (define (my-fn a b) (local [(define x (+ a b)) (define y (+ x (local [(define x 20)] x)))] (list x y))) (my-fn x y) CS 135 Fall 2018 Final Exam Review 29

ALFs - build-x Write build-x that consumes an odd natural number n and produces a table as a (listof (listof Nat)) that contains 1s on the 2 diagonals and 0s everywhere else. Do not use explicit recursion. (ones-on-diagonal 1) (list (list 1)) (ones-on-diagonal 3) (list (list 1 0 1) (list 0 1 0) (list 1 0 1))) CS 135 Fall 2018 Final Exam Review 30

Mutual recursion - Tournament For this example, we are going to use trees to model the results of a tournament. (define-struct tournament (winner round)) ;; A Tournament is a (make-tournament Str Round) ;; requires: winner is the name of the player with the best time ;; in the topmost round ;; A Round is a (listof Player) (define-struct player (name time last-round)) ;; A Player is a (make-player Str Num Round) ;; requires: time > 0 ;; name is the player with the best time in the topmost ;; round of last-round if it is non-empty CS 135 Fall 2018 Final Exam Review 31

Jesse Ben 12.3 Bill 16.9 Jesse 12.1 Anne 13.1 Ben 11 Bill 15.1 Yao 15.8 Jason 18.3 Jesse 19.9 Jimmy 9999 (define cs135-cup (make-tournament "Jesse" (list (make-player "Ben" 12.3 (list (make-player "Anne" 13.1 empty) (make-player "Ben" 11 empty))) (make-player "Bill" 16.9 (list (make-player "Bill" 15.1 empty) (make-player "Yao" 15.8 empty) (make-player "Jason" 18.3 empty))) (make-player "Jesse" 12.1 (list (make-player "Jesse" 19.9 empty) (make-player "Jimmy" 9999 empty)))))) CS 135 Fall 2018 Final Exam Review 32

Mutual recursion - Templates Write a template function for each of Tournament, Round and Player. (define-struct tournament (winner round)) ;; A Tournament is a (make-tournament Str Round) ;; requires: winner is the name of the player with the best time ;; in the topmost round ;; A Round is a (listof Player) (define-struct player (name time last-round)) ;; A Player is a (make-player Str Num Round) ;; requires: time > 0 ;; name is the player with the best time in the topmost ;; round of last-round if it is non-empty CS 135 Fall 2018 Final Exam Review 33

Mutual recursion - new-record Here s a structure we will use to store information about a record: (define-struct record (name time)) ;; A Record is a (make-record Str Num) ;; requires: time > 0 A player makes a new record if they have a faster time than the current record. Using your templates, write a function new-record that consumes a current record and a Tournament. It should produce a Record structure with the name and time of the player in the tournament who has the new record. If no one in the tournament had a faster time than the current record, produce false. You may assume that all times in the tournament are unique. (new-record (make-record "Bill" 11.2) cs135-cup) (make-record "Ben" 11) (new-record (make-record "Karen" 3.1) cs135-cup) false CS 135 Fall 2018 Final Exam Review 34

Stepping Lambda Recall that the rule for evaluating anonymous lambda expression is ((lambda (x1... xn) exp) v1... vn) exp where exp is exp with all occurrences of x1 replaced by v1, all occurrences of x2 replaced by v2, and so on. CS 135 Fall 2018 Final Exam Review 35

Stepping Local An expression of the form (local [(define x1 exp1)... (define xn expn)] bodyexp) is handled as follows. x1 is replaced with a fresh identifier (call it x1 new) everywhere it s used in the local expression. The same thing is done with x2 through xn. The definitions (define x1 new exp1)... (define xn new expn) are then lifted out (all at once) to the top level of the program, preserving their order. CS 135 Fall 2018 Final Exam Review 36

Stepping Local When all the rewritten definitions have been lifted out, what remains looks like (local [] bodyexp ), where bodyexp is the rewritten version of bodyexp. This is just replaced with bodyexp. All of this is a single step. (local [(define x1 exp1)... (define xn expn)] bodyexp) (define x1 0 exp1 )... (define xn 0 expn ) bodyexp CS 135 Fall 2018 Final Exam Review 37

Accumulative and Generative Recursion Accumulative Recursion All parameters in the recursive call are either unchanged or one step closer to the base case (as with pure structural recursion), plus one or more parameters that accumulate partial answers Accumulators are usually produced in the base case(s) directly, or manipulated before being produced In the recursive call, it does not matter how complicated the code is to update an accumulator, as long as it adds on to the partial answer CS 135 Fall 2018 Final Exam Review 38

Generative Recursion A parameter is considered generative if: It is changed in the recursive call without a clear base case It doesn t get one step closer to the base case in the recursive call, according to the data definition It is not an accumulator, and thus isn t produced in the base case Examples of generative recursive calls: (collatz (+ 1 ( 3 n))) (foo (not bool)) (foo (rest (rest (rest lst)))) (foo (remove elem lst)) If in any recursive call of a function, there is at least one parameter that satisfies the conditions above, regardless of how the other parameters are changing, the function uses generative recursion. CS 135 Fall 2018 Final Exam Review 39