PasteRack.org
Paste # 13500
2017-08-11 14:26:59

Fork as a new paste.

Paste viewed 136 times.


Embed:

  1. #lang racket
  2.  
  3. In your first code:
  4.  
  5. (define balance 0)
  6.  
  7. (define (deposit n)
  8.   (begin
  9.     (set! balance (+ n balance))
  10.     balance))
  11.  
  12. This is perfectly fine if you are not using object-oriented techniques:
  13. You've defined balance as a symbol in the global environment (which is bound to a number)
  14. deposit is a procedure that modifies that global binding
  15. In your second code, you are effectively doing the same thing—using a global binding for the state variable:
  16.  
  17.  
  18. (define counter 0)
  19.  
  20. (define (make-counter)
  21.   (begin
  22.     (set! counter(+ counter 1)) counter))
  23.  
  24. So neither of these uses OO approaches.

=>