SlideShare a Scribd company logo
1 of 36
Download to read offline
LLeeaarrnn  yyoouu  aa  
                       HHaasskkeellll
              ffoorr  GGrreeaatt  GGoooodd!!

                      ZZiippppeerr

13年1月20日日曜日
WWhhoo??


              •・ 島崎抄子
              •・ @@sshhookkooss
              •・ ccyybbeerraaggeenntt


13年1月20日日曜日
IInnttrroodduuccttiioonn
              TTrreeee
              →要素を気軽に変更


              FFiillee  SSyysstteemm
              →ファイルの操作




13年1月20日日曜日
TTrreeee


13年1月20日日曜日
TTrreeee

      data Tree a = Empty
                 | Node a (Tree a) (Tree a)
                  deriving (Show)




13年1月20日日曜日
TTrreeee




              GGOOAALL::WW→PP

13年1月20日日曜日
freeTree :: Tree Char


                                        TTrreeee
      freeTree =
         Node 'P'
           (Node 'O'
              (Node 'L'
                   (Node 'N' Empty Empty)
                   (Node 'T' Empty Empty)
              )
              (Node 'Y'
                   (Node 'S' Empty Empty)
                   (Node 'A' Empty Empty)
              )
           )
           (Node 'L'
              (Node 'W'
                   (Node 'C' Empty Empty)
                   (Node 'R' Empty Empty)
              )
              (Node 'A'
                   (Node 'A' Empty Empty)
                   (Node 'C' Empty Empty)
               )
           )


13年1月20日日曜日
TTrreeee
              RRiigghhtt→LLeefftt→PP


      changeToP :: Tree Char -> Tree Char
      changeToP (Node x l (Node y (Node _ m n) r)) =
          Node x l (Node y (Node 'P' m n ) r)




13年1月20日日曜日
TTrreeee
              進路をリストで指定→要素をPP

      data Direction = L | R deriving (Show) type Directions =
      [Direction]
      changeToP :: Directions -> Tree Char -> Tree Char
      changeToP (L:ds) (Node x l r) = Node x (changeToP ds l) r
      changeToP (R:ds) (Node x l r) = Node x l (changeToP ds r)
      changeToP [] (Node _ l r) = Node 'P' l r




13年1月20日日曜日
TTrreeee
              確認用
              目的地の要素を返す関数

      elemAt :: Directions -> Tree a -> a
      elemAt (L:ds) (Node _ l _) = elemAt ds l
      elemAt (R:ds) (Node _ _ r) = elemAt ds r
      elemAt [] (Node x _ _) = x




13年1月20日日曜日
TTrreeee


         要素の更新を、何度も効率よくできるよう
         部分木から近場の部分木へ切り替えたい!




13年1月20日日曜日
TTrreeee
              道しるべを残す
              BBrreeaaddccrruummbbに辿った方向�を追加
      type Breadcrumbs = [Direction]

      goLeft :: (Tree a, Breadcrumbs) -> (Tree a, Breadcrumbs)
      goLeft (Node _ l _, bs) = (l, L:bs)


      goRight :: (Tree a, Breadcrumbs) -> (Tree a, Breadcrumbs)
      goRight (Node _ _ r, bs) = (r, R:bs)




13年1月20日日曜日
TTrreeee

                         (
                            Node 'A'
                               (Node 'A' Empty Empty)
                               (Node 'C' Empty Empty)
                             ,
                             [R, R]
                         ) :: (Tree a, Breadcrumbs)




13年1月20日日曜日
TTrreeee
              1133章で定義した    x -: f = f x 
              を使い美�しく書ける

    ghci> goLeft (goRight (freeTree, []))

    !

    ghci> (freeTree, []) -: goRight -: goLeft

    実行結果:
    (Node 'W' (Node 'C' Empty Empty) (Node 'R' Empty Empty),[L,R])




13年1月20日日曜日
TTrreeee


              木の情報すべてを持ちたい!!!!




13年1月20日日曜日
TTrreeee
              辿らなかった部分木の情報も残す

      data Crumb a = LeftCrumb a (Tree a) | RightCrumb a (Tree a)
          deriving (Show)


      type Breadcrumbs a = [Crumb a]

      goLeft :: (Tree a, Breadcrumbs a) -> (Tree a, Breadcrumbs a)
      goLeft (Node x l r, bs) = (l, LeftCrumb x r:bs)

      goRight :: (Tree a, Breadcrumbs a) -> (Tree a, Breadcrumbs a)
      goRight (Node x l r, bs) = (r, RightCrumb x l:bs)

13年1月20日日曜日
TTrreeee
              情報がある→上へ戻れる!


      goUp :: (Tree a, Breadcrumbs a) -> (Tree a, Breadcrumbs a)
      goUp (t, LeftCrumb x r:bs) = (Node x t r, bs)
      goUp (t, RightCrumb x l:bs) = (Node x l t, bs)




13年1月20日日曜日
TTrreeee
              TTrreeee  aa  と  BBrreeaaddCCrruummbbのペアは、
              元の木の状態を持ちつつ、どの部分
              木に注目しているのかわかる!
              ZZiippppeerrで定義

      type Zipper a = (Tree a, Breadcrumbs a)




13年1月20日日曜日
TTrreeee
              注目している部分木のルートの書き
              換えができる


      modify :: (a -> a) -> Zipper a -> Zipper a
      modify f (Node x l r, bs)=(Node (f x ) l r, bs)
      modify f (Empty, bs) = (Empty, bs)




13年1月20日日曜日
TTrreeee
              LLeefftt→RRiigghhtt→PPに要素を変更
              のあとに、
              ひとつ上の要素をXXに変更したい!

      ghci> let newFocus =
       (freeTree, []) -: goLeft -: goRight -: modify (_ -> 'P')

      ghci> let newFocus2 = newFocus -: goUp -: modify (_ -> 'X')




13年1月20日日曜日
TTrreeee
              注目している部分木の置き換えも
              シンプルなコードでできる


      attach :: Tree a -> Zipper a -> Zipper a
      attach t (_, bs) = (t, bs)




13年1月20日日曜日
TTrreeee
              トップへ戻る

              ggooUUppを、道しるべがなくなるまで再帰


      topMost :: Zipper a -> Zipper a
      topMost (t, []) = (t, [])
      topMost z = topMost (goUp z)




13年1月20日日曜日
LLiisstt

              LLiissttをZZiippppeerrに!

              (注目している要素、辿ってきた要素)




13年1月20日日曜日
LLiisstt
              LLiissttもZZiippppeerrで作ってみる!
      type ListZipper a = ([a], [a])

      goForward :: ListZipper a -> ListZipper a
      goForward (x:xs, bs) = (xs, x:bs)


      goBack :: ListZipper a -> ListZipper a
      goBack (xs, b:bs) = (b:xs, bs)




13年1月20日日曜日
FFiillee  SSyysstteemm



13年1月20日日曜日
FFiillee  SSyysstteemm
      type Name = String

      type Data = String

      data FSItem = File Name Data | Folder Name [FSItem]
              deriving (Show)




       FFiillee・・・名前がついていて、データが
       入�っている

       FFoollddeerr・・・名前がついていて、複数の
       ファイルやフォルダをアイテムとして含む
13年1月20日日曜日
myDisk = Folder "root"
          [File "goat_yelling_like_man.wmv" "baaaaaa"


                          FFiillee  SSyysstteemm
              , File "pope_time.avi" "god bless"
              , Folder "pics"
               [File "ape_throwing_up.jpg" "bleargh"
               , File "watermelon_smash.gif" "smash!!"
               , File "skull_man(scary).bmp" "Yikes!"
                ]
               , File "dijon_poupon.doc" "best mustard"
               , Folder "programs"
                    [File "fartwizard.exe" "10gotofart"
                , File "owl_bandit.dmg" "mov eax, h00t"
                , File "not_a_virus.exe" "really not a virus"
                , Folder "source code"
                      [File "best_hs_prog.hs" "main = print (fix error)"
                      , File "random.hs" "main = print 4"
                      ]
                      ]
                      ]
13年1月20日日曜日
Folder1


                 Folder2 File1 File2

              File3 Folder3

         FFiilleeは空の木のようなもの

13年1月20日日曜日
FFiillee  SSyysstteemm
      data FSCrumb = FSCrumb Name [FSItem] [FSItem] deriving (Show)


        NNaammee::親フォルダ名
        [[FFSSIItteemm]]::前のアイテムのリスト 
        [[FFSSIItteemm]]::後ろのアイテムのリスト


      type FSZipper = (FSItem, [FSCrumb])



          (注目しているアイテム、道しるべ
          のリスト)
13年1月20日日曜日
注目:FFoollddeerr11
                                 前のアイテム:
                         Folder1 FFoollddeerr22の情報等
                                 後ろのアイテム::なし

                 Folder2 File1 File2

              File3 Folder3



