;; ===================================================================================== ;; _____ __ __ ____ _ __ __ ____ ___ ;; / ____/ |/ / __ \/ / / / < / __ < / Spring 2019 Section 54 ;; / / / /|_/ / /_/ / / / /_____/ / / / / / ;; / /___/ / / / ____/ /_/ /_____/ / /_/ / / Assignment 05 ;; \____/_/ /_/_/ \____/ /_/\____/_/ ;; ;; S .d8888. .d88b. db db db d888888b d888888b .d88b. d8b db .d8888. ;; A 88' YP .8P Y8. 88 88 88 `~~88~~' `88' .8P Y8. 888o 88 88' YP ;; M `8bo. 88 88 88 88 88 88 88 88 88 88V8o 88 `8bo. ;; P `Y8b. 88 88 88 88 88 88 88 88 88 88 V8o88 `Y8b. ;; L db 8D `8b d8' 88booo. 88b d88 88 .88. `8b d8' 88 V888 db 8D ;; E `8888Y' `Y88P' Y88888P ~Y8888P' YP Y888888P `Y88P' VP V8P `8888Y' ;; ;; ===================================================================================== (load "asmt-helper.txt") (header "Professor Ellis" "Sample Solutions to Assignment Five") (problem "1") ;; ============================================================ ;; COUNT-BY-TYPE ;; ============================================================ ;; Outputs a count of the number of items in a list of a given ;; type. ;; ------------------------------------------------------------ ;; INPUTS: LST, a list. ;; PRED, a predicate function ;; OUTPUT: 0 if LST contains no elements of the specified type, ;; else the number of elements of that type in LST. ;; ============================================================ (define count-by-type (lambda (lst pred) (if (null? lst) 0 (if (pred (first lst)) (+ 1 (count-by-type (rest lst) pred)) (count-by-type (rest lst) pred))))) (tester '(count-by-type '(a b c d e) symbol?)) (tester '(count-by-type '(1 2 3 #f 4 5 6 #t) boolean?)) (tester '(count-by-type '(a #t b 87 () zz 9 plural z alpha) number?)) (problem "2") ;; ============================================================ ;; GET-ITEM-AT ;; ============================================================ ;; Outputs item number NUM in list LST, or #f if NUM is bigger ;; than the length of LST. ;; ------------------------------------------------------------ ;; INPUTS: NUM, a non-negative integer. ;; LST, a list. ;; OUTPUT: The entry in LST at position NUM, or #f if NUM is ;; bigger than the size of LST. ;; NOTE: Indices begin at 0 (zero). ;; ============================================================ (define get-item-at (lambda (num lst) (cond ((null? lst) #f) ((= num 0) (first lst)) (else (get-item-at (- num 1) (rest lst)))))) (tester '(get-item-at 0 '(a b c d e))) (tester '(get-item-at 12 '(a b c d e))) (tester '(get-item-at 21 '(s u p e r c a l i f r a g i l i s t i c e x p i a l i d o c i o u s))) (problem "3") ;; ============================================================ ;; PRINT-LST ;; ============================================================ ;; Uses side-effect printing to display the contents of LST. ;; Prints "\$\$\$" after the last entry. ;; ------------------------------------------------------------ ;; INPUT: LST, a list. ;; OUTPUT: None. ;; SIDE-EFFECT: Displays the contents of LST, separated by ;; dashes. Displays \$\$\$ after the last entry and ;; starts a new line. ;; ============================================================ (define print-lst (lambda (lst) (cond ((null? lst) (printf "\$\$\$\n")) (else (printf "~A - " (first lst)) (print-lst (rest lst)))))) (tester '(print-lst '(it was a dark and stormy night))) (tester '(print-lst '(it is dark. you are liable to be eaten by a grue.))) (tester '(print-lst '())) (problem "4") ;; ============================================================ ;; ADDY ;; ============================================================ ;; Takes two lists, LST1 and LST2, which have the same number ;; of numerical entries. Outputs a list which contains the ;; sums of the numbers at each index in LST1 and LST2. ;; ------------------------------------------------------------ ;; INPUTS: LST1, a list of numbers. ;; LST2, a list of numbers. ;; OUTPUT: A list containing the sums of the pairs of numbers ;; at each index of LST1 and LST2. ;; ============================================================ (define addy (lambda (lst1 lst2) (if (null? lst1) '() (cons (+ (first lst1) (first lst2)) (addy (rest lst1) (rest lst2)))))) (tester '(addy '(1 2 3 4 5) '(10 20 30 40 50))) (tester '(addy '(1 4 9 16 25 36 49) '(1 8 27 64 125 216 553))) (tester '(addy '(5 4 3 2 1 0) '(-5 -4 -3 -2 -1 0))) (problem "5") ;; ============================================================ ;; SWAPPY ;; ============================================================ ;; Takes a list as input, and outputs a new list with the two ;; items of each successive pair of items swapped. ;; ------------------------------------------------------------ ;; INPUT: LISTO, a list. ;; OUTPUT: A list containing the contents of LISTO, but with ;; the two items in each successive pair swapped ;; around. ;; NOTE: If LISTO contains an odd number of elements, its ;; last item remains unchanged in place. If LISTO has ;; 0 or 1 entries, the output is the same as the input. ;; ============================================================ (define swappy (lambda (lst) (cond ((< (length lst) 2) lst) (else (cons (first (rest lst)) (cons (first lst) (swappy (rest (rest lst))))))))) (tester '(swappy '(a b c d e f g h i))) (tester '(swappy '(c o m p u t e r s c i e n c e))) (tester '(swappy '())) (tester '(swappy '(1 4 9 16 25 36 49 64 81 100 121 144 169 196)))