0

Implement add-leaf, a Scheme procedure that takes in a tree instance t and a number x.

This procedure will return a new tree where for each non-leaf node in the tree, append a new leaf with the label x to that node's branches. If the input tree is a leaf, the resulting tree is unchanged.

Hints:

append will merge the elements in two Scheme lists together. For example, (append (list 1 2) (list 3 4)) results in (1 2 3 4) map applies a procedure onto every item in a Scheme list. For example, (map - (list 2 4)) results in (-2 -4)

You may find it useful to create a helper lambda function to pass in as the procedure to map To create a tree with no branches, you must specify nil for its branches. For example, (tree 5 nil) is a leaf with 5.

; Constructs tree given label and list of branches
(define (tree label branches)
  (if (null? tree)
      null
      (cons label branches)))

; Returns the label of the tree
(define (label t)
  (car t))

; Returns the list of branches of the given tree
(define (branches t)
  (cdr t))

; Returns #t if the given tree is a leaf, otherwise return #f
(define (is-leaf t)
  (null? (branches t)))

My code:

(define (add-leaf t x)
  (if (is-leaf t)
      null
      (begin
        (define mapped-branches
          (map (lambda (x) (append (list (x) (branches t))))))
        
        (tree mapped-branches
              (label (t))
              (append mapped-branches
                      (branches t) 
                      (add-leaf (branches t) x))))))

It complains that define: not allowed in an expression context in: (define mapped-branches (map (lambda (x) (append (list (x) (branches t))))))

Sylwester
  • 47,942
  • 4
  • 47
  • 79
Proteus Yi
  • 53
  • 5
  • There is no question in the question text. We have no idea what you want us to help you with :-/ – Sylwester Aug 29 '23 at 12:24
  • the last paragraph – Proteus Yi Aug 29 '23 at 12:26
  • Yes, and we still don't know what you are struggling with when it comes to that. Btw. `define` inside a procedure can only be before the body expression. Here it is inside an `if` form and that won't work. You can use `let` there instead though. – Sylwester Aug 29 '23 at 12:38
  • The problem is to append a new leaf with the label x to that node's branches. The problem give me a structure to fill but i just can't figure it out. – Proteus Yi Aug 29 '23 at 12:52
  • You should edit you question with the code you have made, what you are supplying to the procedure, what you get as result (error/ wrong structure) and the expected result. – Sylwester Aug 29 '23 at 14:48
  • sorry my bad, i just fixed it – Proteus Yi Aug 29 '23 at 15:41
  • That is what I mentioned. `define` cannot be anywhere else than top level and before the first expression in procedures. It's not allowed in an `if` – Sylwester Aug 29 '23 at 19:35
  • 1
    You need to review the proper syntax in more detail. For instance, `(list (x) (branches t))` is not a meaningful thing to pass to `append`. (Start small.) Also, "create a helper lambda function to pass in as the procedure to map" does not mean "create a named helper function that passes a lambda function to map". – molbdnilo Aug 30 '23 at 14:06
  • ^^^ "start small" +1. this means write short functions and test them independently of each other, at the evaluation prompt, to make sure each is working as intended. then you can write "bigger" functions with confidence, using those "small" functions. --- you can just place that define right at the start of your procedure, as its first "statement". – Will Ness Aug 30 '23 at 18:58

0 Answers0