Skip to content

Instantly share code, notes, and snippets.

@danilomo
Last active May 24, 2017 14:30
Show Gist options
  • Save danilomo/0cd8226ed0b8cb45a6305df020b0d412 to your computer and use it in GitHub Desktop.
Save danilomo/0cd8226ed0b8cb45a6305df020b0d412 to your computer and use it in GitHub Desktop.
Find the closest vector in a list to a given vector v
type Vector3D = (Float, Float, Float)
arr :: [ Vector3D ]
arr = [ ( 35, 30, 39 ),( 20, 12, 5 ) ]
tp :: Vector3D
tp = (19,13,3)
dist:: Vector3D -> Vector3D -> Float
dist (x1, y1, z1) (x2, y2, z2 ) = abs(x1-x2) + abs(y1-y2) + abs(z1-z2)
euclidDist:: Vector3D -> Vector3D -> Float
euclidDist (x1, y1, z1) (x2, y2, z2 ) = sqrt( (x1-x2)^2 + (y1-y2)^2 + (z1-z2)^2 )
proximities:: [Vector3D] -> Vector3D -> (Vector3D->Vector3D->Float) -> [ Float ]
proximities l v d = map (d v) l
array_proximity:: [Vector3D] -> Vector3D -> (Vector3D->Vector3D->Float) -> (Float, Int)
array_proximity l v d = minimum (zip (proximities l v d) [0..])
-- *Main> array_proximity arr tp dist
-- (4.0,1)
-- *Main> array_proximity arr tp euclidDist
-- (2.4494898,1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment