序列操作

看完這一節,感覺世界觀又被重塑。
首先說說這一小節的標題”序列作爲一種約定的界面”是什麼意思:這句話意思是序列作爲程序不同模塊間數據流動的形式。
學習lisp語言不久,lisp就給了我很多驚喜,我特地新建了一個文件夾叫astonishing來放代碼☺
下面是這一節我的代碼:

(define nil (list))
;
(define (enumerate-tree-leaves t)
  (cond ((null? t) nil)
        ((not (pair? t)) (list t))
        (else (append (enumerate-tree-leaves (car t))
                      (enumerate-tree-leaves (cdr t))))))
;
(define (enumerate-interval low high)
  (define (enumerate-iter h ans)
    (if (> low h)
        ans
        (enumerate-iter (- h 1) (cons h ans))))
  (enumerate-iter high nil))
;
(define (filter predicate sequence)
  (cond ((null? sequence) nil)
        ((predicate (car sequence)) (cons (car sequence)
                                          (filter predicate (cdr sequence))))
        (else (filter predicate (cdr sequence)))))
;
(define (accumulate op initial sequence)
  (if (null? sequence)
      initial
      (op (car sequence) (accumulate op initial (cdr sequence)))))
;
(define (append x y)
  (accumulate cons y x))
;
(define (my-map proc sequence)
  (accumulate (lambda (x y) (cons (proc x) y)) nil sequence))
;
(define (sum-odd-squares tree)
  (accumulate + 0 (map (lambda (x) (* x x))
                       (filter (lambda (x) (= (remainder x 2) 1)) (enumerate-tree-leaves t)))))
;
(define (even-fibs n)
  (define (fib x)
    (define (fib-iter a b k)
      (if (= k x)
          a
          (fib-iter b (+ a b) (+ 1 k))))
    (fib-iter 0 1 0))
  (filter (lambda (x) (= (remainder x 2) 0))
          (map fib (enumerate-interval 0 6))))
;
(define (my-min sequence)
  (accumulate (lambda (x y)
                       (if (< x y)
                           x
                           y))
              10000
              sequence))
;2.33
(define (length sequence)
  (accumulate (lambda (x y) (+ 1 y)) 0 sequence))
;2.34
(define (horner-eval x coefficient-sequence)
  (accumulate (lambda (a b) (+ a (* x b))) 0 coefficient-sequence))
                                        ;2.35
(define (count-leaves t)
  (accumulate + 0 (map (lambda (x)
                         (if (pair? x)
                             (count-leaves x)
                             1))
                       t)))
;2.36
(define (accumulate-n op init seqs)
  (if (null? (car seqs))
      nil
      (cons (accumulate op init
                        (map car seqs))
            (accumulate-n op init (map cdr seqs)))))
;2.37
(define (dot-product v w)
  (accumulate + 0 (map * v w)))
(define (matrix-*-vector m v)
  (map (lambda (line) (dot-product line v)) m))
(define (transpose m)
  (accumulate-n cons nil m))
(define (matrix-*-matrix m n)
  (let ((cols (transpose n)))
    (map (lambda (row)
           (map (lambda (col)
                  (dot-product col row))
                cols))
         m)))
;2.38
(define (fold-left op init seq)
  (define (iter tmp ans)
    (if (null? tmp)
        ans
        (iter (cdr tmp) (op ans (car tmp)))))
  (iter seq init))
;2.39效率爲O(n^2)
(define (reverse1 sequence)
  (accumulate (lambda (x y) (append y (list x))) nil sequence))
;效率爲O(n)
(define (reverse2 sequence)
  (fold-left (lambda (x y) (cons y x)) nil  sequence))
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章