Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Recursion Schemesで考える並べ替えアルゴリズム

lotz
November 29, 2019

Recursion Schemesで考える並べ替えアルゴリズム

lotz

November 29, 2019
Tweet

More Decks by lotz

Other Decks in Technology

Transcript

  1. όϒϧιʔτ [1, 6, 5, 2] [1, 6, 5, 2] [1,

    5, 6, 2] [1, 5, 2, 6] [1, 5, 2, 6] [1, 5, 2, 6] [1, 2, 5, 6] [1, 2, 5, 6] ೖΕସ͑ ೖΕସ͑ ೖΕସ͑
  2. ૠೖιʔτ [] [1, 6, 5, 2] [1] [6, 5, 2]

    [1, 6] [5, 2] [1, 5, 6] [2] [1, 2, 5, 6] []
  3. ૠೖιʔτ - ࣮૷ insertSort :: [Int] -> [Int] insertSort xs

    = go [] xs where go ys [] = ys go ys (x:xs) = go (insert x ys) xs insert :: Int -> [Int] -> [Int] insert x ys = xs ++ [x] ++ zs where (xs, zs) = span (<= x) ys
  4. ૠೖιʔτ - foldr foldr :: (a -> b -> b)

    -> b -> [a] -> b foldr _ z [] = z foldr k z (y:ys) = y `k` foldr k z ys insertSort :: [Int] -> [Int] insertSort = foldr insert [] ࣮૷ͷ࠶ؼతͳߏ଄ΛGPMESΛ࢖ͬͯ෼཭ 3FDVSTJPO4DIFNFT
  5. data [] a = [] | a : [a] ࠶ؼతͰ͸ͳ͍ܕ

    data List r = Nil | Cons Int r ࠶ؼతͳߏ଄Λ࣋ͭ൚༻తͳܕ newtype Fix f = In {out :: f (Fix f)} ͋ͱͰܕΫϥε੍໿Λॻ͘ྔΛݮΒͨ͢Ί *OUͷ৔߹ʹݶఆ͠·͢ ࠶ؼతͳܕΛ෼ղ͢Δ
  6. Fix List ͸ [Int] ͱ ಉܕ toList :: Fix List

    -> [Int] toList (In Nil) = [] toList (In (Cons x xs)) = x : toList xs fromList :: [Int] -> Fix List fromList [] = In Nil fromList (x:xs) = In $ Cons x (fromList xs)
  7. Catamorphism cata :: Functor f => (f a -> a)

    -> Fix f -> a cata g = g . fmap (cata g) . out cata g fmap (cata g) g
  8. Anamorphism ana :: Functor f => (a -> f a)

    -> a -> Fix f ana g = In . fmap (ana g) . g g ana g fmap (ana g)
  9. ฒ΂ସ͑ΞϧΰϦζϜͷܕ ιʔτࡁΈͷϦετΛද͢ܕ data SList r = Nil' | Cons' Int

    r sort :: Fix List -> Fix SList (List a -> a) -> Fix List -> a (a -> SList a) -> a -> Fix SList
  10. Fix List ʹ஫໨ͨ͠৔߹ sort :: Fix List -> Fix SList

    sort = cata (? :: List (Fix SList) -> Fix SList) = cata $ ana (?? :: List (Fix SList) -> SList (List (Fix SList))) = cata $ ana (??? . fmap out) ??? :: List (SList (Fix SList)) -> SList (List (Fix SList))
  11. Fix SList ʹ஫໨ͨ͠৔߹ sort :: Fix List -> Fix SList

    sort = ana (? :: Fix List -> SList (Fix List)) = ana $ cata (?? :: List (SList (Fix List)) -> SList (Fix List)) = ana $ cata (fmap In . ???) ??? :: List (SList (Fix List)) -> SList (List (Fix List))
  12. ෼഑๏ଇ swap :: List (SList x) -> SList (List x)

    swap Nil = Nil' swap (Cons a Nil') = Cons' a Nil swap (Cons a (Cons' b x)) | a <= b = Cons' a (Cons b x) | otherwise = Cons' b (Cons a x)
  13. 2ͭͷฒ΂ସ͑ΞϧΰϦζϜ > toList . cata (ana (swap . fmap out))

    . fromList $ [1,6,5,2] [1,2,5,6] > toList . ana (cata (fmap In . swap)) . fromList $ [1,6,5,2] [1,2,5,6]
  14. cata (ana (swap . fmap out)) (In (Cons 1 (In

    (Cons 6 (In (Cons 5 (In (Cons 2 (In Nil)…) ana f (Cons 1 (ana f (Cons 6 (ana f (Cons 5 (ana f (Cons 2 (ana f Nil)…) ana f (Cons 1 (ana f (Cons 6 (ana f (Cons 5 (In (Cons’ 2 (In Nil’))…) cata - ana - swap DBUB͸શͯͷ*OΛஔ͖׵͑Δ ͜͜·ͰධՁ͢Δ ͜Ε͸ιʔτࡁΈͷϦετ
  15. ana f (Cons 1 (ana f (Cons 6 (ana (swap

    . fmap out) (Cons 5 (In (Cons’ 2 (In Nil’))…) ana f (Cons 1 (ana f (Cons 6 (In . fmap (ana f) $ swap (Cons 5 (Cons’ 2 (In Nil’))…) ana f (Cons 1 (ana f (Cons 6 (In (Cons’ 2 (ana f (Cons 5 (In Nil’))…) ͜Ε͸ૠೖιʔτʂ˞ ల։ͯ͠GNBQPVUΛద༻͢Δ Ұͭ಺ଆʹBOBGΛ࢓ࠐΜͰશମΛ*OͰแΉ
  16. ana - cata - swap ana (cata (fmap In .

    swap)) (In (Cons 1 (In (Cons 6 (In (Cons 5 (In (Cons 2 (In Nil)…) In . fmap (ana f) . cata (fmap In . swap) $ (In (Cons 1 (In (Cons 6 (In (Cons 5 (In (Cons 2 (In Nil)…) In . fmap (ana f) $ g (Cons 1 (g (Cons 6 (g (Cons 5 (g (Cons 2 (g Nil)…) In . fmap (ana f) $ g (Cons 1 (g (Cons 6 (g (Cons 5 (Cons’ 2 (In Nil)…) BOBΛల։͢Δ DBUB͸શͯͷ*OΛஔ͖׵͑Δ ͜͜·ͰධՁ͢Δ
  17. In . fmap (ana f) $ g (Cons 1 (g

    (Cons 6 ((fmap In . swap) (Cons 5 (Cons’ 2 (In Nil)…) In . fmap (ana f) $ g (Cons 1 (g (Cons 6 (In (Cons’ 2 (In (Cons 5 (In Nil)…) In . fmap (ana f) $ Cons’ 1 (In (Cons 2 (In (Cons 6 (In (Cons 5 (In Nil)…) In (Cons’ 1 (ana (cata (fmap In . swap)) (In (Cons 2 (In (Cons 6 (In (Cons 5 (In Nil)...) ͜Ε͸όϒϧιʔτʂ ͜Ε͸ιʔτࡁΈͷϦετ