13年1月20日日曜日
FFiillee  SSyysstteemm
              上の階層に行く

      fsUp :: FSZipper -> FSZipper
      fsUp (item, FSCrumb name ls rs:bs) =
        (Folder name (ls ++ [item] ++ rs), bs)




              注目しているアイテムを、前のアイ
              テムに移す



13年1月20日日曜日
注目:FFoollddeerr11
               Folder1 前のアイテム:FFiillee22の情報等
                       後ろのアイテム::なし

        Folder2 File1 File2

 File3 Folder3    注目:FFoollddeerr22
                  前のアイテム:FFiillee33の情報等
                  後ろのアイテム::FFoollddeerr11の情報等



13年1月20日日曜日
FFiillee  SSyysstteemm
                 注目しているIItteemmを変更

      fsTo :: Name -> FSZipper -> FSZipper
      fsTo name (Folder folderName items, bs) =
              let (ls, item:rs) = break (nameIs name) items
              in (item, FSCrumb folderName ls rs:bs)




      nameIs :: Name -> FSItem -> Bool
      nameIs name (Folder folderName _) = name == folderName
      nameIs name (File fileName _) = name == fileName




13年1月20日日曜日
FFiillee  SSyysstteemm

                RReennaammee

      fsRename :: Name -> FSZipper -> FSZipper
      fsRename newName (Folder name items, bs) = (Folder newName
              items, bs)
      fsRename newName (File name dat, bs) = (File newName dat, bs)




13年1月20日日曜日
FFiillee  SSyysstteemm

              NNeewwFFiillee


      fsNewFile :: FSItem -> FSZipper -> FSZipper
      fsNewFile item (Folder folderName items, bs) =
        (Folder folderName (item:items), bs)




13年1月20日日曜日
MMaayybbee!!!!!!
      goLeft :: Zipper a -> Maybe (Zipper a)
      goLeft (Node x l r, bs) = Just (l, LeftCrumb x r:bs)
      goLeft (Empty, _) = Nothing


      goRight :: Zipper a -> Maybe (Zipper a)
      goRight (Node x l r, bs) = Just (r, RightCrumb x l:bs)
      goRight (Empty, _) = Nothing


      goUp :: Zipper a -> Maybe (Zipper a)
      goUp (t, LeftCrumb x r:bs) = Just (Node x t r, bs)
      goUp (t, RightCrumb x l:bs) = Just (Node x l t, bs)
      goUp (_, []) = Nothing



13年1月20日日曜日

More Related Content

Featured

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by HubspotMarius Sescu
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTExpeed Software
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsPixeldarts
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthThinkNow
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfmarketingartwork
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 

Featured (20)

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 

