分からなくなってきた。。。
ねむい
;;何だか自信無くなって来ました。。。。 ;;; q2.33 (define (accumulate op initial sequence) (if (null? sequence) initial (op (car sequence) (accumulate op initial (cdr sequence)))) ) ;;(accumulate + 0 (list 1 2 3 4 5)) (define (map p sequence) (accumulate (lambda (x y) (cons (p x) y) ) '() sequence) ) (define (square x)(* x x)) (trace map) (trace accumulate) ;;(map square (list 1 2 3)) (define (append seq1 seq2) (accumulate cons seq2 seq1) ) ;;(append (list 1 2 3) (list 4 5 6)) (define (length sequence) (accumulate (lambda (x y) ; (if (not(null? y)) (+ 1 y) ; (+ 0 y)) ) 0 sequence) ) ;;(length (list 1 3 4)) ;;; q2.34 (define (horner-eval x coefficient-sequence) (/(accumulate (lambda (this-coeff higher-terms) (* x (+ this-coeff higher-terms)) ) 0 coefficient-sequence) x) ) ;;1 + 3x + 0x(2) + 5x(3) + 0x(4) + 1x(5) ;;(horner-eval 2 (list 1 3 0 5 0 1)) ;;(horner-eval 2 (list 1 3 )) ;;なんだか問題と違う形になったけどまぁいいか。。。。 ;;頭の中で当てはめました。 ;;; q2.35 (define (count-leaves t) (accumulate (lambda (x y) (+ 1 y) ) 0 (map list t)) ) ;;(accumulate (lambda (x y) ;; (+ 1 y) ;; ) 1 ;; (map list (list 1 2 (list 4 5)3))) ;;(count-leaves (list 1 2 3)) ;;入れ子非対応!!!!!! ;;; q2.36 (define (accumulate-n op init seqs) (if (null? (car seqs)) '() (cons (accumulate op init (map car seqs)) (accumulate-n op init (map cdr seqs)))) ) ;;(define s0 (list 1 2 3)) (define s (list (list 1 2 3) (list 4 5 6) (list 7 8 9) (list 10 11 12))) ;;(count-leaves s) ;=>4 ;;(accumulate-n + 0 s) ;;(accumulate + 0 (map car s)) ;;(map car (map cdr s)) ;;時間を忘れて遊んでしまうな@scheme ;;; q2.37 ;;;vector=>一瞬で飛ばし確定 ;;; q2.38 (define (fold-left op initial sequence) (fold-left-iter op initial sequence) ) (define (fold-left-iter op result rest) (if (null? rest) result (fold-left-iter op (op result (car rest)) (cdr rest))) ) (trace fold-left-iter) ;;(fold-left / 1 (list 2 3 4)) ;;(fold-left list '() (list 1 2 3)) (define (fold-right op initial sequence) (fold-right-iter op initial sequence) ) (define (fold-right-iter op result rest) (if (null? rest) result (fold-right-iter op (op (car rest) result) (cdr rest))) ) (trace fold-right-iter) ;;(fold-right / 1 (list 2 3 4)) ;;(fold-right list '() (list 1 2 3)) ;;これreverseなんだが。。。 ;;(fold-right cons '() (list 1 2 3)) ;;; q2.39 (define (reverse sequence) (fold-right (lambda (x y) (cons x y) ) '() sequence) ) ;;(reverse (list 1 2 3)) (define (reverse sequence) (fold-left (lambda (x y) (cons y x) ) '() sequence) ) ;;(reverse (list 1 2 3))
そろそろ雰囲気で書いてたらワケわかんなくなってきてるなぁ