;; ===================================================================================== ;; _____ __ __ ____ _ __ __ ____ ___ ;; / ____/ |/ / __ \/ / / / < / __ < / Spring 2019 Section 54 ;; / / / /|_/ / /_/ / / / /_____/ / / / / / ;; / /___/ / / / ____/ /_/ /_____/ / /_/ / / Assignment 06 ;; \____/_/ /_/_/ \____/ /_/\____/_/ ;; ;; 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 Six") (problem "1") ;; ============================================================ ;; EVERY-OTHER-ONE ;; ============================================================ ;; Outputs a list containing alternating elements in the input ;; list. ;; ------------------------------------------------------------ ;; INPUT: LST, a list. ;; OUTPUT: A list containing alternating elements from LST. ;; ============================================================ (define every-other-one (lambda (lst) (if (or (null? lst) (null? (rest lst))) lst (cons (first lst) (every-other-one (rest (rest lst))))))) (tester '(every-other-one '(a b c d e))) (tester '(every-other-one '())) (tester '(every-other-one '(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 "2") ;; ============================================================ ;; EVERY-OTHER-ONE-ACC ;; ============================================================ ;; Outputs a list containing alternating elements in the input ;; list. ;; ------------------------------------------------------------ ;; INPUTS: LST, a list. ;; ACC, a list. ;; OUTPUT: A list containing alternating elements from LST. ;; =========================================================== (define every-other-one-acc (lambda (lst acc) (cond ((null? lst) (reverse acc)) ((null? (rest lst)) (reverse (cons (first lst) acc))) (else (every-other-one-acc (rest (rest lst)) (cons (first lst) acc)))))) (tester '(every-other-one-acc '(a b c d e) '())) (tester '(every-other-one-acc '() '())) (tester '(every-other-one-acc '(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") ;; ============================================================ ;; REPEATER ;; ============================================================ ;; Outputs a list containing two copies of each element in LST, ;; in order. ;; ------------------------------------------------------------ ;; INPUT: LST, a list. ;; OUTPUT: A list containing each element of LST, twice, in ;; the same order. ;; ============================================================ (define repeater (lambda (lst) (if (null? lst) () (cons (first lst) (cons (first lst) (repeater (rest lst))))))) (tester '(repeater '(space the final frontier))) (tester '(repeater '())) (tester '(repeater '(make a wisdom saving throw))) (problem "4") ;; ============================================================ ;; REPEATER-ACC ;; ============================================================ ;; Outputs a list containing two copies of each element in LST, ;; in order. ;; ------------------------------------------------------------ ;; INPUTS: LST, a list. ;; ACC, a list. ;; OUTPUT: A list containing each element of LST, twice, in ;; the same order. ;; ======================================================== (define repeater-acc (lambda (lst acc) (if (null? lst) (reverse acc) (repeater-acc (rest lst) (cons (first lst) (cons (first lst) acc)))))) (tester '(repeater-acc '(space the final frontier) '())) (tester '(repeater-acc '() '())) (tester '(repeater-acc '(make a wisdom saving throw) '())) (problem "5 (A)") ;; ============================================================ ;; REM-DUPES ;; ============================================================ ;; Outputs a list containing LISTY with duplicate entries ;; removed. ;; ------------------------------------------------------------ ;; INPUT: LISTY, any list. ;; OUTPUT: A list that contains the same elements as ;; LISTY, but without any duplicates. ;; ============================================================ (define rem-dupes (lambda (listy) (if (null? listy) '() (if (member (first listy) (rest listy)) (rem-dupes (rest listy)) (cons (first listy) (rem-dupes (rest listy))))))) (tester '(rem-dupes '(a l b u s d u m b l e d o r e))) (tester '(rem-dupes '(d a e n e r y s t a r g a r y e n))) (tester '(rem-dupes '())) (tester '(rem-dupes '(m i l l e n n i u m f a l c o n))) (problem "5 (B)") ;; ============================================================ ;; REM-DUPES-ACC ;; ============================================================ ;; Outputs a list containing LISTY with duplicate entries ;; removed. ;; ------------------------------------------------------------ ;; INPUTS: LISTY, any list. ;; ACC, a list. ;; OUTPUT: A list that contains the same elements as ;; LISTY, but without any duplicates. ;; ============================================================ (define rem-dupes-acc (lambda (listy acc) (if (null? listy) (reverse acc) (if (member (first listy) acc) (rem-dupes-acc (rest listy) acc) (rem-dupes-acc (rest listy) (cons (first listy) acc)))))) (tester '(rem-dupes-acc '() '())) (tester '(rem-dupes-acc '(1 2 3 1 2 3 1 2 3 4 3 2 1) '())) (tester '(rem-dupes-acc '(ll a n f a i r p w ll g w y n g y ll g o g e r y ch w y r n d r o b w ll ll a n t y s i l i o g o g o g o ch) '())) (tester '(rem-dupes-acc '(R i n d f l e i s c h e t i k e t t i e r u n g s ue b e r w a c h u n g s a u f g a b e n ue b e r t r a g u n g s g e s e t z) '())) (problem "6") ;; ============================================================ ;; CONSEQ-SUMS ;; ============================================================ ;; Outputs a list containing the sums of consecutive pairs of ;; values in LISTY. ;; ------------------------------------------------------------ ;; INPUT: LISTY, a list of numbers. ;; OUTPUT: A list containing the sums of consecutive pairs of ;; values in LISTY. ;; ============================================================ (define conseq-sums (lambda (listy) (if (or (null? listy) (null? (rest listy))) () (cons (+ (first listy) (first (rest listy))) (conseq-sums (rest listy)))))) (tester '(conseq-sums '(10 20 30 40 50))) (tester '(conseq-sums '(1 8 27 64 125 216 553))) (tester '(conseq-sums '())) (tester '(conseq-sums '(1 4 9 16 25 36 49))) (tester '(conseq-sums '(3))) (problem "7") ;; ============================================================ ;; CONSEQ-SUMS-ACC ;; ============================================================ ;; Outputs a list containing the sums of consecutive pairs of ;; values in LISTY. ;; ------------------------------------------------------------ ;; INPUTS: LISTY, a list of numbers. ;; ACC, a list. ;; OUTPUT: A list containing the sums of consecutive pairs of ;; values in LISTY. ;; ============================================================ (define conseq-sums-acc (lambda (listy acc) (if (or (null? listy) (null? (rest listy))) (reverse acc) (conseq-sums-acc (rest listy) (cons (+ (first listy) (first (rest listy))) acc))))) (tester '(conseq-sums-acc '(10 20 30 40 50) '())) (tester '(conseq-sums-acc '(1 8 27 64 125 216 553) '())) (tester '(conseq-sums-acc '() '())) (tester '(conseq-sums-acc '(1 4 9 16 25 36 49) '())) (tester '(conseq-sums-acc '(3) '()))