127 lines
3.4 KiB
Plaintext
127 lines
3.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 (leq a 0)
|
|
0
|
|
(add 1 (div (sub a b) b)))))
|
|
|
|
(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
|
|
(sum (add a 1) b (add erg a)))))
|
|
(def sum (lm (x y) (sumarg x y 0)))
|
|
|
|
|
|
(def inc (lm (x) (add x 1)))
|
|
(def dec (lm (x) (add x (inv 1))))
|
|
|
|
;; ------------ Open TODO's ----------------------------------
|
|
|
|
(def dispatch (lm (a b which) (if (eq which 0) a b)))
|
|
|
|
;; (def cons (lm (a b)
|
|
;; (lm (x) (dispatch a b x))))
|
|
(def cons (lm (a b) (lm (x) (if (eq x 0) a b))))
|
|
|
|
(def car (lm (cell) (cell 0)))
|
|
(def cdr (lm (cell) (cell 1)))
|
|
|
|
;; (define (cons x y)
|
|
;; (define (dispatch m)
|
|
;; (cond ((= m 0) x)
|
|
;; ((= m 1) y)
|
|
;; (else (error "Argument not 0 or 1 -- CONS" m))))
|
|
;; dispatch)
|
|
;; (define (car z) (z 0))
|
|
;; (define (cdr z) (z 1))
|
|
|
|
;; ------------ Open TODO's ----------------------------------
|
|
;; * Garbage Collection
|
|
;; * Multi Threading (each function within it's own thread
|
|
;; * Eval with empty body or empty parameter list?
|
|
;; (def zero (lm (x) (0)))
|
|
;; * lexical scoping like:
|
|
;; (def a (lm (x y z)
|
|
;; (def abc (fn args))
|
|
;; (fn abc args))))
|
|
|
|
|
|
;; 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:
|
|
|
|
|
|
|
|
|
|
(def a (lm (x) (lm (y) (add x y))))
|
|
nil 0x0
|
|
a
|
|
#fn
|
|
(a 1)
|
|
#fn
|
|
(def b (a 1))
|
|
nil 0x0
|
|
b
|
|
#fn
|
|
(b 5)
|
|
|
|
|
|
|
|
((a 1) 4)
|
|
eval_fn_call: expected function body as list
|
|
|
|
|
|
- mindestens 2 sachen sind noch kaputt:
|
|
- wenn ich einen funktionsaufruf am anfang habe das eine funktion
|
|
zurückgibt gehts nicht
|
|
- und im allgemeinen findet man nicht die variable in einem
|
|
funktionsaufruf einer zurückgegebenen funktion
|
|
|
|
|
|
|
|
|
|
(def tripe (lm (x) (lm (y) (lm (z) (add x (add y z))))))
|