zoukankan      html  css  js  c++  java
  • SICP_2.33-2.39

      1 #lang racket
      2 
      3 ;;;;;;;;;;;;;;;;;;;;;;;;;;;2.33
      4 (define (accumulate op initial sequence)
      5   (if (null? sequence)
      6       initial
      7       (op (car sequence)
      8           (accumulate op initial (cdr sequence)))))
      9 
     10 (define (my-map p sequence)
     11   (accumulate (lambda (x y)
     12                 (cons (p x) y)) '() sequence))
     13 
     14 (define (my-append seq1 seq2)
     15   (accumulate cons seq2 seq1))
     16 
     17 (define (my-length sequence)
     18   (accumulate (lambda (x y) (+ 1 y)) 0 sequence))
     19 
     20 ;;;;;;;;;;;;test
     21 (my-map (lambda (x) (* x x)) (list 1 2 3))
     22 
     23 (my-append (list 1 2 3) (list 4 5 6))
     24 
     25 (my-length (list 1 2 3 4 5))
     26 
     27 ;;;;;;;;;;;;;;;;;;;;;;;;;2.34
     28 (define (horner-eval x coefficient-sequence)
     29   (accumulate (lambda (this-coeff higher-terms)
     30                 (+ this-coeff (* x higher-terms)))
     31               0
     32               coefficient-sequence))
     33 
     34 ;;;;;;;;;;test
     35 (horner-eval 2 (list 1 3 0 5 0 1))
     36 
     37 ;;;;;;;;;;;;;;;;;;;;;;;;2.35
     38 (define (count-leaves tree)
     39   (accumulate + 0 (map (lambda (node)
     40                          (if (pair? node)
     41                              (count-leaves node)
     42                              1))
     43                        tree)))
     44 
     45 ;;;;;;;;;;;;test
     46 (count-leaves (list '(1 2 3) '(2 3 4) 4 5))
     47 
     48 ;;;;;;;;;;;;;;;;;;;;2.36
     49 (define (accumulate-n op init seqs)
     50   (if (null? (car seqs))
     51       '()
     52       (cons (accumulate op init (map car seqs))
     53             (accumulate-n op init (map cdr seqs)))))
     54 
     55 ;;;;;;;;;;;;test
     56 (define seqs (list '(1 2 3) '(4 5 6) '(7 8 9) '(10 11 12)))
     57 (accumulate-n + 0 seqs)
     58 
     59 ;;;;;;;;;;;;;;;;;;;;2.37
     60 (define (dot-product v1 v2)
     61   (accumulate + 0 (map * v1 v2)))
     62 
     63 (define (matrix-*-vector m v)
     64   (map (lambda (x) (dot-product x v)) m))
     65 
     66 (define (matrix-*-matrix m n)
     67   (let ((n-cols (transpose n)))
     68     (map (lambda (m-row)
     69            (map (lambda (n-col)
     70                   (dot-product m-row n-col))
     71                 n-cols))
     72          m)))
     73 
     74 (define (transpose mat)
     75   (accumulate-n cons '() mat))
     76 
     77 (define matrix1 (list '(1 2 3) '(1 9 3) '(1 3 4)))
     78 (define matrix2 (list '(1 3 4) '(3 4 5) '(6 2 4)))
     79 ;;;;;;;;;;;;test
     80 (dot-product (list 1 1 1) (list 1 2 3))
     81 (matrix-*-vector (list '(1 2 3) '(1 2 3) '(1 3 3)) '(2 3 4))
     82 (matrix-*-matrix matrix1 matrix2)
     83 
     84 ;;;;;;;;;;;;;;;;;;;2.38
     85 
     86 (define (fold-left op initial sequence)
     87   (define (iter result rest)
     88     (if (null? rest)
     89         result
     90         (iter (op result (car rest))
     91               (cdr rest))))
     92   (iter initial sequence))
     93 
     94 (define (fold-right op initial sequence)
     95   (accumulate op initial sequence))
     96 
     97 ;;;;;;;;;;;;;;;test
     98 (fold-right / 1 (list 1 2 3))
     99 (fold-left / 1 (list 1 2 3))
    100 (fold-right list '() (list 1 2 3))
    101 (fold-left list '() (list 1 2 3))
    102 ;;;;;;;;;;;用cons或list这些将产生不同结果
    103 ;;;;;;;;;;;要使用那些在操作数互换位置后意义相同的。
    104 
    105 ;;;;;;;;;;;;;;;;;;2.39
    106 
    107 (define (reverse1 sequence)
    108   (fold-right (lambda (x y)
    109                 (append y (list x))) '() sequence))
    110 
    111 (define (reverse2 sequence)
    112   (fold-left (lambda (x y)
    113                (append (list y) x)) '() sequence))
    114 
    115 ;;;;;;;;;;;;test
    116 (reverse1 (list 1 2 3 4 6 8 9))
    117 (reverse2 (list 1 2 3 4 7 8 5))

    终于会一点了

    需要复习线性代数了,感觉白学了!

    Yosoro
  • 相关阅读:
    git clone GitLab 工程报错Repository not found
    vue + Element-ui 实现分页
    Element-ui 实现table的合计功能
    python 使用UUID库生成唯一ID
    css 设置overflow:scroll 滚动条的样式
    下载css-loader 安装及使用
    vue实现首页导航切换不同路由的方式(二)【使用vuex实现的】
    vue实现随机验证码功能
    vue实现首页导航切换不同路由的方式
    vue实现菜单切换
  • 原文地址:https://www.cnblogs.com/tclan126/p/6411812.html
Copyright © 2011-2022 走看看