Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>That was fun!</p> <p>Okay, I really hope that wasn't homework.</p> <p>It turns out that there's a very simple recursive solution. What you want at each level is to take a list of trees, gather them by pairs into one-deeper trees, then append the new leaves at this level. This could be written using 'foldr', but I thought it would be a bit less clear.</p> <p>I should clarify the input a bit; on the page you mention, the specifications look like</p> <p>leaves at level 0 :<br> leaves at level 1 :<br> leaves at level 2 : x23, x42, x23<br> leaves at level 3 : x24, x23 </p> <p>This would correspond to the input</p> <p>'(() () (x23 x42 x23) (x24 x23))</p> <p>to the program below.</p> <p>Also, the only thing going on here is the mapping of this table to the binary tree, which will be of help only when decoding. For encoding, this binary tree will be useless.</p> <p>Finally, a big shout-out to <a href="http://www.htdp.org" rel="nofollow">How To Design Programs</a>; I followed the design recipe carefully, dotting all my i's and crossing all my t's. Test cases first, please!</p> <p>Cheers!</p> <p>John Clements</p> <pre><code>#lang racket (require rackunit) ;; a tree is either ;; a symbol, or ;; (list tree tree) ;; a specification is ;; (listof (listof symbol)) ;; spec-&gt;tree : specification -&gt; tree ;; run spec-&gt;treelist, ensure that it's a list of length 1, return it. (define (spec-&gt;tree spec) (match (spec-&gt;treelist spec) [(list tree) tree] [other (error 'spec-&gt;tree "multiple trees produced")])) ;; spec-&gt;treelist : specification -&gt; (listof tree) ;; given a *legal* specification, produce ;; the corresponding tree. ONLY WORKS FOR LEGAL SPECIFICATIONS... (define (spec-&gt;treelist spec) (cond [(empty? spec) empty] [else (append (first spec) (gather-pairs (spec-&gt;treelist (rest spec))))])) ;; go "up one level" by grouping each pair of trees into one tree. ;; The length of the list must be a number divisible by two. (define (gather-pairs trees) (match trees [(list) empty] [(list-rest a b remaining) (cons (list a b) (gather-pairs remaining))] [other (error 'gather "improperly formed specification")])) ;; TEST CASES (check-equal? (gather-pairs '(a b c d)) '((a b) (c d))) (check-equal? (spec-&gt;treelist '((top))) '(top)) (check-equal? (spec-&gt;treelist '(() (two-a two-b))) '((two-a two-b))) (check-equal? (spec-&gt;treelist '(() (two-a) (three-a three-b))) '((two-a (three-a three-b)))) (check-equal? (spec-&gt;treelist '(() () (three-a three-b three-c) (four-a four-b))) '(((three-a three-b) (three-c (four-a four-b))))) (check-equal? (spec-&gt;tree '(() () (three-a three-b three-c) (four-a four-b))) '((three-a three-b) (three-c (four-a four-b)))) </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.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      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