( { 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)))) (global temp 0) { ugly but necessary in the absence of local variables } (fun gen (n) { generate the list [1,2,...,n] } (block (:= temp nil) (while n (block (:= temp (@cons n temp)) (:= n (- n 1)))) temp)) (fun length (l) { return the length of l } (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)))) { Sample solutions in recursive style. } (fun minx (l minsofar) (if (@isnil l) minsofar (@minx (@tail l) (if (<= (@head l) minsofar) (@head l) minsofar)))) (fun min (l) { return the minimum element of l } (@minx (@tail l) (@head l))) (fun zip (l1 l2) { pair the elements of l1 and l2 } (if (@isnil l1) nil (if (@isnil l2) nil (@cons (pair (@head l1) (@head l2)) (@zip (@tail l1) (@tail l2)))))) { Alternative solutions in imperative style.... (fun min (l) { return the minimum element of l } (block (:= temp (@head l)) (:= l (@tail l)) (while (@not (@isnil l)) (block (if (<= (@head l) temp) (:= temp (@head l)) (block)) (:= l (@tail l)))) temp)) (fun zip (l1 l2) { pair the elements of l1 and l2 } (block (:= temp nil) (while (@and (@not (@isnil l1)) (@not (@isnil l2))) (block (:= temp (@append temp (@list1 (pair (@head l1) (@head l2))))) (:= l1 (@tail l1)) (:= l2 (@tail l2)))) temp)) } ) (@list3 (@min (@list3 5 2 10)) (@zip (@list3 1 2 3) (@list3 4 5 6)) (@zip (@list1 1) (@list3 4 5 6)))