• Save
Examples for loopless
Upcoming SlideShare
Loading in...5
×

Like this? Share it with your network

Share

Examples for loopless

  • 272 views
Uploaded on

Feb 01, 2013

Feb 01, 2013

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
    Be the first to like this
No Downloads

Views

Total Views
272
On Slideshare
272
From Embeds
0
Number of Embeds
0

Actions

Shares
Downloads
0
Comments
0
Likes
0

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. Looplessness Yun-Yan Chi IIS, Academia Sinica Feb 01, 2013
  • 2. outline T e x t• unfold• loopless
  • 3. u n unfoldr f o l d structureunfold fold Value
  • 4. u n unfoldr f o l dseed seed seed seed ... element element element [ a , b , c ...
  • 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. 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. L o o loopless p l e s s [ abcde,abcde bacde, permutation bcade, bcdae, bcdea, .....]
  • 8. L o o loopless p l e s s g [ pattern1, f0structure pattern2, f1 pattern3, f2 pattern4, f3 .....]
  • 9. L o o loopless p l e s s O(n) O(1) g [ pattern1, f0structure pattern2, f1 pattern3, f2 pattern4, f3 .....]
  • 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. L o oExample (BinT) p l e s s Inorder Traversal a a [ subtree , ... ] l r = Inorder PreorderTraversal Traversal
  • 21. L o oExample (BinT) p l e s s Inorder Traversal + 1 +1 - = 2 - 2 3 3
  • 22. L o oExample (BinT) p l e s s Inorder Traversal + 1 +1 - 2 - 2 3 3 N EL S PI
  • 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. 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. 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. 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. 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. 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. 0 END• unfold THANK YOU• loopless • Concat • Preorder Traversal (RoseT) • Inorder Traversal (BinT)