Last active
December 25, 2015 12:29
-
-
Save funatsufumiya/6977080 to your computer and use it in GitHub Desktop.
deferences of class definition and usage
This file contains 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
(defprotocol Metric (distance [p1 p2])) | |
(deftype Point3D [x y z] Metric | |
(distance [p1 p2] | |
(let [dx (- (.x p1) (.x p2)) | |
dy (- (.y p1) (.y p2)) | |
dz (- (.z p1) (.z p2))] | |
(Math/sqrt (+ (* dx dx) (* dy dy) (* dz dz)))))) | |
(def p1 (Point3D. 0 0 0)) | |
(def p2 (Point3D. 10 10 10)) | |
(distance p1 p2) |
This file contains 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
(defclass point3d () | |
((x :accessor get-x :initform 0 :initarg :x) | |
(y :accessor get-y :initform 0 :initarg :y) | |
(z :accessor get-z :initform 0 :initarg :z))) | |
(defmethod distance ((p1 point3d) (p2 point3d)) | |
(let ((dx (- (get-x p1) (get-x p2))) | |
(dy (- (get-y p1) (get-y p2))) | |
(dz (- (get-z p1) (get-z p2)))) | |
(sqrt (+ (* dx dx) (* dy dy) (* dz dz))))) | |
(setq p1 (make-instance 'point3d)) | |
(setq p2 (make-instance 'point3d :x 10 :y 10 :z 10)) | |
(distance p1 p2) |
This file contains 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
class point3d (x:float) (y:float) (z:float) = object | |
val mutable x : float = x | |
val mutable y : float = y | |
val mutable z : float = z | |
method get_x = x | |
method get_y = y | |
method get_z = z | |
method set_x v = x <- v | |
method set_y v = y <- v | |
method set_z v = z <- v | |
method distance (p:point3d) = | |
let dx = x -. p#get_x in | |
let dy = y -. p#get_y in | |
let dz = z -. p#get_z in | |
sqrt (dx*.dx +. dy*.dy +. dz*.dz) | |
end;; | |
let p1 = new point3d 0. 0. 0.;; | |
let p2 = new point3d 10. 10. 10.;; | |
p1#distance p2;; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment