Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

純粋関数型データ構造

290 views

Published on

(car (cdr ファンクション倶楽部))での発表資料。
https://esminc.connpass.com/event/57534/

Published in: Software
  • Be the first to comment

  • Be the first to like this

純粋関数型データ構造

  1. 1. (car (cdr ))
  2. 2. about me • wat-aro • @wat-aro • GitHub wat-aro • • Haskell
  3. 3. • SML • SML OCaml • Haskell •
  4. 4. • • •
  5. 5. • • ( ) •
  6. 6. • •
  7. 7. d b a c g f h e
  8. 8. e d b a c g f h e
  9. 9. d b a c g f h e
  10. 10. xs d b a c g f h d g f e ys xs ys
  11. 11. xs d b a c g f h d g f e ys
  12. 12. • • •
  13. 13. ai i ti i m mX i=1 ai mX i=1 ti
  14. 14. • • • •
  15. 15. • • head • tail • snoc
  16. 16. • f, r • head f • (snoc) f r rotate • tail f head f r rotate • rotate: r reverse f append
  17. 17. • f, r data BankersQueue a = BQ Int [a] Int [a] deriving Show
  18. 18. • head Maybe head :: BankersQueue a -> Maybe a head (BQ _ [] _ _) = Nothing head (BQ _ (x : f') _ _) = Just x
  19. 19. • (snoc) r f r rotate • rotate: r reverse f append
  20. 20. snoc a b f d c r e a b f c d r e a b f e d r c f < r append
  21. 21. snoc :: BankersQueue a -> a -> BankersQueue a snoc (BQ lenf f lenr r) x = check (BQ lenf f (lenr + 1) (x : r)) check :: BankersQueue a -> BankersQueue a check q@(BQ lenf f lenr r) | lenr <= lenf = q | otherwise = BQ (lenf + lenr) (f ++ reverse r) 0 []
  22. 22. • tail f cdr f r rotate • f r 1 r rotate |r| tail :: BankersQueue a -> Maybe (BankersQueue a) tail (BQ _ [] _ _) = Nothing tail (BQ lenf (x : f') lenr r) = Just $ check $ BQ (lenf - 1) f' lenr r
  23. 23. • reverse |r| • reverse tail |f| • rotate snoc 2 reverse 1 (rotate snoc 1) • reverse 1 O(1)
  24. 24. • • •

×