36 lines
819 B
Text
36 lines
819 B
Text
;; helper function that finds the rest that are less than or equal
|
|
(define (rest-less-eq x ls)
|
|
(cond
|
|
((null? ls) #f)
|
|
((<= (car ls) x) ls)
|
|
(else (rest-less-eq x (cdr ls)))))
|
|
|
|
;; nest the input as a tree
|
|
(define (make-tree input depth)
|
|
(cond
|
|
((null? input) '())
|
|
((eq? input #f ) '())
|
|
((= depth (car input))
|
|
(cons (car input)(make-tree(cdr input) depth)))
|
|
((< depth (car input))
|
|
(cons (make-tree input (+ depth 1))
|
|
(make-tree (rest-less-eq depth input) depth)))
|
|
(#t '())
|
|
))
|
|
|
|
(define examples
|
|
'(()
|
|
(1 2 4)
|
|
(3 1 3 1)
|
|
(1 2 3 1)
|
|
(3 2 1 3)
|
|
(3 3 3 1 1 3 3 3)))
|
|
|
|
(define (run-examples x)
|
|
(if (null? x) '()
|
|
(begin
|
|
(display (car x))(display " -> ")
|
|
(display (make-tree(car x) 1))(display "\n")
|
|
(run-examples (cdr x)))))
|
|
|
|
(run-examples examples)
|