Add lab and quiz contents
This commit is contained in:
parent
8addf5798a
commit
a00aa74877
28
labs/L07/short-circuit.rkt
Normal file
28
labs/L07/short-circuit.rkt
Normal file
@ -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)))
|
0
labs/L07/short-circuit.rkt~
Normal file
0
labs/L07/short-circuit.rkt~
Normal file
27
tests/Q1/quiz1.rkt
Normal file
27
tests/Q1/quiz1.rkt
Normal file
@ -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
|
||||||
|
)
|
0
tests/Q1/quiz1.rkt~
Normal file
0
tests/Q1/quiz1.rkt~
Normal file
Loading…
Reference in New Issue
Block a user