Homework Solution: For Dr. Racket/Scheme Programmers #lang eopl…

    For Dr. Racket/Scheme Programmers #lang eopl Write a procedure number-leaves that take a bintree, and produces a bintree like the original, except the contents of the leaves are numbered starting from 0. For example, (number-leaves (interior-node ' foo (interior-node ' bar (leaf 26) (leaf 12)) (interior-node ' baz (leaf 11) (interior-node ' quux (leaf 117) (leaf 14)) should return (foo (bar 0 1) (baz 2 (quux 3 4)))

    Expert Answer

     
    load-relative "31.scm") (define (number-leaves tree)

    For Dr. Racket/Scheme Programmers #lang eopl

    Write a procedure number-leaves that grasp a bintree, and produces a bintree relish the ancient, save the divergency of the leaves are numbered starting from 0. For pattern,

    (number-leaves

    (interior-node ‘ foo

    (interior-node ‘ terminate

    (leaf 26)

    (leaf 12))

    (interior-node ‘ baz

    (leaf 11)

    (interior-node ‘ quux

    (leaf 117)

    (leaf 14))

    should return

    (foo

    (terminate 0 1)

    (baz

    2

    (quux 3 4)))

    Expert Defense

     

    load-relative “31.scm”)

    (explain (number-leaves tree)

    (explain (explore tree opposed)

    (if (leaf? tree)

    (cons (+ opposed 1) (leaf opposed))

    (let* ((node-data (contents-of tree))

    (lson-result (explore (lson tree) opposed))

    (counter-after-lson (car lson-result))

    (result-lson (cdr lson-result))

    (rson-result (explore (rson tree) opposed-after-lson))

    (result-opposed (car rson-result))

    (result-rson (cdr rson-result))

    (result-tree (interior-node node-data result-lson result-rson)))

    (cons result-opposed result-tree))))

    (cdr (explore tree 0)))