diff --git a/list-and-let-list/main.rkt b/list-and-let-list/main.rkt new file mode 100644 index 0000000..23dd70e --- /dev/null +++ b/list-and-let-list/main.rkt @@ -0,0 +1,65 @@ +;; The first three lines of this file were inserted by DrRacket. They record metadata +;; about the language level of this file in a form that our tools can easily process. +#reader(lib "htdp-intermediate-reader.ss" "lang")((modname main) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) +; [Number] l -> Number +; Greatest common denominator of a list of positive integers. +(define (gcdl l) + (local + [(define ln (length l))] + (cond [(= 0 ln) (error "No denominator common among nothing.")] + [(= 1 ln) (first l)] + [(= 2 ln) (gcd (first l) (second l))] + [else (gcdl (cons (gcd (first l) (second l)) (rest (rest l))))]))) + +(check-error (gcdl '())) +(check-expect (gcdl '(1)) 1) +(check-expect (gcdl '(1 2 3)) 1) +(check-expect (gcdl '(6 18 12)) 6) +(check-expect (gcdl '(100 50 2)) 2) + +; [T] l -> [[T]] +; Groups equivalent consecutive elements of a list into their own list. +(define (pack l) + (local [; [[T]] l -> T + ; Gets the first element of the first element of l. + (define (first-twice l) (first (first l))) ; This could also be replaced with caar(). + + ; [[T]] l -> [[T]] + ; Repeat twice the first element of the first element of l. + (define (twice-first l) (cons (cons (first-twice l) (first l)) (rest l))) + + ; [T] l, [[T]] m -> [[T]] + ; Inserts first element of list l in new first list in list of lists m. + (define (enlist l m) (cons (list (first l)) m)) + + ; [T] l, [[T]] a -> [[T]] + ; Groups all consecutive elements equivalent to the first element of the first element of a. + ; What's being accumulated: a reversed packed list. + ; How it's initialized: empty. + ; How it's it updated: cons to beginning. + ; How it's used in the final answer: reverse. + (define (consequivalent l a) + (cond [(empty? l) a] + [(empty? a) + (consequivalent (rest l) (enlist l empty))] + [(equal? (first l) (first-twice a)) + (consequivalent (rest l) (twice-first a))] + [else + (consequivalent (rest l) (enlist l a))]))] + (reverse (consequivalent l '())))) + +(check-expect (pack '()) '()) +(check-expect (pack '(1 2 3)) '((1) (2) (3))) +(check-expect (pack '(1 1 1 2 3 3 3 4 4 2 2 5)) '(( 1 1 1) (2) (3 3 3) (4 4) (2 2) (5))) + +; Number start, Number lower, Number upper, [Number] l -> Boolean +; Determines if an object that begins at the starting location and then moves delta by delta ever ever steps outside of the bounds (inclusive). +(define (out-of-bounds? start lower upper l) + (or (< start lower) (> start upper) + (and (not (empty? l)) (out-of-bounds? (+ start (first l)) lower upper (rest l))))) + +(check-expect (out-of-bounds? 0 -1 2 '(0 -2 3 -4 6)) #t) +(check-expect (out-of-bounds? 0 -1 1 '(1)) #f) +(check-expect (out-of-bounds? 0 -1 1 '()) #f) +(check-expect (out-of-bounds? 0 1 2 '()) #t) +(check-expect (out-of-bounds? 0 0 0 '()) #f) diff --git a/list-and-let-list/main.rkt~ b/list-and-let-list/main.rkt~ new file mode 100644 index 0000000..019f937 --- /dev/null +++ b/list-and-let-list/main.rkt~ @@ -0,0 +1,3 @@ +;; The first three lines of this file were inserted by DrRacket. They record metadata +;; about the language level of this file in a form that our tools can easily process. +#reader(lib "htdp-beginner-reader.ss" "lang")((modname main) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) diff --git a/welcome-back-to-dr-racket/main.rkt~ b/welcome-back-to-dr-racket/main.rkt~ new file mode 100644 index 0000000..737998c --- /dev/null +++ b/welcome-back-to-dr-racket/main.rkt~ @@ -0,0 +1,32 @@ +;; The first three lines of this file were inserted by DrRacket. They record metadata +;; about the language level of this file in a form that our tools can easily process. +#reader(lib "htdp-beginner-reader.ss" "lang")((modname main) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) +; List of Number -> String +; Determines whether a list of 3 numbers is ascending or descending. Throws error if neither. +(define (scending-how l) + (cond [(< (first l) (second l) (third l)) "ascending"] + [(> (first l) (second l) (third l)) "descending"] + [else (error "too complex")])) + +(check-expect (scending-how (list 1 2 3)) "ascending") +(check-expect (scending-how (list 5 4 3)) "descending") +(check-error (scending-how (list 1 0 3))) + + +; Number, Number -> Number +; Finds two positive numbers' greatest common denominator. +(define (gcd2 a b) + (cond + [(= a 0) b] + [(= b 0) a] + [(= 0 (+ (modulo a 2) (modulo b 2))) (* 2 (gcd2 (/ a 2) (/ b 2)))] + [(= 0 (modulo b 2)) (gcd2 a (/ b 2))] + [(= 0 (modulo a 2)) (gcd2 (/ a 2) b)] + [else (if (<= a b) (gcd2 a (- b a)) (gcd2 b (- a b)))])) + +(check-expect (gcd2 0 0) 0) +(check-expect (gcd2 100 0) 100) +(check-expect (gcd2 10 2) 2) +(check-expect (gcd2 10 10) 10) +(check-expect (gcd2 14 7) 7) +(check-expect (gcd2 288 64) 32)