Upcoming SlideShare
×

# Examples for loopless

228 views
179 views

Published on

Feb 01, 2013

0 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

• Be the first to like this

Views
Total views
228
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
0
0
Likes
0
Embeds 0
No embeds

No notes for slide

### Examples for loopless

1. 1. Looplessness Yun-Yan Chi IIS, Academia Sinica Feb 01, 2013
2. 2. outline T e x t• unfold• loopless
3. 3. u n unfoldr f o l d structureunfold fold Value
4. 4. u n unfoldr f o l dseed seed seed seed ... element element element [ a , b , c ...
5. 5. u n unfoldr f o l d seed new list seed element seed> unfoldr :: (a -> Maybe (b,a)) -> a -> [b]> unfoldr g a = case g a of> Nothing -> []> Just (b,a) -> b : unfoldr g a
6. 6. u n unfoldr f o l d> uncons :: [a] -> Maybe (a, [a])> uncons [] = Nothing> uncons (x:xs) = Just (x,xs)> id’ = unfoldr uncons> app :: (a -> b) -> [a] -> Maybe (b, [a])> app f [] = Nothing> app f (x:xs) = Just (f x,xs)> map’ f = unfoldr (app f)
7. 7. L o o loopless p l e s s [ abcde,abcde bacde, permutation bcade, bcdae, bcdea, .....]
8. 8. L o o loopless p l e s s g [ pattern1, f0structure pattern2, f1 pattern3, f2 pattern4, f3 .....]
9. 9. L o o loopless p l e s s O(n) O(1) g [ pattern1, f0structure pattern2, f1 pattern3, f2 pattern4, f3 .....]
10. 10. L o o loopless p l e s s> loopless :: (c -> a) ->> (a -> Maybe (b,a)) ->> c -> [b]> loopless prolog step = unfoldr step . prolog O(1) O(n)
11. 11. L o o Example p l e s s> id’ :: [a] -> [a]> id’ = unfoldr uncons . id> reverse’ :: [a] -> [a]> reverse’ = unfoldr uncons . rv> where rv = foldl (flip (:)) []
12. 12. L o o Example (concat) p l e s s> cat :: [[a]] -> [a]> cat = loopless prolog uncons> where prolog [] = []> prolog (xs:xss) = xs ++ prolog xss> cat :: [[a]] -> [a]> cat = loopless (filter (not.null)) step> where step [] = Nothing> step ((x:[]):zs) = Just (x, zs)> step ((x:xs):zs) = Just (x, xs : zs)
13. 13. L o o Example (RoseT) p l e s s Preorder Traversal> type Forest a = [Rose a]> data Rose a = Rose a (Forest a) a a [ , , ..... ] subtree subtree
14. 14. L o o Example (RoseT) p l e s s Preorder Traversal> type Forest a = [Rose a]> data Rose a = Rose a (Forest a) a a [ , ..... ] forest subtree
15. 15. L o o Example (RoseT) p l e s s Preorder Traversal> type Forest a = [Rose a]> data Rose a = Rose a (Forest a) a a ++ [ , ..... ] forest subtree
16. 16. L o o Example (RoseT) p l e s s Preorder Traversal> type Forest a = [Rose a]> data Rose a = Rose a (Forest a)> pre :: Forest a -> [a]> pre [] = []> pre (Rose a ts : rs) = a : pre (ts ++ rs)
17. 17. L o o Example (RoseT) p l e s s Preorder Traversal> type Forest a = [Rose a]> data Rose a = Rose a (Forest a)> preorder = loopless id step> where step :: Forest a ->> Maybe (a, Forest a)> step [] = Nothing> step (Rose a ts : rs) => Just (a, ts ++ rs)
18. 18. L o o Example (RoseT) p l e s s Preorder Traversal> type Forest a = [Rose a]> data Rose a = Rose a (Forest a)> (+:) xs xss = if null xs then xss else xs : xss> preorder = loopless (a->[a]) step where> step :: [Forest a] -> Maybe (a, [Forest a])> step [] = Nothing> step ((Rose a ts : rs) : fs) => Just (a, ts +: rs +: fs)
19. 19. L o o Example (BinT) p l e s s Inorder Traversal> data Bin a = Nil> | Bin a (Bin a) (Bin a) a l r
20. 20. L o oExample (BinT) p l e s s Inorder Traversal a a [ subtree , ... ] l r = Inorder PreorderTraversal Traversal
21. 21. L o oExample (BinT) p l e s s Inorder Traversal + 1 +1 - = 2 - 2 3 3
22. 22. L o oExample (BinT) p l e s s Inorder Traversal + 1 +1 - 2 - 2 3 3 N EL S PI
23. 23. L o o Example (BinT) p l e s s Inorder Traversal + 1 - + 99 ! 7 - * - * 50 13 41 ! - 4 99 50 7 13 N EL S PI
24. 24. L o o Example (BinT) p l e s s Inorder Traversal + 1 - + 99 ! 7 - * - * 50 13 41 ! - 4 99 50 7 13 N EL S PI
25. 25. L o o Example (BinT) p l e s s Inorder Traversal + 1 - + 99 ! 7 - * - * 50 13 41 ! - 4 99 50 7 13 N EL S PI
26. 26. L o o Example (BinT) p l e s s Inorder Traversal> data Bin a = Nil> | Bin a (Bin a) (Bin a)> spinel :: Bin a -> Forest a> spinel Nil = []> spinel (Bin a l r) => (spinel l) ++ [Rose a (spinel r)]
27. 27. L o o Example (BinT) p l e s s Inorder Traversal> data Bin a = Nil> | Bin a (Bin a) (Bin a)> spinel :: Bin a -> Forest a> spinel Nil = []> spinel (Bin a l r) => (spinel l) ++ [Rose a (spinel r)]> Rose a (spinel r) : (spinel l)
28. 28. L o o Example (BinT) p l e s s Inorder Traversal> data Bin a = Nil> | Bin a (Bin a) (Bin a)> spinel :: Bin a -> Forest a> spinel Nil = []> spinel (Bin a l r) => Rose a (spinel r) : (spinel l)> inorder = preorder . reverse . spines
29. 29. 0 END• unfold THANK YOU• loopless • Concat • Preorder Traversal (RoseT) • Inorder Traversal (BinT)