25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 
 
 
 

82 satır
2.0 KiB

  1. (defun type-wrap (obj type)
  2. (set-type obj type)
  3. obj)
  4. (define-syntax defclass (name members :rest body)
  5. "Macro for creatating classes."
  6. (defun underscore (sym)
  7. (string->symbol (concat-strings "_" (symbol->string sym))))
  8. (define underscored-members (map underscore members))
  9. ;; the wrapping let environment
  10. (define let-body (list 'let (zip members underscored-members)))
  11. ;; the body
  12. (map (lambda (fun) (append let-body fun)) body)
  13. ;; the dispatch function
  14. (append let-body '(special-lambda (message :rest args)
  15. "This is the docs for the handle"
  16. (eval (extend (list message) args))))
  17. ;; stuff it all in the constructor function
  18. (eval (list defun (string->symbol (concat-strings "make-" (symbol->string name))) underscored-members
  19. "This is the handle to an object of the class "
  20. let-body)))
  21. ;; (v1 print)
  22. ;; (v1 length)
  23. ;; (v1 get-x)
  24. ;; (v1 set-x 10)
  25. (defclass vector3 (x y z)
  26. (defun get-x () x)
  27. (defun get-y () y)
  28. (defun get-z () z)
  29. (defun set-x (new-x) (mutate x new-x))
  30. (defun set-y (new-y) (mutate y new-y))
  31. (defun set-z (new-z) (mutate z new-z))
  32. (defun length ()
  33. (** (+ (* x x) (* y y) (* z z)) 0.5))
  34. (defun scale (fac)
  35. (mutate x (* fac x))
  36. (mutate y (* fac y))
  37. (mutate z (* fac z))
  38. fac)
  39. (defun add (other)
  40. (make-vector3
  41. (+ x (other get-x))
  42. (+ y (other get-y))
  43. (+ z (other get-z))))
  44. (defun subtract (other)
  45. (make-vector3
  46. (- x (other get-x))
  47. (- y (other get-y))
  48. (- z (other get-z))))
  49. (defun scalar-product (other)
  50. (+ (* x (other get-x))
  51. (* y (other get-y))
  52. (* z (other get-z))))
  53. (defun cross-product (other)
  54. (make-vector3
  55. (- (* y (other get-z)) (* z (other get-y)))
  56. (- (* z (other get-x)) (* x (other get-z)))
  57. (- (* x (other get-y)) (* y (other get-x)))))
  58. (defun printout ()
  59. (printf "[vector3] (" x y z ")"))
  60. )
  61. (define v1 (make-vector3 1 2 3))
  62. (define v2 (make-vector3 3 2 1))
  63. (assert (= (v1 scalar-product v2) 10))