fork download
  1. (define (country-population/area this) (/ (this 'population) (this 'area )))
  2. (define (country-gdp/population this) (/ (this 'gdp ) (this 'population)))
  3. (define (country-area> this another ) (> (this 'area ) (another 'area )))
  4.  
  5. (define (new-country gdp area population)
  6. (letrec
  7. (
  8. (this.props
  9. (list
  10. (cons 'gdp gdp )
  11. (cons 'area area )
  12. (cons 'population population )
  13. (cons 'density country-population/area)
  14. (cons 'gdp-per-capita country-gdp/population )
  15. (cons 'is-bigger country-area> )
  16. )
  17. )
  18. (this
  19. (lambda (operation . args)
  20. (case operation
  21. ((set!)
  22. (let
  23. ((pair (assq (car args) this.props)))
  24. (if pair (set-cdr! pair (cadr args)))
  25. (car args)
  26. )
  27. )
  28. ((get)
  29. (let
  30. ((pair (assq (car args) this.props)))
  31. (if pair (cdr pair) (if (null? (cdr args)) #f (cadr args)))
  32. )
  33. )
  34. (else
  35. (set! operation (assq operation this.props))
  36. (if operation
  37. (begin
  38. (set! operation (cdr operation))
  39. (if (procedure? operation) (apply operation (cons this args)) operation)
  40. )
  41. "Invalid Option"
  42. )
  43. )
  44. )
  45. )
  46. )
  47. )
  48. this
  49. )
  50. )
  51.  
  52. (define make-country-mp new-country)
  53.  
  54. ; ------------------------------------------------------------------------------
  55.  
  56. (define Country-A (make-country-mp 1 10 20))
  57. (define Country-B (make-country-mp 2500 5000 50))
  58.  
  59. (display "gdp : ") (display (Country-B 'gdp )) (newline)
  60. (display "area : ") (display (Country-B 'area )) (newline)
  61. (display "population : ") (display (Country-B 'population )) (newline)
  62. (display "density : ") (display (Country-A 'density )) (newline)
  63. (display "gdp-per-capita: ") (display (Country-B 'gdp-per-capita )) (newline)
  64. (display "is-bigger : ") (display (Country-B 'is-bigger Country-A)) (newline)
  65. (newline)
  66. (display "get 'gdp : ") (display (Country-B 'get 'gdp )) (newline)
  67. (display "set! 'gdp 12 : ") (display (Country-B 'set! 'gdp 12)) (newline)
  68. (display "get 'gdp : ") (display (Country-B 'get 'gdp )) (newline)
  69. (display "get 'area : ") (display (Country-B 'get 'area )) (newline)
  70. (display "get 'density : ") (display (Country-B 'get 'density)) (newline)
  71. (display "get 'Joey : ") (display (Country-B 'get 'Joey )) (newline)
  72. (display "get 'Joey 'u : ") (display (Country-B 'get 'Joey 'u)) (newline)
Success #stdin #stdout 0.03s 14020KB
stdin
Standard input is empty
stdout
gdp           : 2500
area          : 5000
population    : 50
density       : 2
gdp-per-capita: 50
is-bigger     : #t

get 'gdp     : 2500
set! 'gdp 12 : gdp
get 'gdp     : 12
get 'area    : 5000
get 'density : #<procedure country-population/area (a)>
get 'Joey    : #f
get 'Joey 'u : u