-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAlgorithm.hs
53 lines (46 loc) · 1.7 KB
/
Algorithm.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
module Algorithm (
isSetCode,
isListCode
) where
import qualified Set
isPrefixOf :: (Eq a) => [a] -> [a] -> Bool
isPrefixOf prefix word =
isPartOf prefix prefixList
where
prefixList = scanl (\acc x -> acc ++ [x]) [] word
isPartOf _ [] = False
isPartOf x (y:ys) = x == y || isPartOf x ys
deletePrefixIn :: (Eq a) => [a] -> [a] -> [a]
deletePrefixIn _ [] = []
deletePrefixIn [] word = word
deletePrefixIn (x:xs) (y:ys) =
if x == y
then deletePrefixIn xs ys
else []
-- A^-1 B
findCuttedSufixesIn :: (Ord a) => Set.Set [a] -> Set.Set [a] -> Set.Set [a]
findCuttedSufixesIn setL setR
| Set.null setL = Set.empty
| otherwise = Set.union (findCuttedSufixesIn rest setR) (Set.map (deletePrefixIn x) (Set.filter (isPrefixOf x) setR))
where
x = Set.findMax setL
rest = Set.deleteMax setL
calculateNSet :: (Ord a) => Set.Set [a] -> Set.Set [a] -> Set.Set [a]
calculateNSet set setPrev =
Set.union (findCuttedSufixesIn set setPrev) (findCuttedSufixesIn setPrev set)
isSetCode :: (Ord a) => Set.Set [a] -> Bool
isSetCode set = check first setOfFirst
where
first = Set.delete [] (calculateNSet set set)
setOfFirst = Set.insert first Set.empty
check setI previousSets
| Set.member [] setI = False
| Set.null setIPlus = True
| Set.member setIPlus previousSets = True
| otherwise = check setIPlus (Set.insert setIPlus previousSets)
where
setIPlus = calculateNSet set setI
isListCode :: (Ord a) => [[a]] -> Bool
isListCode list = areDuplicatesIn && isSetCode (Set.fromList list)
where
areDuplicatesIn = Set.size (Set.fromList list) == length list