cs 135 midterm 2

40
CS 135 Midterm 2

Upload: ivrit

Post on 23-Feb-2016

205 views

Category:

Documents


4 download

DESCRIPTION

CS 135 Midterm 2. Outline of The Session. Structures Lists Association Lists Recursions Types of Recursions. Structures. Structures. Allow you to store several values in one entity e.g. posn – predefined structure in Scheme (define- struct posn (x y)) The following are created: - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: CS 135 Midterm 2

CS 135 Midterm 2

Page 2: CS 135 Midterm 2

Outline of The Session

• Structures• Lists– Association Lists

• Recursions• Types of Recursions

Page 3: CS 135 Midterm 2

Structures

Page 4: CS 135 Midterm 2

Structures

• Allow you to store several values in one entity• e.g. posn – predefined structure in Scheme• (define-struct posn (x y))• The following are created:– Constructor: (make-posn x y)– Selectors: (posn-x) (posn-y)– Predicate: posn?

• (make-posn 3 2) is in its most simplified form

Page 5: CS 135 Midterm 2

Structures

• Example:• (define-struct chair (color material))• (define mychair (make-chair ‘blue ‘wood))• (chair-color mychair) => ‘blue• (chair-material mychair) => ‘wood• Predicate: chair?

Page 6: CS 135 Midterm 2

Structures: Design Recipe

• Define the structure• Write the data definition– What are the types of each field?

• Create a template for a function that consumes the structure

Page 7: CS 135 Midterm 2

Structures: Design Recipe

(define-struct student (first last id));; a Student = (make-student String String Number);; my-student-fn: Student -> Any(define (my-student-fn data) … (student-first data) … … (student-last data) … … (student-id data) …)

Page 8: CS 135 Midterm 2

Structures: Examples

Question 1:(define-struct student (first last id));; a Student = (make-student String String Number)

• Write a function “swap” that takes a Student as input, and outputs another Student with the first name and last name swapped.

Page 9: CS 135 Midterm 2

Structures: Examples

Question 2• Define a struct called “fivenum” that has five

fields, all of which are numbers.• Write a function “fivenum-avg” that takes a

fivenum as input, and output a number that is the average of the five fields that are in fivenum.

Page 10: CS 135 Midterm 2

Structures: ExamplesHTDP, Section 7, Question 2: Develop data and structure definition for a collection of 3D shapes. The collection includes:• Cubes, with relevant properties being the length of an edge• Prisms, which are rectangular solids and with relevant properties being

length, width, and height• Spheres, with relevant property being the radiusDevelop the function “volume”. The function consumes a 3D shape and produces the volume of the object. • The volume of a cube is the cube of the length of one its edges. • The volume of a prism is the product of its length, width, and height. • The Volume of a sphere is 4/3 * PI * r^3

Page 11: CS 135 Midterm 2

ListsQuestions so far?

Page 12: CS 135 Midterm 2

Lists

• Lists vs. Structs– Unbounded vs. fixed amount of data

• An ordered set of data elements, each containing a list to its successor

Page 13: CS 135 Midterm 2

Lists• List selector functions:– First– Cons– Rest

• Built-in predicates:– Empty?– Cons?– Member?

• Other list functions:– Length– Reverse

Page 14: CS 135 Midterm 2

Lists

;; A List is one of:;; * empty;; * (cons Any List)

• empty is also a list

When stepping through code, a list, like structures, cannot be simplified any further.

Page 15: CS 135 Midterm 2

Lists

• (define lst (cons 2 (cons 3 (cons 4 empty))))• Selectors:– (first lst) => 2– (rest lst) => (cons 3 (cons 4 empty))– (first (rest lst)) => 3

Page 16: CS 135 Midterm 2

Lists: Box-and-Pointer Diagrams

• Write out the code for these Box-and-Pointer diagrams

Page 17: CS 135 Midterm 2

Lists: Box-and-Pointer Diagrams

• Solutions• (cons (cons empty empty) (cons 3 empty))• (cons (cons 1 empty) (cons (cons 4 (cons 7

(cons 8 empty))) (cons (cons empty empty) empty)))

Page 18: CS 135 Midterm 2

Lists: Box-and-Pointer Diagrams

• Draw Box-and-Pointer diagrams:• (cons 1 (cons 2 (cons 3 empty)))• (cons (cons 1 (cons 2 empty)) (cons (cons 3

(cons 4 empty)) empty))• (cons (cons (cons 3 empty) (cons 4 (cons 6

empty))) (cons 7 (cons (cons 8 empty) empty)))

Page 19: CS 135 Midterm 2

Coding with Lists

;; my-list-fn: (listof Any) -> Any(define (my-lst-fn lst) (cond [(empty? lst) …] [else … (first lst) … (my-lst-fn (rest lst)) … ]))

This is for a general list function

Page 20: CS 135 Midterm 2

