0

I am trying to build a function that encodes the following procedure at the bottom into 1's and 0's can anyone help find why I am getting the error message mpair given: 0.

(define (symbols tree)
  (if (leaf? tree)
      (list (symbol-leaf tree))
      (caddr tree)))
(define (right-branch tree) (cadr tree))
(define (left-branch tree) (car tree))
(define (leaf? object)
  (eq? (car object) 'leaf))
(define (member? x set)
  (not (equal? (member x set) false)))
(define (encode-branch symbol tree)
  (let ((left (left-branch tree))
        (right (right-branch tree)))
    (cond ((member? symbol (symbols left)) (list 0 left))
          ((member? symbol (symbols right)) (list 1 right))
          (else (error symbol tree)))))
(define (encode symbol tree)
  (if (leaf? tree) '()
      (let ((new-branch (encode-branch symbol tree)))
        (cons (car new-branch) (encode symbol (cadr new-branch))))))

(encode "doo doo doo da da da da" '((da . 00) (doo . 01) (ron . 1)))

ceving
  • 21,900
  • 13
  • 104
  • 178
dscarf
  • 34
  • 1
  • 5
  • `'((da . 00) (doo . 01) (ron . 1))` doesn't look like it fits with your definition of a tree. The left branch is `'(da . 00)`, right is `'(doo . 01)`, and `'(ron . 1)` is its "symbols", and it has no leaves. – molbdnilo Nov 07 '17 at 06:32
  • The example lacks the definition of `symbol-leaf`. – ceving Nov 09 '17 at 12:33
  • I just answer similer question.[Encoding Huffman Tree Scheme](https://stackoverflow.com/a/61161062/11479158) – tjorchrt Apr 11 '20 at 18:10

0 Answers0