SlideShare une entreprise Scribd logo
1  sur  17
Télécharger pour lire hors ligne
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

Contenu connexe

Similaire à PFDS 9.2.3 Lazy Representations

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

Similaire à 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
 

Plus de 昌平 村山 (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
 

Dernier

Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
WSO2
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Victor Rentea
 

Dernier (20)

Platformless Horizons for Digital Adaptability
Platformless Horizons for Digital AdaptabilityPlatformless Horizons for Digital Adaptability
Platformless Horizons for Digital Adaptability
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
 
Vector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxVector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptx
 
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
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
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
 
Six Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal OntologySix Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal Ontology
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 

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.