Coding with Lists

Question 1:Write a function “addone” that takes a (listof Num) as input, and output a (listof Num) with each element of list incremented by 1.

Page 21: CS 135 Midterm 2

Lists: Tracing (define (addone lon) (cond [(empty? lon) empty] [else (cons (add1 (first lon) ) (addone (rest lon)))]))

• (addone (cons 3 (cons 1 (cons 9 (cons 4 empty)))))• (cons 4 (addone (cons 1 (cons 9 (cons 4 empty)))))• (cons 4 (cons 2 (addone (cons 9 (cons 4 empty)))))• (cons 4 (cons 2 (cons 10 (addone (cons 4 empty)))))• (cons 2 (cons 4 (cons 6 (cons 5 (addone empty)))))• (cons 2 (cons 4 (cons 6 (cons 5 empty))))

Page 22: CS 135 Midterm 2

Coding with Lists

Question 2:Develop the function “string-append-n”, which consumes a list of strings and produces a single string resulting from appending all the strings together. Use the built-in function “string-append”: String String -> String, which appends two strings. If the list is empty, produce an empty string “”.

Page 23: CS 135 Midterm 2

List Abbreviations

• (cons 1 (cons 2 (cons 3 empty)))• (list 1 2 3)

• (cons ‘Apple (cons ‘Orange (cons ‘Fruit empty)))• ‘(Apple Orange Fruit)

Page 24: CS 135 Midterm 2

Association Lists

• List of pairs– Pair is a two-element list– First element is the key– Second element is the value

• E.g. Storing definitions

Page 25: CS 135 Midterm 2

Association Lists

(define (my-al-fn alst)(cond

[(empty? alst) ...][else … (first (first alst) ;; first key … (second (first alst)) ;; first value

… (my-al-fn (rest alst))]))

Page 26: CS 135 Midterm 2

Association Lists: Example

Question 1:Create the function “remove-entry” for an association list. The function “remove-entry” consumes an association list and a key, and produces the association list without the key/value pair for the given key. If the key is not in the association list, return the original association list.

Page 27: CS 135 Midterm 2

Recursion

Page 28: CS 135 Midterm 2

Recursion

• You have just used recursion on lists• A function that calls itself in order to compute

data of unknown size• Must have a base case

Page 29: CS 135 Midterm 2

Recursion

• Natural number recursion– Used to work with any natural number N– Base case is usually N = 0

Question:Write a function “fac” that takes a natural number as input, and outputs the factorial of that number.(fac 4) -> 4*3*2*1 -> 24

Page 30: CS 135 Midterm 2

Recursion

Question 1:Develop list-pick0 that takes a (listof Any) and a natural number as input, and produces the n-th element of the list (index starts at 0). (You can assume n < length of list)

(list-pick0 (list ‘hello ‘world) 0) -> ‘hello

Page 31: CS 135 Midterm 2

Types of Recursion

Page 32: CS 135 Midterm 2

Structural Recursion

• Each recursive call leaves all parameters unchanged or moves the recursion one step closer to a base case

• e.g. traversing a list until empty

Page 33: CS 135 Midterm 2

Accumulative Recursion

• Parameter(s) contain partial answers

(define (my-reverse lst0) ;; wrapper function (my-rev-helper lst0 empty))

(define (my-rev-helper lst acc) ;; helper function(cond [(empty? lst) acc] [else (my-rev-helper (rest lst) (cons (first lst) acc))]))

Page 34: CS 135 Midterm 2

Generative Recursion

• Parameters are freely calculated at each step

(define (euclid-gcd n m)(cond [(zero? m) n] [else (euclid-gcd m (remainder n m))])

Page 35: CS 135 Midterm 2

Practice Problems

Determine whether the following recursive functions uses structural recursion, accumulative recursion, or generative recursion.

Page 36: CS 135 Midterm 2

Type of Recr: Practice Problems

n is a natural number(define (fac n)

(cond [(zero? n) 1] [else (* n (fac (sub1 n)))]))

Page 37: CS 135 Midterm 2

Type of Recr: Practice Problems

(define (fac n)(fac-helper n 1))

(define (fac-helper n acc)(cond [(zero? n) acc]

[else (fac-helper (sub1 n) (* n acc))]))

Page 38: CS 135 Midterm 2

Type of Recr: Practice Problems

n is a natural number(define (collatz n)

(cond [(= n 1) empty][(even? n) (cons (/ n 2) (collatz (/ n 2)) [(odd? n) (cons (add1 (* n 3))

(collatz (add1 (* n 3))))]))

Page 39: CS 135 Midterm 2

Type of Recr: Practice Problems

Lon is a list of numbers(define (sum lon)

(cond [(empty? lon) 0] [else (+ (first lon) (sum (rest lon)))]))

Page 40: CS 135 Midterm 2

Any Final Questions?

Good luck on your [email protected]