#reader(lib "htdp-beginner-reader.ss" "lang")((modname grades-average-list-mixed) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f ())))
(define-struct retake (fst snd))
(check-expect (how-many-done (list "sick" empty empty "travel")) 2)
(check-expect (how-many-done (list 45 empty 40 empty 90)) 5)
(check-expect (how-many-done empty) 0)
(check-expect (how-many-done (list 60 70 (make-retake 40 50) "sick" 60 30)) 5)
(define (how-many-done alog)
(cond
[(empty? alog) 0]
[(empty? (first alog)) (+ 1 (how-many-done (rest alog)))]
((string? (first alog)) (+ 0 (how-many-done (rest alog))))
[else
(+ 1 (how-many-done (rest alog)))]))
(check-expect (summation (list 45 45 45 45)) 180)
(check-expect (summation (list empty 45 45 45)) 135)
(check-expect (summation (list empty 30 empty "sick" 70)) 100)
(check-expect (summation empty) 0)
(define (summation alog)
(cond
[(empty? alog) 0]
[(cons? alog)
(cond [(empty? (first alog)) (+ 0 (summation (rest alog)))]
[(retake? (first alog)) (+ (max (retake-fst (first alog))
(retake-snd (first alog)))
(summation (rest alog)))]
[(string? (first alog)) (summation (rest alog))]
[else (+ (first alog) (summation (rest alog)))])]))
(check-expect (average (list 45 45 45 45 45 empty 45)) (/ (* 6 45) 7))
(check-expect (average (list 100 empty "sick" 50)) 50)
(check-expect (average empty) "No Data")
(check-expect (average (list empty empty "excused" empty)) 0)
(define (average alog)
(cond
[(= (how-many-done alog) 0) "No Data"]
[else (/ (summation alog) (how-many-done alog))]))
(require (planet nah22/racketui))
(define/web grade/web
(oneof ["Actual grade" number]
["Retake" (structure make-retake
["First attempt" number]
["Second attempt" number])]
["Excused (reason)" string+]
["Missing" (constant empty)]))
(web-launch "Grade Average Computer"
(function "Computes the average of grades in the given list."
(average ["Grades" (listof ["Grade Result" grade/web])]
-> ["Final Grade"
(oneof ["Average grade" number]
["No Data" (constant "No Data")])])))