Skip to content

Instantly share code, notes, and snippets.

@jeapostrophe
Created March 8, 2019 14:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jeapostrophe/76833b8a0d363e1394431c65967b090a to your computer and use it in GitHub Desktop.
Save jeapostrophe/76833b8a0d363e1394431c65967b090a to your computer and use it in GitHub Desktop.
#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