# MATH illustrate lists and some list operators
# to make list describable as a function, need to preceed lists
# ... with an argument count
# Lists keep an explicit record of their length
# this is to avoid the need for using a special 'nil' symbol
# ... which cannot itself be placed in the list.
# pending: should introduce number? check function
[hear] (define list-helper /
? n /
? ret /
if (> (n) 1)
(? x /
list-helper
(- (n) 1)
(? y /
? z /
ret (+ 1 (y)) (cons (x) (z))))
(? x /
ret 1 (x)));
[hear] (define list /
? n /
if (= (n) 0)
(cons 0 0)
(list-helper (n) (? y / ? z / cons (y) (z))));
[hear] (define head /
? lst /
if (= (car / lst) 0)
(undefined)
(if (= (car / lst) 1)
(cdr /
lst)
(car /
cdr /
lst)));
[hear] (define tail /
? lst /
if (= (car / lst) 0)
(undefined)
(if (= (car / lst) 1)
(cons 0 0)
(cons (- (car / lst) 1) (cdr / cdr / lst))));
[hear] (define list-length / ? lst / car / lst);
[hear] (define list-ref /
? lst /
? n /
if (= (list-ref / lst) 0)
(undefined)
(if (= (n) 0)
(head /
lst)
(list-ref (tail / lst) (- (n) 1))));
[hear] (define prepend /
? x /
? lst /
if (= (list-length / lst) 0)
(cons 1 (x))
(cons (+ (list-length / lst) 1)
(cons (x) (cdr / lst))));
[hear] (define equal /
? x /
? y /
if (= (number? (x)) (number? (y)))
(if (number? (x)) (= (x) (y)) (list= (x) (y)))
(false));
[hear] (define list= /
? x /
? y /
if (= (list-length / x) (list-length / y))
(if (> (list-length / x) 0)
(and (equal (head / x) (head / y))
(list= (tail / x) (tail / y)))
(true))
(false));
[hear] (= (list-length / (list 0)) 0);
[hear] (= (list-length / (list 4) 6 1 0 4) 4);
[hear] (= (list-length / (list 6) 6 2 7 0 9 4) 6);
[hear] (= (list-length / (list 2) 4 9) 2);
[hear] (= (list-length / (list 3) 6 1 7) 3);
[hear] (= (head / (list 6) 12 11 10 4 1 5) 12);
[hear] (list= (tail /
(list 6) 12 11 10 4 1 5)
((list 5) 11 10 4 1 5));
[hear] (= (head / (list 8) 15 13 12 7 10 11 13 18) 15);
[hear] (list= (tail /
(list 8) 15 13 12 7 10 11 13 18)
((list 7) 13 12 7 10 11 13 18));
[hear] (= (head / (list 2) 11 1) 11);
[hear] (list= (tail / (list 2) 11 1) ((list 1) 1));
[hear] (= (head / (list 6) 5 19 4 16 6 11) 5);
[hear] (list= (tail /
(list 6) 5 19 4 16 6 11)
((list 5) 19 4 16 6 11));
[hear] (= (head /
(list 10) 12 18 7 4 9 18 6 16 6 18)
12);
[hear] (list= (tail /
(list 10) 12 18 7 4 9 18 6 16 6 18)
((list 9) 18 7 4 9 18 6 16 6 18));
[hear] (= (head / (list 6) 19 7 3 10 19 13) 19);
[hear] (list= (tail /
(list 6) 19 7 3 10 19 13)
((list 5) 7 3 10 19 13));
[hear] (= (head / (list 6) 19 7 19 12 16 13) 19);
[hear] (list= (tail /
(list 6) 19 7 19 12 16 13)
((list 5) 7 19 12 16 13));
[hear] (= (head / (list 1) 3) 3);
[hear] (list= (tail / (list 1) 3) ((list 0)));
[hear] (= (head / (list 3) 2 19 17) 2);
[hear] (list= (tail /
(list 3) 2 19 17)
((list 2) 19 17));
[hear] (= (head / (list 7) 1 16 5 14 6 19 2) 1);
[hear] (list= (tail /
(list 7) 1 16 5 14 6 19 2)
((list 6) 16 5 14 6 19 2));
[hear] (= (list-ref ((list 3) 18 14 17) 1) 14);
[hear] (= (list-ref ((list 3) 8 11 10) 2) 10);
[hear] (= (list-ref ((list 8) 15 0 4 9 9 2 10 17) 3) 9);
[hear] (= (list-ref ((list 7) 4 8 8 5 14 5 13) 4) 14);
[hear] (= (list-ref ((list 4) 1 4 7 18) 2) 7);
[hear] (= (list-ref ((list 3) 12 2 3) 1) 2);
[hear] (= (list-ref ((list 6) 12 5 7 15 7 16) 2) 7);
[hear] (= (list-ref ((list 8) 5 15 7 14 7 1 11 19) 0) 5);
[hear] (= (list-ref ((list 3) 19 17 8) 2) 8);
[hear] (= (list-ref ((list 4) 10 10 4 11) 1) 10);
[hear] (list= ((list 0)) ((list 0)));
[hear] (list= ((list 1) 4) ((list 1) 4));
[hear] (list= ((list 2) 7 5) ((list 2) 7 5));
[hear] (list= ((list 3) 15 13 11) ((list 3) 15 13 11));
[hear] (list= ((list 4) 2 8 0 6) ((list 4) 2 8 0 6));
# this next batch of examples are a bit misleading, should streamline
[hear] (not / list= ((list 0)) ((list 1) 9));
[hear] (not / list= ((list 0)) ((list 1) 5));
[hear] (not / list= ((list 1) 18) ((list 2) 8 18));
[hear] (not / list= ((list 1) 18) ((list 2) 18 5));
[hear] (not /
list= ((list 2) 11 18) ((list 3) 7 11 18));
[hear] (not /
list= ((list 2) 11 18) ((list 3) 11 18 6));
[hear] (not /
list= ((list 3) 7 19 17) ((list 4) 6 7 19 17));
[hear] (not /
list= ((list 3) 7 19 17) ((list 4) 7 19 17 0));
[hear] (not /
list= ((list 4) 10 0 11 1)
((list 5) 0 10 0 11 1));
[hear] (not /
list= ((list 4) 10 0 11 1)
((list 5) 10 0 11 1 8));
# some helpful functions
[hear] (list= (prepend 8 ((list 0))) ((list 1) 8));
[hear] (list= (prepend 11 ((list 1) 8)) ((list 2) 11 8));
[hear] (list= (prepend 13 ((list 2) 1 12))
((list 3) 13 1 12));
[hear] (list= (prepend 0 ((list 3) 7 7 5))
((list 4) 0 7 7 5));
[hear] (list= (prepend 16 ((list 4) 16 0 19 3))
((list 5) 16 16 0 19 3));
[hear] (list= (prepend 10 ((list 5) 5 6 7 9 10))
((list 6) 10 5 6 7 9 10));
[hear] (list= (prepend 19 ((list 6) 3 19 18 6 10 16))
((list 7) 19 3 19 18 6 10 16));
[hear] (list= (prepend 19 ((list 7) 17 17 10 1 18 12 14))
((list 8) 19 17 17 10 1 18 12 14));
[hear] (define pair /
? x /
? y /
(list 2) (x) (y));
[hear] (define first / ? lst / head / lst);
[hear] (define second /
? lst /
head /
tail /
lst);
[hear] (list= (pair 3 6) ((list 2) 3 6));
[hear] (= (first / pair 3 6) 3);
[hear] (= (second / pair 3 6) 6);
[hear] (list= (pair 4 9) ((list 2) 4 9));
[hear] (= (first / pair 4 9) 4);
[hear] (= (second / pair 4 9) 9);
[hear] (list= (pair 8 3) ((list 2) 8 3));
[hear] (= (first / pair 8 3) 8);
[hear] (= (second / pair 8 3) 3);
[hear] (define list-find-helper /
? lst /
? key /
? fail /
? idx /
if (= (list-length / lst) 0)
(fail 0)
(if (equal (head / lst) (key))
(idx)
(list-find-helper
(tail /
lst)
(key)
(fail)
(+ (idx) 1))));
[hear] (define list-find /
? lst /
? key /
? fail /
list-find-helper (lst) (key) (fail) 0);
[hear] (define example-fail / ? x 100);
[hear] (= (list-find ((list 1) 13) 13 (example-fail)) 0);
[hear] (= (list-find
((list 10) 0 9 8 16 15 14 17 5 9 2)
15
(example-fail))
4);
[hear] (= (list-find ((list 3) 7 4 10) 7 (example-fail))
0);
[hear] (= (list-find
((list 6) 0 17 10 13 11 5)
17
(example-fail))
1);
[hear] (= (list-find ((list 3) 12 9 6) 12 (example-fail))
0);
[hear] (= (list-find
((list 7) 17 1 4 17 14 13 13)
14
(example-fail))
4);
[hear] (= (list-find ((list 3) 2 15 2) 15 (example-fail))
1);
[hear] (= (list-find
((list 9) 6 13 10 8 10 9 6 15 18)
13
(example-fail))
1);
[hear] (= (list-find ((list 3) 12 16 0) 12 (example-fail))
0);
[hear] (= (list-find ((list 1) 15) 15 (example-fail)) 0);
[hear] (= (list-find
((list 4) 2 17 11 5)
14
(example-fail))
100);
[hear] (= (list-find
((list 6) 12 1 19 6 17 9)
2
(example-fail))
100);
[hear] (= (list-find
((list 8) 11 6 17 8 13 10 9 16)
19
(example-fail))
100);