;; multiples0: natNum num --> list-of-num ;; creates a list with the first n multiples of x (define (multiples0 n x) (cond [(zero? n) empty] [(< 0 n) (cons (* x n) (multiples0 (sub1 n) x))])) "multiples0 test case:" (equal? empty (multiples0 0 2)) (equal? (list 10 8 6 4 2)(multiples0 5 2)) ;; multiples: natNum num --> list-of-num ;; creates a list with the first n multiples of x (define (multiples n x) (local [(define (helper n) (cond [(zero? n) empty] [(< 0 n) (cons (* x n) (helper (sub1 n)))]))] (helper n))) "multiples test case:" (equal? empty (multiples 0 2)) (equal? (list 10 8 6 4 2)(multiples 5 2)) ;; op: function any any --> any ;; evaluates the given function with the remaining two input parameters. ;; The function must have the following abstract contract: ;; f: any any --> any (define (op f x y) (f x y)) "op test cases:" (= 5 (op + 3 2)) (= 1.5 (op / 3 2)) (boolean=? true (op > 3 2)) (string=? "Comp210 just gets more nifty each day!" (op string-append "Comp210 just gets " "more nifty each day!")) ;; isBetterThan: function num num --> string ;; Uses the function to determine if the x is "better than" y. ;; Returns a string with its answer. ;; The supplied function must adhere to this abstract contract: ;; comp: num num --> boolean (define (isBetterThan comp x y) (cond [(comp x y) (string-append (number->string x) " is better than " (number->string y))] [else (string-append (number->string y) " is better than " (number->string x))])) "isBetterThan tests:" (isBetterThan > 4 5) (isBetterThan < 4 5) ;; randComp: num num --> boolean ;; randomly compares x and y (define (randComp x y) (< (* x (random 100)) (* y (random 100)))) "randComp test cases:" (isBetterThan randComp 4 5) (isBetterThan randComp 4 5) (isBetterThan randComp 4 5) (isBetterThan randComp 4 5) (isBetterThan randComp 4 5) (isBetterThan randComp 4 5) ;; filterAbyB_bad: list-of-num list-of-sym --> list-of-sym ;; replaces any element in listB with '_ if the corresponding ;; value in lonA is negative. (define (filterAbyB_bad lonA listB) (cond [(empty? lonA) empty] [(empty? listB) empty] [(cons? lonA) (cond [(>= 0 (first lonA)) (cons '_ (filterAbyB_bad (rest lonA) (rest listB)))] [else (cons (first listB) (filterAbyB_bad (rest lonA) (rest listB)))])])) "filterAbyB_bad test cases:" (equal? (list 'A 'B '_ 'D '_ '_ 'G) (filterAbyB_bad (list 1 4 -2 3 -1 -3 2) (list 'A 'B 'C 'D 'E 'F 'G))) ;; filterAbyB: list-of-num list-of-any (function: num --> (function: symbol --> symbol)) ;; --> list-of-num-and-any ;; replaces any element in listB according to the gate function, ;; which depends on the corresponding element in lonA. ;; The gateFn must have the following abstract contract: ;; gateFn: num --> function: symbol --> symbol (define (filterAbyB lonA listB gateFn) (cond [(empty? lonA) empty] [(empty? listB) empty] [(cons? lonA) (cons ((gateFn (first lonA)) (first listB)) (filterAbyB (rest lonA) (rest listB) gateFn))])) ;; stepFn: num --> function: sym --> sym ;; returns a no-op function if a > 0 ;; returns a function that returns '_ otherwise. (define (stepFn a) (local [(define (f1 s) '_) (define (f2 s) s)] (cond [(>= 0 a) f1] [else f2]))) "filterAbyB test case, stepFn:" (equal? (list 'A 'B '_ 'D '_ '_ 'G) (filterAbyB (list 1 4 -2 3 -1 -3 2) (list 'A 'B 'C 'D 'E 'F 'G) stepFn)) ;;signFn: num --> function: sym --> sym ;; returns a function that prepends a "+" to the symbol if a >0 ;; otherwise returns a function that prepends a "-" to the symbol. (define (signFn a) (local [(define (f1 s) (string->symbol (string-append "-" (symbol->string s)))) (define (f2 s) (string->symbol (string-append "+" (symbol->string s))))] (cond [(>= 0 a) f1] [else f2]))) "filterAbyB test case, signFn:" (equal? (list '+A '+B '-C '+D '-E '-F '+G) (filterAbyB (list 1 4 -2 3 -1 -3 2) (list 'A 'B 'C 'D 'E 'F 'G) signFn)) ;; opList: function non-empty-list-of-numbers --> number ;; recursively applies func to every element of a list and the ;; accumulated result for the list. ;; func must have the following contract: ;; func: num num --> num ;; and header: ;; (func first acc) ;; where first is the data in first and acc is the accumulated ;; result so far. (define (opList func nelon) (local [(define (helper lon acc) (cond [(empty? lon) acc] [(cons? lon) (helper (rest lon) (func (first lon) acc))]))] (cond [(cons? nelon) (helper (rest nelon) (first nelon))]))) "opList test cases:" (= 15 (opList + (list 1 2 3 4 5))) (= 120(opList * (list 1 2 3 4 5))) ;;sumSq: num num --> num ;;sums the acc with the square of x (define (sumSq x acc) (+ acc (* x x))) ;;getMin: num num --> num ;; the result is the smaller of acc and x. (define (getMin x acc) (cond [(< x acc) x] [else acc])) "more opList test cases:" (= 55 (opList sumSq (list 1 2 3 4 5))) (= 1 (opList getMin (list 5 3 6 1 4 2))) ;;orderedInsert: num lon --> lon ;; does an ordered insert of x into the presumed ordered lon. (define (orderedInsert x lon) (cond [(empty? lon) (cons x empty)] [(cons? lon) (cond [(< x (first lon)) (cons x (cons (first lon) (rest lon)))] [else (cons (first lon) (orderedInsert x (rest lon)))])])) ;; orderedInsert0: --> empty ;; The base case \ (define (orderedInsert0) empty) ;; opList2: function function list-of-any --> any ;; f0 is executed on the base case. ;; abstract contract for f0: ;; f0: --> any ;; f1 is executed on the inductive case, where the first input is (first a-list) ;; and the second input is (opList2 f0 f1 (rest a-list)) ;; abstract contract for f1: ;; f1: num lon --> any (define (opList2 f0 f1 a-list) (cond [(empty? a-list) (f0)] [(cons? a-list) (f1 (first a-list) (opList2 f0 f1 (rest a-list)))])) "opList2 test case, orderedInsert:" (equal? (list 1 2 3 4 5 6 7) (opList2 orderedInsert0 orderedInsert (list 4 2 7 3 5 6 1)))