-
Notifications
You must be signed in to change notification settings - Fork 0
/
SeqList_copy.hs
97 lines (77 loc) · 2.23 KB
/
SeqList_copy.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import Par
import Seq
--TABULATE
tabulateL :: (Int -> a) -> Int -> [a]
tabulateL f n
| n == 0 = [f 0]
| n > 0 = let (a,b) = f n ||| (tabulateL f (n-1))
in appendS b [a]
--MAP
mapL :: (a -> b) -> [a] -> [b]
mapL _ [] = []
mapL f (x:xs) = let (a,b) = f x ||| (map f xs)
in a:b
--FILTER
filterL :: (a -> Bool) -> [a] -> [a]
filterL _ [] = []
filterL f (x:xs) = let (a,b) = f x ||| (filter f xs)
in if a then x:b else b
--SHOWT
showtL :: [a] -> TreeView a [a]
showtL [] = EMPTY
showtL [a] = ELT a
showtL xs = let
n = quot (lengthS xs) 2
(l, r) = (takeS xs n) ||| (dropS xs n)
in NODE l r
--CONCAT
concatL :: [[a]] -> [a]
concatL [] = []
concatL [xs] = xs
concatL (xs:ys:zss) = let (a,b) = (appendS xs ys) ||| (concatL zss)
in appendS a b
--REDUCE
reduceL :: (a -> a -> a) -> a -> [a] -> a
reduceL _ n [] = n
reduceL f n xs = f n (reduceL' f xs)
contraer :: (a -> a -> a) -> [a] -> [a]
contraer _ [] = []
contraer _ [x] = [x]
contraer f (x:y:xs) = let (a,b) = (f x y) ||| (contraer f xs)
in a:b
reduceL' :: (a -> a -> a) -> [a] -> a
reduceL' _ [x] = x
reduceL' f [x,y] = f x y
reduceL' f xs = reduceL' f (contraer f xs)
--SCAN
expandir :: (a -> a -> a) -> [a] -> [a] -> [a]
expandir _ [] [] = []
expandir _ [_] zs = zs
expandir f (x:_:xs) (z:zs) = let (a,b) = (f z x) ||| (expandir f xs zs)
in z:a:b
scanL :: (a -> a -> a) -> a -> [a] -> ([a], a)
scanL f n xs = (scanL' f n xs) ||| (reduceS f n xs)
scanL' :: (a -> a -> a) -> a -> [a] -> [a]
scanL' _ n [_] = [n]
scanL' f n xs = expandir f xs (scanL' f n (contraer f xs))
--SHOWL
showlL :: [a] -> ListView a [a]
showlL [] = NIL
showlL (x:xs) = CONS x xs
instance Seq [] where
emptyS = []
singletonS x = [x]
lengthS = length
nthS = (!!)
tabulateS = tabulateL
mapS = mapL
filterS = filterL
appendS = (++)
takeS xs n = take n xs
dropS xs n = drop n xs
showtS = showtL
showlS = showlL
joinS = concatL
reduceS = reduceL
scanS = scanL
fromList = id