SlideShare ist ein Scribd-Unternehmen logo
1 von 17
Downloaden Sie, um offline zu lesen
9.2.3 Lazy Representations
9.2.1 Binary Random-Access Lists
type 'a tree = LEAF of 'a
              | NODE of int * 'a tree * 'a tree
    type 'a digit = ZERO
               | ONE of 'a tree
    type rlist = Element.t digit list

    let rec consTree = function
      | (t, []) -> [ONE t]
      | (t, ZERO :: ts) -> ONE t :: ts
      | (t1, ONE t2 :: ts) -> ZERO :: consTree (link (t1, t2), ts)
    ;;

    let rec unconsTree = function
      | [] -> raise Empty
      | [ONE t] -> (t, [])
      | (ONE t :: ts) -> (t, ZERO :: ts)
      | (ZERO :: ts) ->
          let (NODE (_, t1, t2), ts') = unconsTree ts in
          (t1, ONE t2 :: ts')
    ;;
9.2.2 Zeroless Representations
type 'a tree = LEAF of 'a
              | NODE of int * 'a tree * 'a tree
    type 'a digit = ONE of 'a tree
               | TWO of 'a tree * 'a tree
    type rlist = Elem.t digit list

    let rec consTree = function
      | (t, []) -> [ONE t]
      | (t1, ONE t2 :: ts) -> TWO (t1, t2) :: ts
      | (t1, TWO (t2, t3) :: ts) -> ONE t1 :: consTree (link (t2, t2), ts)
    ;;

    let rec unconsTree = function
      | [] -> raise Empty
      | [ONE t] -> (t, [])
      | (ONE t1 :: ts) ->
          let (NODE (_, t2, t3), ts') = unconsTree ts in
          (t1, TWO (t2, t3) :: ts')
      | (TWO (t1, t2) :: ts) -> (t1, ONE t2 :: ts)
    ;;
9.2.3 Lazy Representations

Binomial Heapsに遅延評価を導入したら、
挿入がAmortized timeでO(1)になった。



      Binary Random Access List の cons 関数も
      O(1) Amortized Timeで実行可能なはず。
Amortized Analysisする中で、
データ構造をPersistentな使い方に対応させるには、
遅延評価を使って関数をIncrementalに変換する。
type 'a tree = LEAF of 'a
              | NODE of int * 'a tree * 'a tree
    type 'a digit = ZERO
               | ONE of 'a tree
    type rlist = Elem.t digit S.stream

    let rec consTree = function
      | (t, lazy S.Nil) -> lazy (S.Cons (ONE t, lazy S.Nil))
      | (t1, lazy (S.Cons (ONE t2, ts))) -> lazy (S.Cons (ONE t1, consTree (link (t2, ts))))
      | (t, lazy (S.Cons (ZERO, ts))) -> lazy (S.Cons (ONE t, ts))
    ;;

    let rec unconsTree = function
      | (lazy S.Nil) -> raise Empty
      | (lazy (S.Cons (ONE t, lazy S.Nil))) -> (t, lazy S.Nil)
      | (lazy (S.Cons (ONE t, ts))) -> (t, (lazy (S.Cons (ZERO, ts))))
      | (lazy (S.Cons (ZERO, ts))) ->
          let (NODE (_, t1, t2), ts') = unconsTree ts in
          (t1, lazy (S.Cons (ONE t2, ts')))
    ;;
type 'a tree = LEAF of 'a
              | NODE of int * 'a tree * 'a tree
    type 'a digit = ZERO
               | ONE of 'a tree
               | TWO of 'a tree * 'a tree
    type rlist = Elem.t digit S.stream

    let rec consTree = function
      | (t, lazy S.Nil) -> lazy (S.Cons (ONE t, lazy S.Nil))
      | (t1, lazy (S.Cons (TWO (t2, t3), ts))) -> lazy (S.Cons (ONE t1, consTree (link (t2, t3), ts)))
      | (t1, lazy (S.Cons (ONE t2, ts))) -> lazy (S.Cons (TWO (t1, t2), ts))
      | (t, lazy (S.Cons (ZERO, ts))) -> lazy (S.Cons (ONE t, ts))
    ;;

    let rec unconsTree = function
      | (lazy S.Nil) -> raise Empty
      | (lazy (S.Cons (TWO (t1, t2), ts))) -> (t1, lazy (S.Cons (ONE t2, ts)))
      | (lazy (S.Cons (ONE t, lazy S.Nil))) -> (t, lazy S.Nil)
      | (lazy (S.Cons (ONE t, ts))) -> (t, (lazy (S.Cons (ZERO, ts))))
      | (lazy (S.Cons (ZERO, ts))) ->
          let (NODE (_, t1, t2), ts') = unconsTree ts in
          (t1, lazy (S.Cons (ONE t2, ts')))
    ;;
最下位桁から1が連続するとき、その連続部分の数の表現は一種類だけ。
つまり、すべてが1だけで構成される数のとき、表現は1つに収束する。
例えば、111と1111の間の数は様々な表現をもつが、
両端の111と1111はひとつずつしかない。
PFDS 9.2.3 Lazy Representations

Weitere ähnliche Inhalte

Ähnlich wie PFDS 9.2.3 Lazy Representations

PFDS 4章をOCamlに翻訳
PFDS 4章をOCamlに翻訳PFDS 4章をOCamlに翻訳
PFDS 4章をOCamlに翻訳
Kiwamu Okabe
 

Ähnlich wie PFDS 9.2.3 Lazy Representations (20)

Python programming : List and tuples
Python programming : List and tuplesPython programming : List and tuples
Python programming : List and tuples
 
Lists.pptx
Lists.pptxLists.pptx
Lists.pptx
 
PFDS 4章をOCamlに翻訳
PFDS 4章をOCamlに翻訳PFDS 4章をOCamlに翻訳
PFDS 4章をOCamlに翻訳
 
Session -5for students.pdf
Session -5for students.pdfSession -5for students.pdf
Session -5for students.pdf
 
Advanced data structure
Advanced data structureAdvanced data structure
Advanced data structure
 
‏‏chap6 list tuples.pptx
‏‏chap6 list tuples.pptx‏‏chap6 list tuples.pptx
‏‏chap6 list tuples.pptx
 
List_tuple_dictionary.pptx
List_tuple_dictionary.pptxList_tuple_dictionary.pptx
List_tuple_dictionary.pptx
 
Dependent Types with Idris
Dependent Types with IdrisDependent Types with Idris
Dependent Types with Idris
 
STRING LIST TUPLE DICTIONARY FILE.pdf
STRING LIST TUPLE DICTIONARY FILE.pdfSTRING LIST TUPLE DICTIONARY FILE.pdf
STRING LIST TUPLE DICTIONARY FILE.pdf
 
PRESENTATION ON TUPLES.pptx
PRESENTATION ON TUPLES.pptxPRESENTATION ON TUPLES.pptx
PRESENTATION ON TUPLES.pptx
 
Python Lecture 11
Python Lecture 11Python Lecture 11
Python Lecture 11
 
Pytho_tuples
Pytho_tuplesPytho_tuples
Pytho_tuples
 
python_avw - Unit-03.pdf
python_avw - Unit-03.pdfpython_avw - Unit-03.pdf
python_avw - Unit-03.pdf
 
The Ring programming language version 1.7 book - Part 26 of 196
The Ring programming language version 1.7 book - Part 26 of 196The Ring programming language version 1.7 book - Part 26 of 196
The Ring programming language version 1.7 book - Part 26 of 196
 
least_action.pdf
least_action.pdfleast_action.pdf
least_action.pdf
 
The basics and design of lua table
The basics and design of lua tableThe basics and design of lua table
The basics and design of lua table
 
Revision Tour 1 and 2 complete.doc
Revision Tour 1 and 2 complete.docRevision Tour 1 and 2 complete.doc
Revision Tour 1 and 2 complete.doc
 
Pytho lists
Pytho listsPytho lists
Pytho lists
 
Ch5b
Ch5bCh5b
Ch5b
 
Zippers
ZippersZippers
Zippers
 

Mehr von 昌平 村山 (6)

PFDS 11.2 catenable double ended queue
PFDS 11.2 catenable double ended queuePFDS 11.2 catenable double ended queue
PFDS 11.2 catenable double ended queue
 
PFDS 10.2.2 heaps with efficient merging
PFDS 10.2.2 heaps with efficient mergingPFDS 10.2.2 heaps with efficient merging
PFDS 10.2.2 heaps with efficient merging
 
PFDS 10.2.1 lists with efficient catenation
PFDS 10.2.1 lists with efficient catenationPFDS 10.2.1 lists with efficient catenation
PFDS 10.2.1 lists with efficient catenation
 
PFDS chart
PFDS chartPFDS chart
PFDS chart
 
PFDS 8.4.3 Real-Time Deques
PFDS 8.4.3 Real-Time DequesPFDS 8.4.3 Real-Time Deques
PFDS 8.4.3 Real-Time Deques
 
PFDS 5.5 Pairing heap
PFDS 5.5 Pairing heapPFDS 5.5 Pairing heap
PFDS 5.5 Pairing heap
 

Kürzlich hochgeladen

EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
Earley Information Science
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
Joaquim Jorge
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
giselly40
 

Kürzlich hochgeladen (20)

Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 

PFDS 9.2.3 Lazy Representations

  • 3. type 'a tree = LEAF of 'a | NODE of int * 'a tree * 'a tree type 'a digit = ZERO | ONE of 'a tree type rlist = Element.t digit list let rec consTree = function | (t, []) -> [ONE t] | (t, ZERO :: ts) -> ONE t :: ts | (t1, ONE t2 :: ts) -> ZERO :: consTree (link (t1, t2), ts) ;; let rec unconsTree = function | [] -> raise Empty | [ONE t] -> (t, []) | (ONE t :: ts) -> (t, ZERO :: ts) | (ZERO :: ts) -> let (NODE (_, t1, t2), ts') = unconsTree ts in (t1, ONE t2 :: ts') ;;
  • 5. type 'a tree = LEAF of 'a | NODE of int * 'a tree * 'a tree type 'a digit = ONE of 'a tree | TWO of 'a tree * 'a tree type rlist = Elem.t digit list let rec consTree = function | (t, []) -> [ONE t] | (t1, ONE t2 :: ts) -> TWO (t1, t2) :: ts | (t1, TWO (t2, t3) :: ts) -> ONE t1 :: consTree (link (t2, t2), ts) ;; let rec unconsTree = function | [] -> raise Empty | [ONE t] -> (t, []) | (ONE t1 :: ts) -> let (NODE (_, t2, t3), ts') = unconsTree ts in (t1, TWO (t2, t3) :: ts') | (TWO (t1, t2) :: ts) -> (t1, ONE t2 :: ts) ;;
  • 6. 9.2.3 Lazy Representations Binomial Heapsに遅延評価を導入したら、 挿入がAmortized timeでO(1)になった。 Binary Random Access List の cons 関数も O(1) Amortized Timeで実行可能なはず。
  • 8. type 'a tree = LEAF of 'a | NODE of int * 'a tree * 'a tree type 'a digit = ZERO | ONE of 'a tree type rlist = Elem.t digit S.stream let rec consTree = function | (t, lazy S.Nil) -> lazy (S.Cons (ONE t, lazy S.Nil)) | (t1, lazy (S.Cons (ONE t2, ts))) -> lazy (S.Cons (ONE t1, consTree (link (t2, ts)))) | (t, lazy (S.Cons (ZERO, ts))) -> lazy (S.Cons (ONE t, ts)) ;; let rec unconsTree = function | (lazy S.Nil) -> raise Empty | (lazy (S.Cons (ONE t, lazy S.Nil))) -> (t, lazy S.Nil) | (lazy (S.Cons (ONE t, ts))) -> (t, (lazy (S.Cons (ZERO, ts)))) | (lazy (S.Cons (ZERO, ts))) -> let (NODE (_, t1, t2), ts') = unconsTree ts in (t1, lazy (S.Cons (ONE t2, ts'))) ;;
  • 9.
  • 10.
  • 11.
  • 12.
  • 13. type 'a tree = LEAF of 'a | NODE of int * 'a tree * 'a tree type 'a digit = ZERO | ONE of 'a tree | TWO of 'a tree * 'a tree type rlist = Elem.t digit S.stream let rec consTree = function | (t, lazy S.Nil) -> lazy (S.Cons (ONE t, lazy S.Nil)) | (t1, lazy (S.Cons (TWO (t2, t3), ts))) -> lazy (S.Cons (ONE t1, consTree (link (t2, t3), ts))) | (t1, lazy (S.Cons (ONE t2, ts))) -> lazy (S.Cons (TWO (t1, t2), ts)) | (t, lazy (S.Cons (ZERO, ts))) -> lazy (S.Cons (ONE t, ts)) ;; let rec unconsTree = function | (lazy S.Nil) -> raise Empty | (lazy (S.Cons (TWO (t1, t2), ts))) -> (t1, lazy (S.Cons (ONE t2, ts))) | (lazy (S.Cons (ONE t, lazy S.Nil))) -> (t, lazy S.Nil) | (lazy (S.Cons (ONE t, ts))) -> (t, (lazy (S.Cons (ZERO, ts)))) | (lazy (S.Cons (ZERO, ts))) -> let (NODE (_, t1, t2), ts') = unconsTree ts in (t1, lazy (S.Cons (ONE t2, ts'))) ;;
  • 14.
  • 15.