0
define class1-scores (list 10 89 56 99 73 77 45 86 84))
(define pass-score 60)
(define (map proc lst)
(if (null? lst)
(list)
(cons (proc (car lst)) (map proc (cdr lst)))))
(define (accumulate combiner null-value lst)
(if (null? lst)
null-value
(combiner (car lst) (accumulate combiner null-value (cdr lst)))))
(define (iter-accumulate combiner null-value lst)
(define (helper lst so-far)
(if (null? lst)
so-far
(helper (cdr lst) (combiner (car lst) so-far))))
(helper lst null-value))
(define (filter pred lst)
(if (null? lst)
(list)
(let ((item (car lst)))
(if (pred item)
(cons item (filter pred (cdr lst)))
(filter pred (cdr lst))))))
;; 1.
(define (score-margin scores pass-score)
(cond ((null? scores) 'null)
((null? (cdr scores)) (- (car scores) pass-score))
(else (score-margin (cdr scores) pass-score))))
(define class2-scores (list 65 50 70 85 80))
(score-margin class2-scores pass-score)
; 20
; this procedure is iterative process,
; because nothing is stored in stack.
; time complexity is n, space complexity is constant.
;; 2.
(define (class-average scores)
(/ (accumulate (lambda (a b) (+ a b)) 0 scores) (length scores)))
; this procedure is recursive process,
; because accumulate we defined is recursive.
; so, time complexity & space complexity is n.
;; 3.
(define (list-max lst)
(if (null? (cdr lst))
(car lst)
(max (car lst) (list-max (cdr lst)))))
; this procedure is recursive process.
; time complexity & space complexity is n.
;; 4.
(define (score-high scores)
(accumulate max 0 scores))
(define (score-range scores)
(define (helper scores low-so-far high-so-far)
(if (null? scores)
(list low-so-far high-so-far)
(helper (cdr scores) (min (car scores) low-so-far)
(max (car scores) high-so-far))))
(helper (cdr scores) (car scores) (car scores)))
;; ---
(define (mid-kor mid) (car mid))
(define (mid-eng mid) (cdr mid))
(define (final-kor final) (car final))
(define (final-eng final) (cdr final))
(define mid-scores (list (cons 90 97) (cons 80 70) (cons 84 85)))
(define final-scores (list (cons 95 94) (cons 80 75) (cons 80 87)))
;; 6.
(define (grade-improved-kor final mid)
(if (or (null? mid) (null? final))
'()
(cons (- (final-kor (car final)) (mid-kor (car mid)))
(grade-improved-kor (cdr final) (cdr mid)))))
(grade-improved-kor final-scores mid-scores)
; (5 0 -4)
;; 7.
(define (map2 proc lst1 lst2)
(if (or (null? lst1) (null? lst2))
(list)
(cons (proc (car lst1) (car lst2))
(map2 proc (cdr lst1) (cdr lst2)))))
(define (grade-improved-kor final mid)
(map2 (lambda (a b) (- a b)) (map final-kor final) (map mid-kor mid)))
;; 8.
(define (improved-kor? final mid)
(>= (final-kor final) (mid-kor mid)))
(define (improved-eng? final mid)
(>= (final-eng final) (mid-eng mid)))
(define (filter2 pred lst1 lst2)
(if (or (null? lst1) (null? lst2))
(list)
(let ((item1 (car lst1)) (item2 (car lst2)))
(if (pred item1 item2)
(cons (cons item1 item2) (filter2 pred (cdr lst1) (cdr lst2)))
(filter2 pred (cdr lst1) (cdr lst2))))))
(define (improved-scores-kor final mid)
(map (lambda (item) (final-kor (car item)))
(filter2 improved-kor? final mid)))
(improved-scores-kor final-scores mid-scores)
; (95 80)
;; 9,
(define (merge2 pred proc lst1 lst2)
(cond ((null? lst1) '())
((pred (car lst1) (car lst2))
(cons (proc (car lst1) (car lst2))
(merge2 pred proc (cdr lst1) (cdr lst2))))
(else (merge2 pred proc (cdr lst1) (cdr lst2)))))
;; 10.
(define (improved-scores-kor final mid)
(merge2 improved-kor? (lambda (final mid) (final-kor final)) final mid))
;; 11.
(define (improved-kor-show-eng final mid)
(merge2 improved-kor? (lambda (final mid) (final-eng final)) final mid))
(improved-kor-show-eng final-scores mid-scores)
; (94 75)
(define (improved-kor-total-eng final mid)
(accumulate (lambda (a b) (+ a b)) 0 (improved-kor-show-eng final mid)))
(improved-kor-total-eng final-scores mid-scores)
; 169
;; 12.
(define (improved-kor-eng-total-scores final mid)
(merge2 (lambda (final mid)
(and (improved-kor? final mid)
(improved-eng? final mid)))
(lambda (final mid)
(+ (final-kor final) (final-eng final)
(mid-kor mid) (mid-eng mid)))
final mid))
(improved-kor-eng-total-scores final-scores mid-scores)
; (305)
(define my-mids (list (cons 5 6) (cons 6 7) (cons 4 10) (cons 8 9)))
(define my-finals (list (cons 6 6) (cons 7 6) (cons 5 10) (cons 10 9)))
(improved-kor-eng-total-scores my-finals my-mids)
; (23 29 36)
Tümünü Göster