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-11 7:12

>>13
How?

>>15
Well all right then. But this still isn't really a processor. You've defined some functions, but you are still relying on the language to do loops and such. It can hardly be called a processor until you have some flow contol instructions.

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