diff --git a/labs/L07/short-circuit.rkt b/labs/L07/short-circuit.rkt new file mode 100644 index 0000000..57cbac2 --- /dev/null +++ b/labs/L07/short-circuit.rkt @@ -0,0 +1,28 @@ +#lang racket +(define-syntax-rule (And a b) + (if b a #f)) + +(module+ test + (require rackunit) + (define (die) + (error 'die "don't run this")) + + (check-equal? (And (die) #f) #f) + (check-exn exn:fail? (lambda () (and (die) #f)))) + +(module+ test + (define-syntax-rule (check-fail expr) + (check-exn exn:fail? (lambda () expr))) + (check-fail (and (die) #f)) + (check-fail (And #f (die)))) + +(define-syntax-rule (Or a b) + (or b a)) + ;(if (not a) b #t)) + +(module+ test + (check-equal? (Or #t #t) #t) + (check-equal? (Or #f #t) #t) + (check-equal? (Or #t #f) #t) + (check-equal? (Or (die) #t) #t) + (check-fail (or (die) #t))) \ No newline at end of file diff --git a/labs/L07/short-circuit.rkt~ b/labs/L07/short-circuit.rkt~ new file mode 100644 index 0000000..e69de29 diff --git a/tests/Q1/quiz1.rkt b/tests/Q1/quiz1.rkt new file mode 100644 index 0000000..a1ea8bb --- /dev/null +++ b/tests/Q1/quiz1.rkt @@ -0,0 +1,27 @@ +#lang racket + +(define (sum lst) ;define sum function + (define (helper lst sum) ;setup tail recursive helper + (cond ;conditions for recursion + [(empty? lst) 0] ;base case + [else (if ;case for if the list item is a number + (number? (first lst)) ;if the item is a number + (helper ((rest lst) (+ sum (first lst)))) ;call the helper function with the rest of the list and add to sum + (helper (rest lst) sum)) ;error case if it is not a number + ])) + (helper lst 0)) ;first recursive call + +;my attempet to use match, was not going well +;(match lst +; [`() 0] +; [(number? head tail ...) (+ sum head) + +(module+ test + (require rackunit) + (check-equal? (sum `()) 0) + (check-equal? (sum `(1 2)) 3) + (check-equal? (sum `(1 2 "buckle my shoe")) 3) + (check-equal? (sum `(-1 -2)) -3) ;test for negative numbers + (check-equal? (sum `(-1 `(1 2) -2))) ;test for recursive lists + (check-equal? (sum `(0.1 0.2)) 0.3) ;test for floating point numbers + ) \ No newline at end of file diff --git a/tests/Q1/quiz1.rkt~ b/tests/Q1/quiz1.rkt~ new file mode 100644 index 0000000..e69de29