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 2:05

>>5
I want to look cool on /prog/.

>>7
I'm not, I'm using it backwards. It has several advantages over S(n)=n∪{n} and S(n)={n-1}, but finding those are left as an exercise to the interpreter.

>>8
Why wouldn't you? Do you think Kiketel wastes precious transistors parallelizing addition? Of course not. Addition at the hardware level is all done by a single half-adder looped on itself. That's recursion at it's sloppiest. I've only refined the process. But as a matter of fact, I haven't even done that! How's that for a mind fuck? The addition is done with the successor function, the recursion in the adder is only for looping. But that just means that's it's a recursive definition somewhere else, right?

>>12
I worked it out, and surprisingly, it should take only a few minutes, assuming that the memory access time is constant for all locations.

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