Advertisement
dgulczynski

balance.rkt

Jun 6th, 2018
498
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Racket 3.72 KB | None | 0 0
  1. #lang racket
  2.  
  3. ;;; rozdział 3.1.1
  4.  
  5. (define balance 100)
  6.  
  7. (define (withdraw amount)
  8.   (if (>= balance amount)
  9.       (begin (set! balance (- balance amount))
  10.              balance)
  11.       "Insufficient funds"))
  12.  
  13. ;: (withdraw 25)
  14. ;: (withdraw 25)
  15. ;: (withdraw 60)
  16. ;: (withdraw 15)
  17.  
  18. (define new-withdraw
  19.   (let ((balance 100))
  20.     (lambda (amount)
  21.       (if (>= balance amount)
  22.           (begin (set! balance (- balance amount))
  23.                  balance)
  24.           "Insufficient funds"))))
  25.  
  26.  
  27. (define (make-withdraw balance)
  28.   (lambda (amount)
  29.     (if (>= balance amount)
  30.         (begin (set! balance (- balance amount))
  31.                balance)
  32.         "Insufficient funds")))
  33.  
  34.  
  35. ;: (define W1 (make-withdraw 100))
  36. ;: (define W2 (make-withdraw 100))
  37. ;: (W1 50)
  38. ;: (W2 70)
  39. ;: (W2 40)
  40. ;: (W1 40)
  41.  
  42. (define (make-account balance password)
  43.   (define (withdraw amount)
  44.     (if (>= balance amount)
  45.         (begin (set! balance (- balance amount))
  46.                balance)
  47.         "Insufficient funds"))
  48.   (define (deposit amount)
  49.     (set! balance (+ balance amount))
  50.     balance)
  51.   (define (incorrect-pass i)
  52.     'incorect-pass)
  53.   (define (change-pass p)
  54.     (set! password p))
  55.   (define (dispatch m p)
  56.     (cond ((not (eq? p password)) incorrect-pass)
  57.           ((eq? m 'change-pass) change-pass)
  58.           ((eq? m 'withdraw) withdraw)
  59.           ((eq? m 'deposit) deposit)
  60.           (else (error "Unknown request -- MAKE-ACCOUNT"
  61.                        m))))
  62.   dispatch)
  63.  
  64. ;: (define acc (make-account 100))
  65.  
  66. ;: ((acc 'withdraw) 50)
  67. ;: ((acc 'withdraw) 60)
  68. ;: ((acc 'deposit) 40)
  69. ;: ((acc 'withdraw) 60)
  70.  
  71. ;;; rozdział 3.1.3
  72.  
  73. (define (make-simplified-withdraw balance)
  74.   (lambda (amount)
  75.     (set! balance (- balance amount))
  76.     balance))
  77.  
  78.  
  79. ;: (define W (make-simplified-withdraw 25))
  80. ;: (W 20)
  81. ;: (W 10)
  82.  
  83.  
  84. (define (make-decrementer balance)
  85.   (lambda (amount)
  86.     (- balance amount)))
  87.  
  88. ;: (define D (make-decrementer 25))
  89. ;: (D 20)
  90. ;: (D 10)
  91.  
  92. ;: ((make-decrementer 25) 20)
  93. ;: ((lambda (amount) (- 25 amount)) 20)
  94. ;: (- 25 20)
  95.  
  96. ;: ((make-simplified-withdraw 25) 20)
  97.  
  98. ;: ((lambda (amount) (set! balance (- 25 amount)) 25) 20)
  99. ;: (set! balance (- 25 20)) 25
  100.  
  101. ;;; Toższamość obiektów
  102.  
  103. ;: (define D1 (make-decrementer 25))
  104. ;: (define D2 (make-decrementer 25))
  105. ;:
  106. ;: (define W1 (make-simplified-withdraw 25))
  107. ;: (define W2 (make-simplified-withdraw 25))
  108. ;:
  109. ;: (W1 20)
  110. ;: (W1 20)
  111. ;: (W2 20)
  112.  
  113. ;: (define peter-acc (make-account 100))
  114. ;: (define paul-acc (make-account 100))
  115. ;:
  116. ;: (define peter-acc (make-account 100))
  117. ;: (define paul-acc peter-acc)
  118.  
  119. ;;; Kłopoty z programowaniem imperatywnym
  120.  
  121. #|(define (factorial n)
  122.   (define (iter product counter)
  123.     (if (> counter n)
  124.         product
  125.         (iter (* counter product)
  126.               (+ counter 1))))
  127.   (iter 1 1))
  128. |#
  129. (define (factorial n)
  130.   (let ((product 1)
  131.         (counter 1))
  132.     (define (iter)
  133.       (if (> counter n)
  134.           product
  135.           (begin (set! product (* counter product))
  136.                  (set! counter (+ counter 1))
  137.                  (iter))))
  138.     (iter)))
  139.  
  140. (define (cons x y)
  141.   (define (set-x! v) (set! x v))
  142.   (define (set-y! v) (set! y v))
  143.   (define (dispatch m)
  144.     (cond ((eq? m 'car) x)
  145.           ((eq? m 'cdr) y)
  146.           ((eq? m 'set-car!) set-x!)
  147.           ((eq? m 'set-cdr!) set-y!)
  148.           (else (error "Undefined operation -- CONS" m))))
  149.   dispatch)
  150.  
  151. (define (car z) (z 'car))
  152. (define (cdr z) (z 'cdr))
  153.  
  154. (define (set-car! z new-value)
  155.   ((z 'set-car!) new-value)
  156.   z)
  157.  
  158. (define (set-cdr! z new-value)
  159.   ((z 'set-cdr!) new-value)
  160.   z)
  161. ;; zad 2
  162. (define (make-cycle l)
  163.   (define (aux x)
  164.     (if (null? (mcdr x))
  165.         (set-mcdr! x l)
  166.         (aux (mcdr l))))
  167.   (aux l))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement