Anonymous user
Algebraic data types: Difference between revisions
→{{header|Racket}}
No edit summary |
|||
Line 580:
{{trans|OCaml}}
<
#lang racket
;; Using short names to make the code line up nicely
(struct
(define (balance t)
(match t
[(
[(N 'B (N 'R a x (N 'R b y c)) z d) (
[(
[(N 'B a x (N 'R b y (N 'R c z d))) (
[else t]))
Line 599 ⟶ 597:
(define (ins t)
(match t
['empty (
[(
(match (ins s) [(N _ l v r) (N 'B l
(define (visualize t0)
(let loop ([t t0] [last? #t] [indent '()])
(for-each display (reverse indent))
(printf "~a~a[~a]\n" (I "\\-" "+-") (N-value t) (N-color t))
(define subs (filter N? (list (N-left t) (N-right t))))
(for ([s subs] [n (in-range (sub1 (length subs)) -1 -1)])
(loop s (zero? n) (cons (I " " "| ") indent)))))
(visualize (for/fold ([t 'empty]) ([i 16]) (insert i t)))
</lang>
<pre>
7[B]
+-3[B]
| +-1[B]
| | +-0[B]
| | \-2[B]
| \-5[B]
| +-4[B]
| \-6[B]
\-11[B]
+-9[B]
| +-8[B]
| \-10[B]
\-13[B]
+-12[B]
\-14[B]
\-15[R]
</pre>
=={{header|Rascal}}==
|