SlideShare une entreprise Scribd logo
1  sur  29
Looplessness
   Yun-Yan Chi
   IIS, Academia Sinica




   Feb 01, 2013
outline
                   T
                   e
                   x
                   t




•   unfold
•   loopless
u
                        n


   unfoldr
                        f
                        o
                        l
                        d




     structure



unfold           fold

         Value
u
                                                   n


        unfoldr
                                                   f
                                                   o
                                                   l
                                                   d




seed   seed          seed          seed      ...
       element       element       element




  [    a         ,     b       ,      c      ...
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'
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)
L
                              o
                              o


      loopless
                              p
                              l
                              e
                              s
                              s




                 [ abcde,
abcde              bacde,
      permutation bcade,
                   bcdae,
                   bcdea,
                     .....]
L
                                      o
                                      o


       loopless
                                      p
                                      l
                                      e
                                      s
                                      s




            g   [ pattern1,      f0
structure
                  pattern2,
                                 f1
                  pattern3,
                                 f2
                  pattern4,
                                 f3
                        .....]
L
                                         o
                                         o


       loopless
                                         p
                                         l
                                         e
                                         s
                                         s




            O(n)                O(1)

             g     [ pattern1,      f0
structure
                     pattern2,
                                    f1
                     pattern3,
                                    f2
                     pattern4,
                                    f3
                           .....]
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)
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 (:)) []
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)
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
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
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
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)
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)
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)
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
L
                                          o
                                          o


Example (BinT)
                                          p
                                          l
                                          e
                                          s
                                          s


     Inorder Traversal



     a                   a

                 [    subtree   , ... ]
 l       r
             =
 Inorder             Preorder
Traversal            Traversal
L
                                      o
                                      o


Example (BinT)
                                      p
                                      l
                                      e
                                      s
                                      s


        Inorder Traversal



    +                 1       +

1         -       =       2       -

    2         3                   3
L
                                              o
                                              o


Example (BinT)
                                              p
                                              l
                                              e
                                              s
                                              s


        Inorder Traversal



    +               1       +

1         -             2       -

    2         3                 3

                                           N EL
                                    S PI
L
                                                                            o
                                                                            o


         Example (BinT)
                                                                            p
                                                                            l
                                                                            e
                                                                            s
                                                                            s


                  Inorder Traversal


                  +                    1        -            +

                                           99       !    7   -      *

    -                         *
                                                    50       13     4
1        !                -        4
    99       50       7       13


                                                                         N EL
                                                                  S PI
L
                                                                            o
                                                                            o


         Example (BinT)
                                                                            p
                                                                            l
                                                                            e
                                                                            s
                                                                            s


                  Inorder Traversal


                  +                    1        -            +

                                           99       !    7   -      *

    -                         *
                                                    50       13     4
1        !                -        4
    99       50       7       13


                                                                         N EL
                                                                  S PI
L
                                                                            o
                                                                            o


         Example (BinT)
                                                                            p
                                                                            l
                                                                            e
                                                                            s
                                                                            s


                  Inorder Traversal


                  +                    1        -            +

                                           99       !    7   -      *

    -                         *
                                                    50       13     4
1        !                -        4
    99       50       7       13


                                                                         N EL
                                                                  S PI
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)]
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)
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
0
                                    END
•   unfold                         THANK YOU


•   loopless
    •   Concat
    •   Preorder Traversal (RoseT)
    •   Inorder Traversal (BinT)

Contenu connexe

Tendances (8)

SERA Email 2.5.03
SERA Email 2.5.03SERA Email 2.5.03
SERA Email 2.5.03
 
Palavras Cruzadas sobre homonimos1 respostas
Palavras Cruzadas sobre homonimos1 respostasPalavras Cruzadas sobre homonimos1 respostas
Palavras Cruzadas sobre homonimos1 respostas
 
Numeros en mandarin
Numeros en mandarinNumeros en mandarin
Numeros en mandarin
 
Vocabulary notebook
Vocabulary notebookVocabulary notebook
Vocabulary notebook
 
Doc. 114
Doc. 114Doc. 114
Doc. 114
 
Hfc c he example of hfc
Hfc c he example of hfcHfc c he example of hfc
Hfc c he example of hfc
 
Group y3
Group y3Group y3
Group y3
 
Doc.70
Doc.70Doc.70
Doc.70
 

En vedette

Program Language - Fall 2013
Program Language - Fall 2013 Program Language - Fall 2013
Program Language - Fall 2013 Yun-Yan Chi
 
for "Parallelizing Multiple Group-by Queries using MapReduce"
for "Parallelizing Multiple Group-by Queries using MapReduce"for "Parallelizing Multiple Group-by Queries using MapReduce"
for "Parallelizing Multiple Group-by Queries using MapReduce"Yun-Yan Chi
 
