Advertisement
timothy235

sicp-2-2-1-representing-sequences

Feb 20th, 2016
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Racket 3.06 KB | None | 0 0
  1. #lang racket
  2.  
  3. ;;;;;;;;;;
  4. ;; 2.17 ;;
  5. ;;;;;;;;;;
  6.  
  7. ;; I believe it's customary with Racket to use first instead of car and rest
  8. ;; instead of cdr.
  9.  
  10. (define (my-last-pair lst)
  11.   (define rest-of-list (rest lst))
  12.   (if (empty? rest-of-list)
  13.     lst
  14.     (my-last-pair rest-of-list)))
  15.  
  16. (define lst (list 1 2 3 4))
  17. (my-last-pair lst)
  18. ;; '(4)
  19.  
  20. ;;;;;;;;;;
  21. ;; 2.18 ;;
  22. ;;;;;;;;;;
  23.  
  24. (define (my-reverse lst)
  25.   (define (iter old-lst new-lst)
  26.     (if (empty? old-lst)
  27.       new-lst
  28.       (iter (rest old-lst) (cons (first old-lst) new-lst))))
  29.   (iter lst empty))
  30.  
  31. (my-reverse lst)
  32. ;; '(4 3 2 1)
  33.  
  34. ;;;;;;;;;;
  35. ;; 2.19 ;;
  36. ;;;;;;;;;;
  37.  
  38. (define us-coins (list 50 25 10 5 1))
  39. (define uk-coins (list 100 50 20 10 5 2 1 0.5))
  40.  
  41. (define (cc amount coin-values)
  42.   (cond [(= amount 0) 1]
  43.         [(or (< amount 0) (no-more? coin-values)) 0]
  44.         [else
  45.           (+ (cc amount
  46.                  (except-first-denomination coin-values))
  47.              (cc (- amount
  48.                     (first-denomination coin-values))
  49.                  coin-values))]))
  50.  
  51. (define (first-denomination coin-values)
  52.   (first coin-values))
  53. (define (except-first-denomination coin-values)
  54.   (rest coin-values))
  55. (define (no-more? coin-values)
  56.   (empty? coin-values))
  57.  
  58. (cc 100 us-coins)
  59. ;; 292
  60. (cc 100 (my-reverse us-coins))
  61. ;; 292
  62. (cc 100 (shuffle us-coins))
  63. ;; 292
  64.  
  65. ;; I don't think re-ordering the list of coin values affects the final answer.
  66. ;; The validity of the recursion does not depend on the order of the
  67. ;; denominations.
  68.  
  69. ;;;;;;;;;;
  70. ;; 2.20 ;;
  71. ;;;;;;;;;;
  72.  
  73. (define (my-filter predicate? items)
  74.   (cond [(empty? items) empty]
  75.         [(predicate? (first items))
  76.          (cons (first items)
  77.                (my-filter predicate? (rest items)))]
  78.         [else (my-filter predicate? (rest items))]))
  79.  
  80. (define (same-parity x . ys)
  81.   (define (same-parity-as-x? y)
  82.     (= (remainder (- x y) 2) 0))
  83.   (cons x (my-filter same-parity-as-x? ys)))
  84.  
  85. (same-parity 1 2 3 4 5 6 7)
  86. ;; '(1 3 5 7)
  87. (same-parity 2 3 4 5 6 7)
  88. ;; '(2 4 6)
  89.  
  90. ;;;;;;;;;;
  91. ;; 2.21 ;;
  92. ;;;;;;;;;;
  93.  
  94. (define (square-list items)
  95.   (if (empty? items)
  96.     empty
  97.     (cons (sqr (first items))
  98.           (square-list (rest items)))))
  99.  
  100. (define (my-map proc items)
  101.   (if (empty? items)
  102.     empty
  103.     (cons (proc (first items))
  104.           (my-map proc (rest items)))))
  105.  
  106. (define (new-square-list items)
  107.   (map sqr items))
  108.  
  109. (square-list (list 1 2 3 4))
  110. ;; '(1 4 9 16)
  111. (new-square-list (list 1 2 3 4))
  112. ;; '(1 4 9 16)
  113.  
  114. ;;;;;;;;;;
  115. ;; 2.22 ;;
  116. ;;;;;;;;;;
  117.  
  118. ;; cons'ing up a new list by cdr'ing down an old list processes the elements in
  119. ;; last-in-first-out order.  So everything is reversed.  It's like a stack where
  120. ;; cons is the push and car/cdr is the pop.
  121.  
  122. ;; Reversing the arguments does not work because you can't cons a list onto an
  123. ;; element.
  124.  
  125. ;;;;;;;;;;
  126. ;; 2.23 ;;
  127. ;;;;;;;;;;
  128.  
  129. (define (my-for-each proc items)
  130.   (cond [(empty? items) (void)]
  131.         [else
  132.           (proc (first items))
  133.           (my-for-each proc (rest items))]))
  134.  
  135. (my-for-each displayln (list 57 321 88))
  136. ;; 57
  137. ;; 321
  138. ;; 88
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement