-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsrc.hs
38 lines (31 loc) · 1.11 KB
/
src.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import Data.List
import System.Random
data Nucleotide = A | C | G | T deriving (Eq, Show, Enum, Bounded)
type Sequence = [Nucleotide]
type DataPoints = [Sequence]
type Cluster = [Sequence]
type Clustering = [Cluster]
instance Random Nucleotide where
random = randomR (minBound :: Nucleotide, maxBound)
randomR (min, max) g = (toEnum v, g')
where (v, g') = randomR (fromEnum min, fromEnum max) g
generateSequence :: RandomGen g => g -> Int -> Sequence
generateSequence g sz = take sz $ randoms g
generateDataPoints :: RandomGen g => g -> Int -> Int -> DataPoints
generateDataPoints g n sz = take n $ map (flip generateSequence sz) generators
where
generators = map mkStdGen $ randoms g
distance :: Sequence -> Sequence -> Double
distance [] [] = 0
distance [] _ = 1
distance _ [] = 1
distance (x:xs) (y:ys)
| x == y = distance xs ys / 2
| otherwise = 1
cluster :: Double -> DataPoints -> Clustering
cluster _ [] = []
cluster a (p:pts) = (p:cls) : cluster a other
where
(cls, other) = partition (\x -> distance p x <= a) pts
-- From ghci, call with
-- > cluster 0.5 (generateDataPoints (mkStdGen 42) 100 10)