Skip to content

Instantly share code, notes, and snippets.

@dharmatech
Created August 8, 2021 22:48
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 dharmatech/7a22ae98aa3026ed4dfbca433c4d5bd4 to your computer and use it in GitHub Desktop.
Save dharmatech/7a22ae98aa3026ed4dfbca433c4d5bd4 to your computer and use it in GitHub Desktop.
{-# LANGUAGE OverloadedRecordDot, OverloadedRecordUpdate, DuplicateRecordFields #-}
----------------------------------------------------------------------
data Point = Point { x :: Double, y :: Double, distance :: Point -> Double }
instance Show Point where
show p = "Point { x = " ++ show p.x ++ ", y = " ++ show p.y ++ " }"
point_distance :: Point -> Point -> Double
point_distance a b = sqrt ((b.x - a.x)^2 + (b.y - a.y)^2)
----------------------------------------------------------------------
make_point x y =
let
base = Point x y (\p -> 0.0)
in
Point base.x base.y (\other -> point_distance base other)
----------------------------------------------------------------------
a = make_point 0.0 0.0
b = make_point 1.0 1.0
c = make_point 3.0 3.0
ab = a.distance b
ac = a.distance c
bc = b.distance c
result_distance = (make_point 1.0 1.0).distance (make_point 10.0 10.0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment