PasteRack.org
Paste # 83691
2017-08-11 15:04:52

Fork as a new paste.

Paste viewed 133 times.


Embed:

  1. #lang racket
  2.  
  3. Consider make-point, a coordinate pair object that has the following capabilities:
  4. ;; create a point object and bind it to symbol 'mypoint
  5. (define mypoint (make-point 10 20))
  6. ;; point object accepts message 'x and returns the x value
  7. (mypoint 'x)  10
  8. ;; point object accepts message 'y and returns the y value
  9. (mypoint 'y)  20
  10. ;; point object accepts message 'shift and returns procedure
  11. ;; which offsets x and y values by corresponding amounts
  12. ((mypoint 'shift) 10 10)
  13. ;; x coordinate is now 20, and y coordinate is now 30
  14. (mypoint 'x)   20
  15. (mypoint 'y)  30
  16. Given this spec, write a definition for make-point.
  17.  
  18.  
  19. ;I think my third cond is wrong because it doesn't do anything to num1 or num2
  20. ;it just returns a procedure but doesn't actually make any changes
  21. ;(define (make-point num1 num2)
  22.  ; (lambda (symbol)
  23.   ;(cond
  24.    ; [(eq? symbol 'x) num1]
  25.     ;[(eq? symbol 'y) num2]
  26.     ;[(eq? symbol 'shift) (lambda (incx incy)
  27.      ;                      (make-point (+ incx num1) (+ incy num2)))])))
  28.  
  29.  
  30. (define (make-point num1 num2)
  31.   (lambda (symbol)
  32.   (cond
  33.     [(eq? symbol 'x) num1]
  34.     [(eq? symbol 'y) num2]
  35.     [(eq? symbol 'shift) (lambda (incx incy)
  36.                            (set! num1 (+ incx num1))
  37.                            (set! num2 (+ incy num2)))])))

=>