Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

List Processor

Name: Anonymous 2015-10-10 7:03

A real List Processor would process only with lists. Let's build N using lists and make an instruction set for operating on them.
(define n-zero? (lambda (x) (equal? x '())))
(define n-zero '())

(define n-succ
(lambda (n)
(((lambda (x) (x x))
(lambda (V)
(lambda (A B)
(if (n-zero? A) B (cons (car A) ((V V) (cdr A) B))))))
(cons n n-zero) n)))

(define n-prev
(lambda (n)
(if (n-zero? n) #f
(car n))))

(define n-lt?
(lambda (a b)
(((lambda (x) (x x))
(lambda (V)
(lambda (n m)
(if (n-zero? m) #f
(if (equal? n (car m)) #t
((V V) n (cdr m))))))) a b)))

(define n-gt?
(lambda (a b)
(and (not (equal? a b)) (not (n-lt? a b)))))

(define n-add
(lambda (a b)
(if (n-zero? b) a (n-add (n-succ a) (n-prev b)))))

(define n-sub
(lambda (a b)
(if (n-zero? b) a (n-sub (n-prev a) (n-prev b)))))

(define n-mul
(lambda (a b)
(((lambda (x) (x x))
(lambda (V)
(lambda (t m)
(if (n-zero? t) m ((V V) (n-prev t) (n-add m a))))))
(n-prev b) a)))

(define n-div
(lambda (a b)
(if (n-zero? a) n-zero
(if (n-zero? b) #f
(((lambda (x) (x x))
(lambda (V)
(lambda (t d)
(if (n-lt? d b) t ((V V) (n-succ t) (n-sub d b))))))
n-zero a)))))

(define n-mod
(lambda (a b)
(if (n-zero? b) n-zero
(if (n-zero? b) #f
(n-sub b (n-mul (n-div b a) a))))))

(define n-pow
(lambda (a b)
(((lambda (x) (x x))
(lambda (V)
(lambda (t m)
(if (n-zero? t) m ((V V) (n-prev t) (n-mul m a))))))
(n-prev b) a)))

(define make-stack
(lambda()
(let* ((stk n-zero)
(pop (lambda ()
((lambda (a b)
(begin
(if (n-zero? a) #f (set! stk (cdr a)))
b)) stk (car stk))))
(push (lambda (n) (begin
(set! stk (cons n stk))
n)))
(action (lambda (m)
(if (eq? m 'push) (lambda (n) (push n))
(if (eq? m 'pop) (pop) #f)))))
action)))

(define stack (make-stack))
(define push (lambda (n) ((stack 'push) n)))
(define pop (lambda () (stack 'pop)))

(define dup (lambda () ((lambda (x) (begin (push x) (push x))) (pop))))
(define dup-second (lambda () ((lambda (x y) (begin (push y) (push x) (push y))) (pop) (pop))))
(define swap (lambda () ((lambda (x y) (begin (push y) (push x))) (pop) (pop))))
(define drop (lambda () (pop)))
(define add (lambda () (push ((lambda (a b) (n-add a b)) (pop) (pop)))))
(define sub (lambda () (push ((lambda (a b) (n-sub a b)) (pop) (pop)))))
(define mul (lambda () (push ((lambda (a b) (n-mul a b)) (pop) (pop)))))
(define div (lambda () (push ((lambda (a b) (n-div a b)) (pop) (pop)))))
(define mod (lambda () (push ((lambda (a b) (n-mod a b)) (pop) (pop)))))
(define cmp (lambda () (push ((lambda (a b) (if (equal? a b) (n-succ n-zero) n-zero)) (pop) (pop)))))
(define cmp-lt (lambda () (push ((lambda (a b) (if (n-lt? a b) (n-succ n-zero) n-zero)) (pop) (pop)))))
(define exec-if (lambda (fn) ((lambda (a) (if (equal? a n-zero) #f (fn))) (pop))))
(define write (lambda () ((lambda (a) (begin (display a) (newline))) (pop))))
(define write-peek (lambda () (begin (dup) (write))))

Name: Anonymous 2015-10-10 15:11

>>3
define is a list of opcodes instructing the environment to register the symbol as a function.
An opcode is a list of bytes that the interpreter evaluates in order to modify the environment.
An enviroment is a list of pairs of symbols and functions.
A pair is a list of two items.
A symbol is a list of characters.
A character is a list of bytes.
A function is a list of opcodes.
An interpreter is a list of bytes that the processor executes.
A byte is a list of eight bits.
A processor is a list of wires and silicon.
A wire is a list of copper atoms.
Silicon is a list of silica atoms.
An atom is a list of subatomic particles.
A subatomic particle is a list of infinitesimal copies of the current universe, for all relevant purposes.
A universe is a list of everything within the universe, including define.

Did I miss anything?

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List