SICP

it2024-12-08  19

3.15

对于z1

 

z1 ------>[ * ][ * ]

                |     |

               v     v

              [ * ][ * ]----->[ * ][ \ ]

                |                  |

               v                  v

               wow             b

 

对于z2

 

z2 ----->[ * ][ * ]

               |     |                            

               |     + ------------------>[ * ][ * ]------>[ * ][ \ ]

               |                                  |                    |

               |                                  v                   v

               |                                  a                   b

               |                                                       ^

               |                                                       |

               +----------------------->[ * ][ * ]------>[ * ][ \ ]

                                                  |

                                                  v

                                                  wow

 

3.16

1 (define l2 (list 1)) 2 (define l3 (cons l2 l2)) ->结果为4 3 (define l4 (list l3)) 4 5 6 (define l1 '(1 2 3)) ->结果为3 7 8 (define l5 (list a)) 9 (define l6 (cons l5 l5)) 10 (define l7 (cons l6 l6)) ->结果为7

 1 (define l8 (set-car! (cddr l1) l1)) ->什么都不返回

 

3.17

1 (define new-count-pairs 2 (let ((store '())) 3 (lambda (x) 4 (cond ((not (pair? x)) 0) 5 ((memq x store) 0) 6 (else (set! store (cons x store)) 7 (+ (new-count-pairs (car x)) 8 (new-count-pairs (cdr x)) 9 1))))))

 

把检查过的元素放到一个表中。在计数之前检查是否存在。注意memq的用法!!!

 

3.18

这一题的一些解法

1 (define (cycle? x) 2 (define (iter search-list seen) 3 (cond ((not (pair? search-list)) #f) 4 ((memq search-list seen) #t) 5 (else (or (iter (car search-list) (cons search-list seen)) 6 (iter (cdr search-list) (cons search-list seen)))))) 7 (iter x '()))

这一题重点是要注意car也可以形成环

 

另一种貌似有问题的解法

 

他这里的解法没有考虑表中出现重复元素的情况。

 

3.19

1 (define (loop? ls) 2 (define (iter x y) 3 (let ((x-walk (list-walks 1 x)) 4 (y-walk (list-walks 2 y))) 5 (cond ((or (null? x-walks) (null? y-walks)) #f) 6 ((eq? x-walks y-walks) #t) 7 (else (iter x-walk y-walk))))) 8 (iter ls ls)) 9 10 (define (list-walks step ls) 11 (cond ((null? ls) '()) 12 ((zero? step) ls) 13 (else (list-walks (- step 1) 14 (cdr ls)))))

不会这题,参考,用了算法:龟兔赛跑

 

3.20

还是理解的不透彻

 

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

最新回复(0)