4034df6027cbecf3f4a19a711a248536

This is written in a dialect of lisp: scheme. The first 3 functions are helper functions, the main function is the last function. Basically it creates a list of varying depths by parsing a list of numbers. Each number represents the depth that the element in the new list should have. I'm hoping someone can come up with a more straight forward way to accomplish what the below code does.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
(define (adv-to-equal-node n L)
  (cond ((null? L) `())
	((= n (car L)) L)
	(else (adv-to-equal-node n (cdr L)))))

(define (subnode-of? n1 n2)
  (< n1 n2))

(define (equal-nodes? n1 n2)
  (= n1 n2))

; Take a list of numbers and build a list 
; where subnodes are 1 less than thier parent
; nodes on the same level are equal. 
; 
; (build-list `(0 1 1 2 2 1 1 2 1 0)
; => (0 (1 1 (2 2) 1 1 (2) 1) 0)
;
; (build-list `(0 1 2 3 4 5 1 0))
; => (0 (1 (2 (3 (4 (5)))) 1) 0)
;
; (build-list `(1 2 1 2))
; => (1 (2) 1 (2))
;
(define (build-list L)
  (define (helper n L)
    (cond ((null? L) `())
	  ((subnode-of? n (car L))
           (cons (cons (car L) 
		       (helper (car L) (cdr L)))
		 (helper n (adv-to-equal-node n (cdr L)))))
	  ((equal-nodes? n (car L))
	   (cons (car L)
		 (helper (car L) (cdr L))))
	  (else `())))
  (cons (car L) (helper (car L) (cdr L))))

Refactorings

No refactoring yet !

Your refactoring





Format Copy from initial code

or Cancel