So I am using DrRacket and have a struct defined as:
(define-struct thing (a b))
Then, I have an example tree of this format:
(define v (make-thing
(make-thing (make-thing 1 2)
(make-thing 3 4))
(make-thing (make-thing 5 6)
(make-thing 7 8))))
I need to build a function which takes in a non-positive number which is also a power of 2 (so like 1,2,4,8,16...) and either outputs the number 1 (if n=1) or makes a 'thing' in the format as above.
So far I have tried a lot of ways but I either end up getting something like:
(make-thing (make-thing (make-thing 1 1)
(make-thing 1 1))
(make-thing (make-thing 1 1)
(make-thing 1 1))))
I cannot figure out how to increment the variables properly - I've been able to figure out how to apply the required number of make-thing
s:
Here's the code I use right now:
(define (helper pow current)
(cond
((= pow 0) 1)
(else (make-thing
(helper (- pow 1) current)
(helper (- pow 1) (- current 1))))))
(define current 1)
(define (build-thing-or-number n)
(cond
((= n 1) 1)
(else (make-thing
(helper (- (log n 2) 1) current)
(helper (- (log n 2) 1) (add1 current))))))