Start haskell zipper

  • 1. LLeeaarrnn yyoouu aa HHaasskkeellll ffoorr GGrreeaatt GGoooodd!! ZZiippppeerr 13年1月20日日曜日
  • 2. WWhhoo?? •・ 島崎抄子 •・ @@sshhookkooss •・ ccyybbeerraaggeenntt 13年1月20日日曜日
  • 3. IInnttrroodduuccttiioonn TTrreeee →要素を気軽に変更 FFiillee SSyysstteemm →ファイルの操作 13年1月20日日曜日
  • 5. TTrreeee data Tree a = Empty | Node a (Tree a) (Tree a) deriving (Show) 13年1月20日日曜日
  • 6. TTrreeee GGOOAALL::WW→PP 13年1月20日日曜日
  • 7. freeTree :: Tree Char TTrreeee freeTree = Node 'P' (Node 'O' (Node 'L' (Node 'N' Empty Empty) (Node 'T' Empty Empty) ) (Node 'Y' (Node 'S' Empty Empty) (Node 'A' Empty Empty) ) ) (Node 'L' (Node 'W' (Node 'C' Empty Empty) (Node 'R' Empty Empty) ) (Node 'A' (Node 'A' Empty Empty) (Node 'C' Empty Empty) ) ) 13年1月20日日曜日
  • 8. TTrreeee RRiigghhtt→LLeefftt→PP changeToP :: Tree Char -> Tree Char changeToP (Node x l (Node y (Node _ m n) r)) =     Node x l (Node y (Node 'P' m n ) r) 13年1月20日日曜日
  • 9. TTrreeee 進路をリストで指定→要素をPP data Direction = L | R deriving (Show) type Directions = [Direction] changeToP :: Directions -> Tree Char -> Tree Char changeToP (L:ds) (Node x l r) = Node x (changeToP ds l) r changeToP (R:ds) (Node x l r) = Node x l (changeToP ds r) changeToP [] (Node _ l r) = Node 'P' l r 13年1月20日日曜日
  • 10. TTrreeee 確認用 目的地の要素を返す関数 elemAt :: Directions -> Tree a -> a elemAt (L:ds) (Node _ l _) = elemAt ds l elemAt (R:ds) (Node _ _ r) = elemAt ds r elemAt [] (Node x _ _) = x 13年1月20日日曜日
  • 11. TTrreeee 要素の更新を、何度も効率よくできるよう 部分木から近場の部分木へ切り替えたい! 13年1月20日日曜日
  • 12. TTrreeee 道しるべを残す BBrreeaaddccrruummbbに辿った方向�を追加 type Breadcrumbs = [Direction] goLeft :: (Tree a, Breadcrumbs) -> (Tree a, Breadcrumbs) goLeft (Node _ l _, bs) = (l, L:bs) goRight :: (Tree a, Breadcrumbs) -> (Tree a, Breadcrumbs) goRight (Node _ _ r, bs) = (r, R:bs) 13年1月20日日曜日
  • 13. TTrreeee ( Node 'A' (Node 'A' Empty Empty) (Node 'C' Empty Empty) , [R, R] ) :: (Tree a, Breadcrumbs) 13年1月20日日曜日
  • 14. TTrreeee 1133章で定義した x -: f = f x  を使い美�しく書ける ghci> goLeft (goRight (freeTree, [])) ! ghci> (freeTree, []) -: goRight -: goLeft 実行結果: (Node 'W' (Node 'C' Empty Empty) (Node 'R' Empty Empty),[L,R]) 13年1月20日日曜日
  • 15. TTrreeee 木の情報すべてを持ちたい!!!! 13年1月20日日曜日
  • 16. TTrreeee 辿らなかった部分木の情報も残す data Crumb a = LeftCrumb a (Tree a) | RightCrumb a (Tree a)     deriving (Show) type Breadcrumbs a = [Crumb a] goLeft :: (Tree a, Breadcrumbs a) -> (Tree a, Breadcrumbs a) goLeft (Node x l r, bs) = (l, LeftCrumb x r:bs) goRight :: (Tree a, Breadcrumbs a) -> (Tree a, Breadcrumbs a) goRight (Node x l r, bs) = (r, RightCrumb x l:bs) 13年1月20日日曜日
  • 17. TTrreeee 情報がある→上へ戻れる! goUp :: (Tree a, Breadcrumbs a) -> (Tree a, Breadcrumbs a) goUp (t, LeftCrumb x r:bs) = (Node x t r, bs) goUp (t, RightCrumb x l:bs) = (Node x l t, bs) 13年1月20日日曜日
  • 18. TTrreeee TTrreeee aa と BBrreeaaddCCrruummbbのペアは、 元の木の状態を持ちつつ、どの部分 木に注目しているのかわかる! ZZiippppeerrで定義 type Zipper a = (Tree a, Breadcrumbs a) 13年1月20日日曜日
  • 19. TTrreeee 注目している部分木のルートの書き 換えができる modify :: (a -> a) -> Zipper a -> Zipper a modify f (Node x l r, bs)=(Node (f x ) l r, bs) modify f (Empty, bs) = (Empty, bs) 13年1月20日日曜日
  • 20. TTrreeee LLeefftt→RRiigghhtt→PPに要素を変更 のあとに、 ひとつ上の要素をXXに変更したい! ghci> let newFocus = (freeTree, []) -: goLeft -: goRight -: modify (_ -> 'P') ghci> let newFocus2 = newFocus -: goUp -: modify (_ -> 'X') 13年1月20日日曜日
  • 21. TTrreeee 注目している部分木の置き換えも シンプルなコードでできる attach :: Tree a -> Zipper a -> Zipper a attach t (_, bs) = (t, bs) 13年1月20日日曜日
  • 22. TTrreeee トップへ戻る ggooUUppを、道しるべがなくなるまで再帰 topMost :: Zipper a -> Zipper a topMost (t, []) = (t, []) topMost z = topMost (goUp z) 13年1月20日日曜日
  • 23. LLiisstt LLiissttをZZiippppeerrに! (注目している要素、辿ってきた要素) 13年1月20日日曜日
  • 24. LLiisstt LLiissttもZZiippppeerrで作ってみる! type ListZipper a = ([a], [a]) goForward :: ListZipper a -> ListZipper a goForward (x:xs, bs) = (xs, x:bs) goBack :: ListZipper a -> ListZipper a goBack (xs, b:bs) = (b:xs, bs) 13年1月20日日曜日
  • 26. FFiillee SSyysstteemm type Name = String type Data = String data FSItem = File Name Data | Folder Name [FSItem] deriving (Show) FFiillee・・・名前がついていて、データが 入�っている FFoollddeerr・・・名前がついていて、複数の ファイルやフォルダをアイテムとして含む 13年1月20日日曜日
  • 27. myDisk = Folder "root" [File "goat_yelling_like_man.wmv" "baaaaaa" FFiillee SSyysstteemm , File "pope_time.avi" "god bless" , Folder "pics" [File "ape_throwing_up.jpg" "bleargh" , File "watermelon_smash.gif" "smash!!" , File "skull_man(scary).bmp" "Yikes!" ] , File "dijon_poupon.doc" "best mustard" , Folder "programs" [File "fartwizard.exe" "10gotofart" , File "owl_bandit.dmg" "mov eax, h00t" , File "not_a_virus.exe" "really not a virus" , Folder "source code" [File "best_hs_prog.hs" "main = print (fix error)" , File "random.hs" "main = print 4" ] ] ] 13年1月20日日曜日
  • 28. Folder1 Folder2 File1 File2 File3 Folder3 FFiilleeは空の木のようなもの 13年1月20日日曜日
  • 29. FFiillee SSyysstteemm data FSCrumb = FSCrumb Name [FSItem] [FSItem] deriving (Show) NNaammee::親フォルダ名 [[FFSSIItteemm]]::前のアイテムのリスト  [[FFSSIItteemm]]::後ろのアイテムのリスト type FSZipper = (FSItem, [FSCrumb]) (注目しているアイテム、道しるべ のリスト) 13年1月20日日曜日
  • 30. 注目:FFoollddeerr11 前のアイテム: Folder1 FFoollddeerr22の情報等 後ろのアイテム::なし Folder2 File1 File2 File3 Folder3 13年1月20日日曜日
  • 31. FFiillee SSyysstteemm 上の階層に行く fsUp :: FSZipper -> FSZipper fsUp (item, FSCrumb name ls rs:bs) =   (Folder name (ls ++ [item] ++ rs), bs) 注目しているアイテムを、前のアイ テムに移す 13年1月20日日曜日
  • 32. 注目:FFoollddeerr11 Folder1 前のアイテム:FFiillee22の情報等 後ろのアイテム::なし Folder2 File1 File2 File3 Folder3 注目:FFoollddeerr22 前のアイテム:FFiillee33の情報等 後ろのアイテム::FFoollddeerr11の情報等 13年1月20日日曜日
  • 33. FFiillee SSyysstteemm 注目しているIItteemmを変更 fsTo :: Name -> FSZipper -> FSZipper fsTo name (Folder folderName items, bs) = let (ls, item:rs) = break (nameIs name) items in (item, FSCrumb folderName ls rs:bs) nameIs :: Name -> FSItem -> Bool nameIs name (Folder folderName _) = name == folderName nameIs name (File fileName _) = name == fileName 13年1月20日日曜日
  • 34. FFiillee SSyysstteemm RReennaammee fsRename :: Name -> FSZipper -> FSZipper fsRename newName (Folder name items, bs) = (Folder newName items, bs) fsRename newName (File name dat, bs) = (File newName dat, bs) 13年1月20日日曜日
  • 35. FFiillee SSyysstteemm NNeewwFFiillee fsNewFile :: FSItem -> FSZipper -> FSZipper fsNewFile item (Folder folderName items, bs) = (Folder folderName (item:items), bs) 13年1月20日日曜日
  • 36. MMaayybbee!!!!!! goLeft :: Zipper a -> Maybe (Zipper a) goLeft (Node x l r, bs) = Just (l, LeftCrumb x r:bs) goLeft (Empty, _) = Nothing goRight :: Zipper a -> Maybe (Zipper a) goRight (Node x l r, bs) = Just (r, RightCrumb x l:bs) goRight (Empty, _) = Nothing goUp :: Zipper a -> Maybe (Zipper a) goUp (t, LeftCrumb x r:bs) = Just (Node x t r, bs) goUp (t, RightCrumb x l:bs) = Just (Node x l t, bs) goUp (_, []) = Nothing 13年1月20日日曜日