Genetic programming
Genetic programmingGenetic programming
Genetic programmingYun-Yan Chi
 
Semantic Genetic Programming Tutorial
Semantic Genetic Programming TutorialSemantic Genetic Programming Tutorial
Semantic Genetic Programming TutorialAlbertoMoraglio
 
Machine X Language
Machine X LanguageMachine X Language
Machine X LanguageYun-Yan Chi
 

En vedette (7)

Program Language - Fall 2013
Program Language - Fall 2013 Program Language - Fall 2013
Program Language - Fall 2013
 
for "Parallelizing Multiple Group-by Queries using MapReduce"
for "Parallelizing Multiple Group-by Queries using MapReduce"for "Parallelizing Multiple Group-by Queries using MapReduce"
for "Parallelizing Multiple Group-by Queries using MapReduce"
 
Any tutor
Any tutorAny tutor
Any tutor
 
Genetic programming
Genetic programmingGenetic programming
Genetic programming
 
Insert 2 Merge
Insert 2 MergeInsert 2 Merge
Insert 2 Merge
 
Semantic Genetic Programming Tutorial
Semantic Genetic Programming TutorialSemantic Genetic Programming Tutorial
Semantic Genetic Programming Tutorial
 
Machine X Language
Machine X LanguageMachine X Language
Machine X Language
 

Plus de Yun-Yan Chi

Tezos Taipei Meetup #2 - 15/06/2019
Tezos Taipei Meetup #2 - 15/06/2019Tezos Taipei Meetup #2 - 15/06/2019
Tezos Taipei Meetup #2 - 15/06/2019Yun-Yan Chi
 
Traversing on Algebraic Datatype
Traversing on Algebraic DatatypeTraversing on Algebraic Datatype
Traversing on Algebraic DatatypeYun-Yan Chi
 
Data type a la carte
Data type a la carteData type a la carte
Data type a la carteYun-Yan Chi
 
Paper presentation: The relative distance of key point based iris recognition
Paper presentation: The relative distance of key point based iris recognitionPaper presentation: The relative distance of key point based iris recognition
Paper presentation: The relative distance of key point based iris recognitionYun-Yan Chi
 
Deriving a compiler and interpreter for a Multi-level
Deriving a compiler and interpreter for a Multi-level Deriving a compiler and interpreter for a Multi-level
Deriving a compiler and interpreter for a Multi-level Yun-Yan Chi
 
Number System in Haskell
Number System in Haskell Number System in Haskell
Number System in Haskell Yun-Yan Chi
 
Constructing List Homomorphisms from Proofs
Constructing List Homomorphisms from ProofsConstructing List Homomorphisms from Proofs
Constructing List Homomorphisms from ProofsYun-Yan Chi
 

Plus de Yun-Yan Chi (7)

Tezos Taipei Meetup #2 - 15/06/2019
Tezos Taipei Meetup #2 - 15/06/2019Tezos Taipei Meetup #2 - 15/06/2019
Tezos Taipei Meetup #2 - 15/06/2019
 
Traversing on Algebraic Datatype
Traversing on Algebraic DatatypeTraversing on Algebraic Datatype
Traversing on Algebraic Datatype
 
Data type a la carte
Data type a la carteData type a la carte
Data type a la carte
 
Paper presentation: The relative distance of key point based iris recognition
Paper presentation: The relative distance of key point based iris recognitionPaper presentation: The relative distance of key point based iris recognition
Paper presentation: The relative distance of key point based iris recognition
 
Deriving a compiler and interpreter for a Multi-level
Deriving a compiler and interpreter for a Multi-level Deriving a compiler and interpreter for a Multi-level
Deriving a compiler and interpreter for a Multi-level
 
Number System in Haskell
Number System in Haskell Number System in Haskell
Number System in Haskell
 
Constructing List Homomorphisms from Proofs
Constructing List Homomorphisms from ProofsConstructing List Homomorphisms from Proofs
Constructing List Homomorphisms from Proofs
 

Examples for loopless

  • 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 structure unfold fold Value
  • 4. u n unfoldr f o l d seed 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, f0 structure pattern2, f1 pattern3, f2 pattern4, f3 .....]
  • 9. L o o loopless p l e s s O(n) O(1) g [ pattern1, f0 structure 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 o Example (BinT) p l e s s Inorder Traversal a a [ subtree , ... ] l r = Inorder Preorder Traversal Traversal
  • 21. L o o Example (BinT) p l e s s Inorder Traversal + 1 + 1 - = 2 - 2 3 3
  • 22. L o o Example (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 4 1 ! - 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 4 1 ! - 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 4 1 ! - 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)