SICP_2.33-2.39

本文通过具体的Scheme语言代码示例,介绍了如何利用高阶函数如accumulate、fold等进行列表操作和矩阵运算,包括求和、乘积、矩阵乘法等,并展示了递归与折叠函数的应用。
  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))

终于会一点了

 

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

转载于:https://www.cnblogs.com/tclan126/p/6411812.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值