-
Notifications
You must be signed in to change notification settings - Fork 0
/
NumberSequence.hs
53 lines (44 loc) · 1.6 KB
/
NumberSequence.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 NumberSequence where
import Data.List
import Data.Maybe
-- the sequence at pag. 80
figure :: [Int]
figure = [1,3,7,12,18,26,35,45,56]
-- | Given a sequence such constructed as figure, return the numbers already
-- skipped and the numbers to skip
skip :: [Int] -> ([Int],[Int])
skip figure = (skipped, toSkip)
where
figure' = background figure
lastSkipped = last figure - last (init figure)
toSkip = dropWhile (<= lastSkipped) figure'
skipped = takeWhile (<= lastSkipped) figure'
-- | Given a sequence constructed as figure, return the numbers already
-- skipped
skipped :: [Int] -> [Int]
skipped = fst . skip
-- | Given a sequence constructed as figure, return the numbers to skip
toSkip :: [Int] -> [Int]
toSkip = snd . skip
-- | Numbers not in a sequence ("background" of the "figure")
background :: [Int] -> [Int]
background figure = [1..(last figure)] \\ figure
-- | Given a sequence such as figure, return the next number, unless a
-- decision is required
next :: [Int] -> Maybe Int
next figure = if null $ toSkip figure
then Nothing
else Just $ last figure + head (toSkip figure)
-- | Given a sequence such as figure, continue it until possible
-- (tends to be infinite ;)
continue :: [Int] -> [Int]
continue figure = case next figure of
(Just n) -> continue (figure ++ [n])
Nothing -> figure
-- | Given a sequence such as figure, continue it with its m successors,
-- if possible
continue' :: [Int] -> Int -> [Int]
continue' figure 0 = figure
continue' figure m = case next figure of
(Just n) -> continue' (figure ++ [n]) (m - 1)
Nothing -> figure