Note that there are some explanatory texts on larger screens.

plurals
  1. POF# PurelyFunctionalDataStructures WeightBiasedLeftistHeap ex 3.4
    primarykey
    data
    text
    <p>I'm working on Okasaki's Purely Functional Data Structures and trying to build F# implementations of things. I'm also going through the exercises listed in the book (some are pretty challenging). Well I'm stuck on exercise 3.4 which calls for modifying the merge function of the WeightBiasedLeftistHeap such that it executes in a single pass as opposed to the original 2 pass implementation.</p> <p>I haven't been able to figure out how to do this yet and was hoping for some suggestions. There was another <a href="https://stackoverflow.com/questions/3526117/weight-biased-leftist-heaps-advantages-of-top-down-version-of-merge">post here on SO</a> where a guy does it in SML by pretty much inlining the makeT function. I started out going this route (in the commented section 3.4 First Try. But abandoned that approach because I thought that this really wasn't executing in a single pass (it still goes 'till reaching a leaf then unwinds and rebuilds the tree). Am I wrong in interpreting that as still being a two pass merge?</p> <p><a href="https://www.assembla.com/code/purelyfunctionaldatastructuresinfsharp/subversion/nodes/trunk/PurelyFunctionalDataStructures/Chap3/Heaps.fs?rev=33" rel="nofollow noreferrer">Here is a link to my complete implementation of WeightBiasedLeftistHeap.</a></p> <p>Here are my failed attempts to do this in F#:</p> <pre><code>type Heap&lt;'a&gt; = | E | T of int * 'a * Heap&lt;'a&gt; * Heap&lt;'a&gt; module WeightBiasedLeftistHeap = exception EmptyException let weight h = match h with | E -&gt; 0 | T(w, _,_,_) -&gt; w let makeT x a b = let weightA = weight a let weightB = weight b if weightA &gt;= weightB then T(weightA + weightB + 1, x, a, b) else T(weightA + weightB + 1, x, b, a) // excercise 3.4 first try // let rec merge3_4 l r = // match l,r with // | l,E -&gt; l // | E,r -&gt; r // | T(_, lx, la, lb) as lh, (T(_, rx, ra, rb) as rh) -&gt; // if lx &lt;= rx then // let right = merge3_4 lb rh // let weightA = weight la // let weightB = weight right // // if weightA &gt;= weightB then // T(weightA + weightB + 1, lx, la, right) // else // T(weightA + weightB + 1, lx, right, la) // else // let right = merge3_4 lh rb // let weightA = weight ra // let weightB = weight right // // if weightA &gt;= weightB then // T(weightA + weightB + 1, rx, ra, right) // else // T(weightA + weightB + 1, rx, right, ra) // excercise 3.4 second try (fail!) // this doesn't work, I couldn't figure out how to do this in a single pass let merge3_4 l r = let rec merge' l r value leftChild = match l,r with | l,E -&gt; makeT value leftChild l | E,r -&gt; makeT value leftChild r | T(_, lx, la, lb) as lh, (T(_, rx, ra, rb) as rh) -&gt; if lx &lt;= rx then merge' lb rh lx la //(fun h -&gt; makeT(lx, la, h)) else merge' lh rb rx ra //(fun h -&gt; makeT(rx, ra, h)) match l, r with | l, E -&gt; l | E, r -&gt; r | T(_, lx, la, lb) as lh, (T(_, rx, ra, rb) as rh) -&gt; let lf = fun h -&gt; makeT(lx, la, h) if lx &lt;= rx then merge' lb rh lx la // (fun h -&gt; makeT(lx, la, h)) else merge' lh rb rx ra // (fun h -&gt; makeT(rx, ra, h)) let rec merge l r = match l,r with | l,E -&gt; l | E,r -&gt; r | T(_, lx, la, lb) as lh, (T(_, rx, ra, rb) as rh) -&gt; if lx &lt;= rx then makeT lx la (merge lb rh) else makeT rx ra (merge lh rb) let insert3_4 x h = merge3_4 (T(1,x,E,E)) h </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload