Created
March 8, 2019 14:40
-
-
Save jeapostrophe/76833b8a0d363e1394431c65967b090a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#lang racket/base | |
(require racket/match | |
racket/math) | |
(let () | |
(struct get-x ()) | |
(struct get-y ()) | |
(struct distance-from-origin ()) | |
(struct set-x (nx)) | |
(struct set-y (ny)) | |
(define (make-posn-object x y) | |
(λ (method) | |
(match method | |
[(get-x) x] | |
[(get-y) y] | |
[(set-x nx) (set! x nx)] | |
[(set-y ny) (set! y ny)] | |
[(distance-from-origin) | |
(sqrt (+ (sqr x) (sqr y)))]))) | |
(define o (make-posn-object 3 4)) | |
(vector (o (get-x)) | |
(o (get-y)) | |
(o (distance-from-origin)) | |
(o (set-x 10)) | |
(o (distance-from-origin)))) | |
(let () | |
(define-syntax-rule | |
(define-class (class-name init-field ...) | |
[(method method-args ...) method-body] ...) | |
(begin | |
(struct method (method-args ...)) ... | |
(define (class-name init-field ...) | |
(λ (call) | |
(match call | |
[(method method-args ...) method-body] ...))))) | |
(define-class (make-posn-object x y) | |
[(get-x) x] | |
[(get-y) y] | |
[(set-x nx) (set! x nx)] | |
[(set-y ny) (set! y ny)] | |
[(distance-from-origin) (sqrt (+ (sqr x) (sqr y)))]) | |
(define o (make-posn-object 3 4)) | |
(vector (o (get-x)) | |
(o (get-y)) | |
(o (distance-from-origin)) | |
(o (set-x 10)) | |
(o (distance-from-origin)))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment