Advertisement
timothy235

sicp-2-1-3-what-is-data

Feb 18th, 2016
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Racket 2.44 KB | None | 0 0
  1. #lang racket
  2.  
  3. ;;;;;;;;;
  4. ;; 2.4 ;;
  5. ;;;;;;;;;
  6.  
  7. (define (my-cons x y)
  8.   (lambda (m) (m x y)))
  9.  
  10. (define (my-car z)
  11.   (z (lambda (p q) p)))
  12.  
  13. ;; Verify (my-car (my-cons x y)) returns x using the substitution model of evaluation.
  14.  
  15. ;; (my-car (my-cons x y))
  16. ;; (my-car (lambda (m) (m x y)))
  17. ;; ((lambda (m) (m x y)) (lambda (p q) p))
  18. ;; ((lambda (p q) p) x y)
  19. ;; x
  20.  
  21. (define (my-cdr z)
  22.   (z (lambda (p q) q)))
  23.  
  24. (define pr (my-cons 1 2))
  25. (my-car pr)
  26. ;; 1
  27. (my-cdr pr)
  28. ;; 2
  29.  
  30. ;;;;;;;;;
  31. ;; 2.5 ;;
  32. ;;;;;;;;;
  33.  
  34. (define (my-num-cons a b)
  35.   (* (expt 2 a) (expt 3 b)))
  36.  
  37. (define (my-num-car z)
  38.   (if (zero? (remainder z 2))
  39.     (add1 (my-num-car (/ z 2)))
  40.     0))
  41.  
  42. (define (my-num-cdr z)
  43.   (if (zero? (remainder z 3))
  44.     (add1 (my-num-cdr (/ z 3)))
  45.     0))
  46.  
  47. (define num-pr (my-num-cons 2 5))
  48. (my-num-car num-pr)
  49. ;; 2
  50. (my-num-cdr num-pr)
  51. ;; 5
  52.  
  53. ;;;;;;;;;
  54. ;; 2.6 ;;
  55. ;;;;;;;;;
  56.  
  57. (define (my-compose f g)
  58.   (lambda (x) (f (g x))))
  59.  
  60. (define (repeated f n)
  61.   (if (= n 1)
  62.     f
  63.     (my-compose f (repeated f (sub1 n)))))
  64.  
  65. ;; church numerals
  66.  
  67. (define zero (lambda (f) (lambda (x) x)))
  68. (define (add-1 n)
  69.   (lambda (f)
  70.     (lambda (x) (f ((n f) x)))))
  71.  
  72. ;; Evaluate (add-1 zero) to get a direct definition of one.
  73.  
  74. ;; (add-1 zero)
  75. ;; (lambda (f) (lambda (x) (f ((zero f) x))))
  76. ;; (lambda (f) (lambda (x) (f x))) ; because (zero f) is the identity function
  77. ;; (lambda (f) f)
  78.  
  79. (define one (lambda (f) f)) ; or (lambda (f) (repeated f 1))
  80.  
  81. ;; Evaluate (add-1 one) to get a direct definition of two.
  82.  
  83. ;; (add-1 one)
  84. ;; (lambda (f) (lambda (x) (f ((one f) x))))
  85. ;; (lambda (f) (lambda (x) (f (f x)))) ; because (one f) is the function f
  86. ;; (lambda (f) (my-compose f f))
  87.  
  88. (define two (lambda (f) (my-compose f f))) ; or (lambda (f) (repeated f 2))
  89.  
  90. ;; The church numeral n is the function of f that returns the function of x
  91. ;; that applies f to x n times.  In other words n(f) is f composed with itself n
  92. ;; times.  So church numeral addition should be composition of functions.
  93.  
  94. (define (church-numeral->numeral n)
  95.   ((n add1) 0)) ; sends zero to 0, one to 1, two to 2, etc
  96.  
  97. (define (church-plus n m)
  98.   (lambda (f)
  99.     (repeated f (+ (church-numeral->numeral n)
  100.                    (church-numeral->numeral m)))))
  101.  
  102. (church-numeral->numeral zero)
  103. ;; 0
  104. (church-numeral->numeral one)
  105. ;; 1
  106. (church-numeral->numeral two)
  107. ;; 2
  108. (church-numeral->numeral (church-plus one zero))
  109. ;; 1
  110. (church-numeral->numeral (church-plus one two))
  111. ;; 3
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement