learning-sicp

My embarrassing half assed SICP run.
git clone https://kaka.farm/~git/learning-sicp
Log | Files | Refs

exercise-38.scm (3247B)


      1 ;; Exercise 2.38
      2 
      3 (define (my-fold-right op initial sequence)
      4   (accumulate op initial sequence))
      5 
      6 (define (my-fold-left op initial sequence)
      7   (define (iter result rest)
      8     (if (null? rest)
      9         result
     10         (iter (op result (car rest))
     11               (cdr rest))))
     12   (iter initial sequence))
     13 
     14 (define (exercise-2.38)
     15   (define my-fold-right-/-ratsui
     16     (/ 1
     17        (/ 2
     18           (/ 3
     19              1))))
     20   (define my-fold-left-/-ratsui
     21     (/ (/ (/ 1
     22              1)
     23           2)
     24        3))
     25   (define my-fold-right-list-ratsui
     26     (list 1
     27           (list 2
     28                 (list 3
     29                       '()))))
     30   (define my-fold-left-list-ratsui
     31     (list (list (list '()
     32                       1)
     33                 2)
     34           3))
     35 
     36   (test-begin "2.38")
     37   (test-equal my-fold-right-/-ratsui
     38     (my-fold-right /
     39                    1
     40                    (list 1 2 3)))
     41   (test-equal my-fold-right-/-ratsui
     42     (my-fold-right /
     43                    (/ 3
     44                       1)
     45                    (list 1 2)))
     46   (test-equal my-fold-right-/-ratsui
     47     (my-fold-right /
     48                    (/ 2
     49                       (/ 3
     50                          1))
     51                    (list 1)))
     52   (test-equal my-fold-right-/-ratsui
     53     (my-fold-right /
     54                    (/ 1
     55                       (/ 2
     56                          (/ 3
     57                             1)))
     58                    (list)))
     59   (test-equal my-fold-left-/-ratsui
     60     (my-fold-left /
     61                   1
     62                   (list 1 2 3)))
     63   (test-equal my-fold-left-/-ratsui
     64     (my-fold-left /
     65                   (/ 1
     66                      1)
     67                   (list 2 3)))
     68   (test-equal my-fold-left-/-ratsui
     69     (my-fold-left / (/ (/ 1
     70                           1)
     71                        2)
     72                   (list 3)))
     73   (test-equal my-fold-left-/-ratsui
     74     (my-fold-left / (/ (/ (/ 1
     75                              1)
     76                           2)
     77                        3)
     78                   (list)))
     79   (test-equal my-fold-right-list-ratsui
     80     (my-fold-right list
     81                    '()
     82                    (list 1 2 3)))
     83   (test-equal my-fold-right-list-ratsui
     84     (my-fold-right list
     85                    (list 3 '())
     86                    (list 1 2)))
     87   (test-equal my-fold-right-list-ratsui
     88     (my-fold-right list
     89                    (list 2 (list 3 '()))
     90                    (list 1)))
     91   (test-equal my-fold-right-list-ratsui
     92     (my-fold-right list
     93                    (list 1 (list 2 (list 3 '())))
     94                    (list)))
     95   (test-equal my-fold-left-list-ratsui
     96     (my-fold-left list
     97                   '()
     98                   (list 1 2 3)))
     99   (test-equal my-fold-left-list-ratsui
    100     (my-fold-left list
    101                   (list '() 1)
    102                   (list 2 3)))
    103   (test-equal my-fold-left-list-ratsui
    104     (my-fold-left list
    105                   (list (list '() 1) 2)
    106                   (list 3)))
    107   (test-equal my-fold-left-list-ratsui
    108     (my-fold-left list
    109                   (list (list (list '() 1) 2) 3)
    110                   (list)))
    111   (test-end "2.38")
    112 
    113   ;; if op: A -> A,
    114   ;; (cut my-fold-right op <> <>) is equivalent to (cut my-fold-left op <> <>)
    115   ;; iff
    116   ;; for all a, b, c in A,
    117   ;; (op (op a b) c) = (op a (op b c)).
    118   ;; Did I get that right? Seems right.
    119   )
    120 
    121 (exercise-2.38)
    122