mirror of
https://github.com/usatiuk/psil.git
synced 2025-10-28 10:47:49 +01:00
reverse tree
This commit is contained in:
40
clitests/reverse-tree.psil
Normal file
40
clitests/reverse-tree.psil
Normal file
@@ -0,0 +1,40 @@
|
||||
(define (equal? a b)
|
||||
(if (nil? a) (if (nil? b) 1 0)
|
||||
(if (atom a)
|
||||
(if (atom b) (= a b) 0)
|
||||
(if (atom b) 0 (if (equal? (car a) (car b)) (equal? (cdr a) (cdr b)) 0))
|
||||
)
|
||||
)
|
||||
)
|
||||
|
||||
(define (empty? l)
|
||||
(equal? l (quote()))
|
||||
)
|
||||
|
||||
(define (reverse-tree bst)
|
||||
(if (empty? bst)
|
||||
(nil)
|
||||
(cons
|
||||
(car bst)
|
||||
(cons
|
||||
(reverse-tree (car (cdr (cdr bst))))
|
||||
(cons (reverse-tree (car (cdr bst))) (nil))
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
|
||||
(reverse-tree (quote()))
|
||||
(equal? (reverse-tree (quote())) (nil))
|
||||
|
||||
(reverse-tree (quote(1 () ())))
|
||||
(equal? (reverse-tree (quote(1 () ()))) (quote(1 () ())))
|
||||
|
||||
(reverse-tree (quote(1 (2 () ()) ())))
|
||||
(equal? (reverse-tree (quote(1 (2 () ()) ()))) (quote(1 () (2 () ()))))
|
||||
|
||||
(reverse-tree (quote(2 (1 () ()) (3 () ()))))
|
||||
(equal? (reverse-tree (quote(2 (1 () ()) (3 () ())))) (quote(2 (3 () ()) (1 () ()))))
|
||||
|
||||
(reverse-tree (quote(10 (5 (3 () ()) ()) (20 () (25 () ())))))
|
||||
(equal? (reverse-tree (quote(10 (5 (3 () ()) ()) (20 () (25 () ()))))) (quote(10 (20 (25 () ()) ()) (5 () (3 () ())))))
|
||||
9
clitests/reverse-tree.psil.ex
Normal file
9
clitests/reverse-tree.psil.ex
Normal file
@@ -0,0 +1,9 @@
|
||||
1
|
||||
(1 () ())
|
||||
1
|
||||
(1 () (2 () ()))
|
||||
1
|
||||
(2 (3 () ()) (1 () ()))
|
||||
1
|
||||
(10 (20 (25 () ()) ()) (5 () (3 () ())))
|
||||
1
|
||||
@@ -69,6 +69,8 @@ struct ConsCell : public Cell {
|
||||
} else {
|
||||
_car.load()->print(res);
|
||||
}
|
||||
} else {
|
||||
res << "()";
|
||||
}
|
||||
if (_cdr) {
|
||||
if (_cdr.load()->_type == CellType::CONS) {
|
||||
@@ -100,7 +102,7 @@ struct ConsCell : public Cell {
|
||||
_car.load()->print(res);
|
||||
}
|
||||
} else {
|
||||
res << "null ";
|
||||
res << "()";
|
||||
}
|
||||
if (_cdr) {
|
||||
if (_cdr.load()->_type == CellType::CONS) {
|
||||
|
||||
Reference in New Issue
Block a user