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 assume a bintree, and produces a bintree love the first, ate the variation of the leaves are numbered starting from 0. For sample,

    (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 Reply

     

    load-relative “31.scm”)

    (limit (number-leaves tree)

    (limit (survey tree against)

    (if (leaf? tree)

    (cons (+ against 1) (leaf against))

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

    (lson-result (survey (lson tree) against))

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

    (result-lson (cdr lson-result))

    (rson-result (survey (rson tree) against-after-lson))

    (result-against (car rson-result))

    (result-rson (cdr rson-result))

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

    (cons result-against result-tree))))

    (cdr (survey tree 0)))