( { Basic boolean support } (global false 0) (global true 1) (fun not (x) (if x false true)) (fun and (x y) (if x y x)) (fun or (x y) (if x x y)) { Equality on integers } (fun eq (x y) (@and (<= x y) (<= y x))) { Lists } (global nil 0) { the empty list } (fun cons (h t) (pair h t)) { construct new list from old } (fun head (l) (fst l)) { extract the head } (fun tail (l) (snd l)) { extract the tail } (fun isnil (l) (@not (ispair l))) { test for empty } (fun list1 (x) (@cons x nil)) { convenience functions } (fun list2 (x y) (@cons x (@cons y nil))) (fun list3 (x y z) (@cons x (@cons y (@cons z nil)))) (fun gen (n) { generate the list [1,2,...,n] } (local (temp 0) (block (:= temp nil) (while n (block (:= temp (@cons n temp)) (:= n (- n 1)))) temp))) (fun length (l) { return the length of l } (local (temp 0) (block (:= temp 0) (while (@not (@isnil l)) (block (:= temp (+ temp 1)) (:= l (@tail l)))) temp))) (fun append (l m) { return (l @ m) } (if (@isnil l) m (@cons (@head l) (@append (@tail l) m)))) { recursive functional solution } (fun nrevaux (l m) (if (@isnil l) m (@nrevaux (@tail l) (setsnd l m)))) (fun nreverse (l) (@nrevaux l nil)) { alternative imperative solution ... (fun nreverse (l) (local (w nil) (block (while (@not (@isnil l)) (local (temp (@tail l)) (block (setsnd l w) (:= w l) (:= l temp)))) w))) } ) (@nreverse (@list3 1 2 3))