Mercurial > hg > Members > atton > haskell > reflection_without_remorse
annotate diffList.hs @ 2:eccc7eced616 default tip
Add foldr style diffList
author | Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp> |
---|---|
date | Sat, 05 Jul 2014 19:08:19 +0900 |
parents | 0251da3f04f2 |
children |
rev | line source |
---|---|
0 | 1 -- Reflection without Remorse |
2 -- http://homepages.cwi.nl/~ploeg/papers/zseq.pdf | |
3 -- https://github.com/atzeus/reflectionwithoutremorse | |
4 | |
5 -- try implement diff list | |
6 | |
7 type DiffList a = [a] -> [a] | |
8 | |
9 absFromDiffList :: DiffList a -> [a] -- original name = abs | |
10 absFromDiffList a = a [] | |
11 | |
12 rep :: [a] -> DiffList a | |
13 rep = (++) | |
14 | |
15 (+++) :: DiffList a -> DiffList a -> DiffList a | |
16 (+++) = (.) | |
17 | |
18 list = replicate 10000 'a' | |
19 | |
20 normalList = replicate 1000 list -- [l1, list2, list3, ..., listn] | |
21 diffList = map rep normalList -- [(++ list1), (++ list2), (++ list3), ..., (++ listn)] | |
22 | |
1 | 23 normalListLeftLength = length $ foldl1 (++) normalList |
0 | 24 -- length $ list1 ++ list2 ++ list3 ++ ... ++ listn |
25 -- length $ ((list1 ++ list2) ++ list3) ++ ... ++ listn -- left associated | |
1 | 26 -- |
27 normalListRightLength = length $ foldr1 (++) normalList | |
28 -- length $ list1 ++ list2 ++ list3 ++ ... ++ listn | |
29 -- length $ (list1 ++ (list2 ++ (list3 ++ ... ++ listn))) -- right associated | |
0 | 30 |
1 | 31 diffListLeftLength = length $ absFromDiffList $ foldl1 (+++) diffList |
0 | 32 -- length $ absFromDiffList $ (++ list1) +++ (++ list2) +++ (++ list3) +++ ... +++ (++ listn) |
1 | 33 -- length $ absFromDiffList $ (((++ list1) +++ (++ list2)) +++ (++ list3)) +++ ... +++ (++ listn) |
2
eccc7eced616
Add foldr style diffList
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
1
diff
changeset
|
34 -- length $ absFromDiffList $ ((((++ list1) . (++ list2)) . (++ list3)) . ... . (++ listn) -- (++ list1)) = \x -> list1 ++ x |
0 | 35 -- length $ absFromDiffList $ (++ (list1 ++ (list2 ++ (list3 ++ (...))))) |
36 -- length $ (++ (list1 ++ (list2 ++ (list3 ++ (...))))) [] | |
37 -- length $ (list1 ++ (list2 ++ (list3 ++ (...)))) ++ [] | |
2
eccc7eced616
Add foldr style diffList
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
1
diff
changeset
|
38 -- length $ ((list1 ++ (list2 ++ (list3 ++ (...)))) ++ []) -- right associated |
eccc7eced616
Add foldr style diffList
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
1
diff
changeset
|
39 |
eccc7eced616
Add foldr style diffList
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
1
diff
changeset
|
40 diffListRightLength = length $ absFromDiffList $ foldr1 (+++) diffList |
eccc7eced616
Add foldr style diffList
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
1
diff
changeset
|
41 -- length $ absFromDiffList $ (++ list1) +++ (++ list2) +++ (++ list3) +++ ... +++ (++ listn) |
eccc7eced616
Add foldr style diffList
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
1
diff
changeset
|
42 -- length $ absFromDiffList $ ((++ list1) +++ ((++ list2) +++ ((++ list3) +++ ... +++ (++ listn)))) |
eccc7eced616
Add foldr style diffList
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
1
diff
changeset
|
43 -- length $ absFromDiffList $ ((++ list1) . ((++ list2) . ((++ list3) . ... . (++ listn)))) |
eccc7eced616
Add foldr style diffList
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
1
diff
changeset
|
44 -- length $ absFromDiffList $ (++ (list1 ++ (list2 ++ (list3 ++ (...))))) |
eccc7eced616
Add foldr style diffList
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
1
diff
changeset
|
45 -- length $ (++ (list1 ++ (list2 ++ (list3 ++ (...))))) [] |
eccc7eced616
Add foldr style diffList
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
1
diff
changeset
|
46 -- length $ ((list1 ++ (list2 ++ (list3 ++ (...)))) ++ []) -- right associated |
1 | 47 |
48 | |
49 -- normalListRightLength -- ok | |
50 -- normalListLeftLength -- slowly | |
51 -- diffListLeftLength -- ok | |
2
eccc7eced616
Add foldr style diffList
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
1
diff
changeset
|
52 -- diffListRightLength -- ok |