Advertisement
Guest User

Untitled

a guest
Nov 1st, 2014
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import Data.List;
  2.  
  3. closest :: [(Float, Float)] -> Float
  4. closest l = closest' $ sortBy (\a b -> (compare  (fst a) (fst b))) l
  5.    where
  6.    closest' []  = 10.0**30.0;
  7.     closest' [a] = 10.0**30.0;
  8.    closest' [a,b]  = dist a b
  9.     closest' s =
  10.        let h   = splitAt ((length s) `div` 2) s
  11.            d1  = closest' (fst h)
  12.             d2  = closest' (snd h)
  13.            md  = min  d1 d2
  14.            xp  = fst $ head $ (snd h)
  15.            t   = sortBy  (\a b -> (compare (snd a) (snd b))) [x | x<-s, (abs  ((fst x)-xp)) < md]
  16.            d3  = comp6 t
  17.        in (min md d3)
  18.        where
  19.            comp6 []     = 10.0**30.0;
  20.            comp6 (x:xs) = min (comp6' x (take 6 xs)) (comp6 xs)
  21.                 where
  22.                     comp6' p [] = 10.0**30.0;
  23.                    comp6' p (x:xs) = (min (dist p x) (comp6' p xs))
  24.                
  25. dist a b = sqrt  ( ((fst a) - (fst b))**2.0 + ((snd a) - (snd b))**2.0)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement