(def atom?
(fn [a]
(not (seq? a))))
(println "atom?")
(println (atom? 'a))
(println (atom? '()))
(println (atom? (quote thai)))
(def null?
(fn [a]
(or
(nil? a)
(= () a))))
(def not_
(fn [b]
(cond
b false
true true)))
(println "")
(println "not_")
(println (not_ true))
(println (not_ false))
(println (not_ (= 1 1)))
(println (not_ (= 'a 'b)))
(def non-atom?
(fn [s]
(not_ (atom? s))))
(println "")
(println "non-atom?")
(println (non-atom? 'a))
(println (non-atom? '(a)))
(println (non-atom? (quote thai)))
(def leftmost
(fn [l]
(cond
(null? l) '()
(non-atom? (first l)) (leftmost (first l))
true (first l))))
(println "")
(println "leftmost")
(println (leftmost (quote ((((pad))thai))chicken())))
(def rember*
(fn [a l]
(cond
(null? l) '()
(non-atom? (first l)) (cons (rember* a (first l)) (rember* a (rest l)))
true (cond
(= (first l) a) (rember* a (rest l))
true (cons (first l) (rember* a (rest l)))))))
(println "")
(println "rember*")
(println (rember* 'bacon '(((bbq sauce)) (with (egg and (bacon))))))
(def insertR*
(fn [new old l]
(cond
(null? l) '()
(non-atom? (first l)) (cons (insertR* new old (first l)) (insertR* new old (rest l)))
true (cond
(= (first l) old) (cons old (cons new (insertR* new old (rest l))))
true (cons (first l) (insertR* new old (rest l)))))))
(println "")
(println "insertR*")
(println (insertR* 'chicken 'baked '(((baked)) (with roast) vegetables)))
(def zero_?
(fn [n]
(= 0 n)))
(def add1
(fn [n]
(+ 1 n)))
(def sub1
(fn [n]
(- n 1)))
(def +_
(fn [n m]
(cond
(zero_? m) n
true (add1 (+ n (sub1 m))))))
(def occur*
(fn [a l]
(cond
(null? l) 0
(non-atom? (first l)) (+_ (occur* a (first l)) (occur* a (rest l)))
true (cond
(= (first l) a) (add1 (occur* a (rest l)))
true (occur* a (rest l))))))
(println "")
(println "occur*")
(println (occur* 'creamy '(((creamy)) new (york (cheesecake)) with a ((creamy) latte))))
(def subst*
(fn [new old l]
(cond
(null? l) '()
(non-atom? (first l)) (cons (subst* new old (first l)) (subst* new old (rest l)))
true (cond
(= (first l) old) (cons new (subst* new old (rest l)))
true (cons (first l) (subst* new old (rest l)))))))
(println "")
(println "subst*")
(println (subst* 'baked 'creamy '(((creamy) cheesecake) (with (hot (espresso))))))
(def insertL*
(fn [new old l]
(cond
(null? l) '()
(non-atom? (first l))
(cons
(insertL* new old (first l))
(insertL* new old (rest l)))
true (cond
(= (first l) old)
(cons new
(cons old
(insertL*
new old (rest l))))
true (cons (first l)
(insertL*
new old (rest l)))))))
(println "")
(println "insertL*")
(println (insertL* 'fresh 'creamy '(((creamy) cheesecake) (with (hot (espresso))))))
(def member*
(fn [a l]
(cond
(null? l) false
(non-atom? (first l))
(or
(member* a (first l))
(member* a (rest l)))
true (or
(= (first l) a)
(member* a (rest l))))))
(println "")
(println "member*")
(println (member* 'creamy '(((creamy) cheesecake) (with (hot (espresso))))))
(def member*
(fn [a l]
(cond
(null? l) false
(atom? (first l))
(or
(= (first l) a)
(member* a (rest l)))
true (or
(member* a (first l))
(member* a (rest l))))))
(println "")
(println "member* - without non-atom?")
(println (member* 'creamy '(((creamy) cheesecake) (with (hot (espresso))))))
(def >_
(fn [n m]
(cond
(zero_? n) false
(zero_? m) true
true (>_ (sub1 n) (sub1 m)))))
(println "")
(println ">_")
(println (>_ 10 1))
(println (>_ 1 10))
(def =_
(fn [n m]
(cond
(>_ n m) false
true true)))
(println "")
(println "=_")
(println (=_ 1 10))
(println (=_ 10 1))
(println (=_ 10 10))
(def eqan?
(fn [a1 a2]
(cond
(number? a1)
(cond
(number? a2) (=_ a1 a2)
true false)
(number? a2) false
true (= a1 a2))))
(println "")
(println "eqan?")
(println (eqan? 1 10))
(println (eqan? 10 1))
(println (eqan? 10 10))
(println (eqan? 'a 'b))
(println (eqan? 'a 'a))
(def eqlist?
(fn [l1 l2]
(cond
(and (null? l1) (null? l2)) true
(or (null? l1) (null? l2)) false
(and (non-atom? (first l1)) (non-atom? (first l2)))
(and (eqlist? (first l1) (first l2))
(eqlist? (rest l1) (rest l2)))
(or (non-atom? (first l1)) (non-atom? (first l2))) false
true (and
(eqan? (first l1) (first l2))
(eqlist? (rest l1) (rest l2))))))
(println "")
(println "eqlist?")
(println (eqlist? '(with (hot (espresso))) '(with (hot (espresso)))))
(println (eqlist? '(with (hot (espresso))) '((creamy) cheesecake)))
(def equal?
(fn [s1 s2]
(cond
(and (atom? s1) (atom? s2))
(eqan? s1 s2)
(and (non-atom? s1) (non-atom? s2))
(eqlist? s1 s2)
true false)))
(println "")
(println "equal?")
(println (equal? '(hot espresso coffee) '(creamy cheesecake)))
(println (equal? '(creamy cheesecake) '(creamy cheesecake)))
(def eqlist?
(fn [l1 l2]
(cond
(and (null? l1) (null? l2)) true
(or (null? l1) (null? l2)) false
true (and
(equal? (first l1) (first l2))
(eqlist? (rest l1) (rest l2))))))
(println "")
(println "eqlist? refactored")
(println (eqlist? '(with (hot (espresso))) '(with (hot (espresso)))))
(println (eqlist? '(with (hot (espresso))) '((creamy) cheesecake)))
(def rember
(fn [s l]
(cond
(null? l) '()
(non-atom? (first l))
(cond
(equal? (first l) s) (rest l)
true (cons (first l) (rember s (rest l))))
true (cond
(equal? (first l) s) (rest l)
true (cons (first l) (rember s (rest l)))))))
(println "")
(println "rember")
(println (rember 'fresh '(((fresh creamy) cheesecake) (with (hot (espresso))))))
(println (rember 'fresh '(fresh creamy cheesecake with hot espresso)))
(def rember
(fn [s l]
(cond
(null? l) '()
true (cond
(equal? (first l) s) (rest l)
true (cons (first l) (rember s (rest l)))))))
(println "")
(println "rember - refactored")
(println (rember 'fresh '(((fresh creamy) cheesecake) (with (hot (espresso))))))
(println (rember 'fresh '(fresh creamy cheesecake with hot espresso)))
(def rember
(fn [s l]
(cond
(null? l) '()
(equal? (first l) s) (rest l)
true (cons (first l) (rember s (rest l))))))
(println "")
(println "rember - refactored again")
(println (rember 'fresh '(((fresh creamy) cheesecake) (with (hot (espresso))))))
(println (rember 'fresh '(fresh creamy cheesecake with hot espresso)))
(def insertL*
(fn [new old l]
(cond
(null? l) '()
(non-atom? (first l))
(cons
(insertL* new old (first l))
(insertL* new old (rest l)))
(= (first l) old)
(cons new (cons old (insertL* new old (rest l))))
true (cons (first l) (insertL* new old (rest l))))))
(println "")
(println "insertL* - refactored")
(println (insertL* 'fresh 'creamy '(((creamy) cheesecake) (with (hot (espresso))))))