CS 115 – SOS Review Package – Midterm

Integer 115 e and integer str str b f and

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: (rest (rest alist)) -> (cons ‘c (cons ‘d empty)) (rest (rest (rest (rest list1)))) -> empty List Template: (define (mylist alist) (cond [(empty? alist) ...] [(cons? alist) ...])) RECURSION The continuous application of a variable that is defined in terms of a past. Recursive Process: 1. Create a base case that ends the program when true 2. Test your base case with given variables 3. Change the variable and recurse the formula Example 8: Create a function, countdown, that takes in an integer, int, and creates a descending list of numbers, starting with the int, and ending with zero. (define (countdown int) (cond [(zero? int)(cons 0 empty)] [else (cons int (countdown (- int 1)))])) Example 9: Using structural recursion, write a Scheme function called reciprocate that consumes a list and produces a list of numbers and symbols by reciprocating each entry in the list (the reciprocal of x is 1=x). If the input list entry is 0, then its reciprocal is the symbol ’Infinity. Likewise, if the list entry is ’Infinity, then its reciprocal is 0. Finally, if the list entry is not a number or the symbol ’Infinity, then it should be skipped. For example, (reciprocate (cons 2 (cons 0 (cons ’Infinity (cons "boo" empty))))) produces (cons 0.5 (cons ’Infinity (cons 0 empty))). ;; reciprocate: list  ­> list (define (reciprocate alist) (cond [(empty? alist)empty] [(equal? 'Infinity (first alist)))(cons 0 (reciprocate (rest alist)))] [(not (number? (first alist)))(reciprocate (rest alist))] [(zero? (first alist))(cons 'infinity (reciprocate (rest alist)))] [else (cons (/ 1 (first alist))(reciprocate (rest alist)))])) (reciprocate empty) -> empty (reciprocate (cons 2 empty)) -> (cons 0.5 empty) (reciprocate (cons 0 (cons 5 (cons 'infinity (cons 2 empty))))) -> (cons ‘infinity (cons 0.2 (cons 0 (cons 0.5 empty)))) (reciprocate (cons 'infinity (cons 0 (cons 5 (cons "computer" empty))))) -> (cons 0 (cons ‘infinity (cons 0.2 empty))) Practice Questions 1. Assume the following constants have been defined: (define str “1”) (define a 10) (define b -1) (define mylist (cons ‘x (cons ‘y (cons ‘z empty)))) a. (* (+ 4 1) (- 10 5)) b. (/ ((- 5 1)) (+ 1 1)) c. (or (not (> a b)) (and (> a 3) (> b 10))) d. (integer? “115”) e. (and (integer? str) (> str b)) f. (and (< str b) (integer? str)) g. (first (rest (rest (mylist))) h. (rest (first (mylist)) 2. Give a full trace starting with each of the following Scheme expressions: a. (define z 5) (define (f x y) (* (- x y) 2)) (f a (+ a 1)) b. (cond [(> (posn-x (make-posn 3 5)) 4 (+ 2 2)] [(> 5 4) “a2”] [else ‘a3]) 3. Consider the following contract for a function my-fun: my-fun: string[len>0] (union num symbol) -> (listof boolean) Is the contract satisfied...
View Full Document

This note was uploaded on 01/19/2014 for the course CS 115 taught by Professor Troyvasiga during the Fall '13 term at University of Waterloo, Waterloo.

Ask a homework question - tutors are online