96 lines
2.4 KiB
Plaintext
96 lines
2.4 KiB
Plaintext
;; ------------ Boolean Logic --------------------
|
|
;; Boolean logic: and, or, not, nor:
|
|
|
|
(def and (lm (a b) (nand (nand a b) (nand a b))))
|
|
(def not (lm (a) (nand a a)))
|
|
(def or (lm (a b) (nand (nand a a) (nand b b))))
|
|
(def nor (lm (a b) (not (or a b))))
|
|
|
|
;; ------------ Relational Operators -------------
|
|
;; equal, not euql, lower, greater w/ equal:
|
|
|
|
(def gt (lm (a b) (lt b a)))
|
|
(def neq (lm (a b) (not (eq a b))))
|
|
(def geq (lm (a b) (or (eq a b) (gt a b))))
|
|
(def eq (lm (a b) (and (not (lt a b)) (not (lt b a)))))
|
|
(def leq (lm (a b) (or (eq a b) (lt a b))))
|
|
|
|
;; ------------ arithmetic operators -------------
|
|
;; Functions to implement arithmetic based on
|
|
;; (Z,+,inv).
|
|
;; Implemented: sub, mult, mod, div, sum, inc, dec
|
|
|
|
;; sub: a - b:
|
|
(def sub (lm (a b) (add a (inv b))))
|
|
|
|
;; mult: a * b
|
|
(def mult (lm (a b)
|
|
(if (eq b 1)
|
|
a
|
|
(add a (mult a (sub b 1))))))
|
|
|
|
;; div: a / b (result: integer)
|
|
(def div (lm (a b)
|
|
(if (geq a b) (add 1 (div (sub a b) b)) 0)))
|
|
|
|
|
|
(def modsubr (lm (a b last)
|
|
(if (lt a 0) last
|
|
(modsubr (sub a b) b a))))
|
|
;; mod: a mod b
|
|
(def mod (lm (a b) (modsubr a b 0)))
|
|
|
|
;; sum: sum(a, b, 0)
|
|
(def sumarg (lm (a b erg)
|
|
(if (gt a b)
|
|
erg
|
|
(sumarg (add a 1) b (add erg a)))))
|
|
(def sum (lm (x y) (sumarg x y 0)))
|
|
|
|
|
|
;; increment, decrement numbers:
|
|
(def inc (lm (x) (add x 1)))
|
|
(def dec (lm (x) (add x (inv 1))))
|
|
|
|
;; ------------ Misc Functions -------------
|
|
|
|
(def fac (lm (x)
|
|
(if (eq x 0) 1
|
|
(mult x (fac (dec x))))))
|
|
|
|
(def abs (lm (x) (if (lt x 0) (inv x) x)))
|
|
|
|
;; ------------ Data Structure (List) ------------
|
|
|
|
(def cons (lm (a b) (lm (x) (if x a b))))
|
|
(def car (lm (cell) (cell t)))
|
|
(def cdr (lm (cell) (cell nil)))
|
|
|
|
|
|
|
|
|
|
;; ------------ Open TODO's --------------------
|
|
;; * Garbage Collection ( free() )
|
|
;; * Multi Threading (each function within it's own
|
|
;; thread)
|
|
;; * Eval with empty body or empty parameter list?
|
|
;; (def zero (lm (x) (0)))
|
|
;; (geht:(def c (lm (x) 0)) )
|
|
;; (def zero (lm () (0)))
|
|
;; * check if lexical scoping works:
|
|
;; (def a (lm (x y z)
|
|
;; (def abc (fn args))
|
|
;; (fn abc args))))
|
|
;; * set! is missing
|
|
|
|
|
|
;; Local Variables:
|
|
;; mode: scheme
|
|
;; eval: (message "main()")
|
|
;; fill-column: 55
|
|
;; comment-column: 40
|
|
;; indent-tabs-mode: nil
|
|
;; tab-width: 2
|
|
;; c-basic-offset: 2
|
|
;; End:
|