Bigger scheme - sam's capsule: sloum's small s-expression based language - Addendum
/ \ /
k --- a b --- f
/ \ / \
me --- l i
\ / \ /
j --- c d --- e
\ /
g --- h
Someone might wonder why does my code look like that:
(define Stack '()) (define PUSH (lambda (val) (set! Stack (cons val Stack)))) (define POP (lambda () (begin0 (car Stack)(set! Stack (cdr Stack))))) (define PEEK (lambda () (if (not (null? Stack)) (display (car Stack))))) (define ADD (lambda () ( begin0 (+ (car Stack) (car (cdr Stack))) (set! Stack (cdr (cdr Stack)))))) (define SUB (lambda () ( begin0 (- (car (cdr Stack)) (car Stack)) (set! Stack (cdr (cdr Stack)))))) (define MUL (lambda () ( begin0 (* (car Stack) (car (cdr Stack))) (set! Stack (cdr (cdr Stack)))))) (define DIV (lambda () ( begin0 (/ (car (cdr Stack)) (car Stack)) (set! Stack (cdr (cdr Stack))))))
Especially people who are familiar with the RPN notation (the best notation) _may_ even call it inefficient and ask...
Is it the only way?
Of course it isn't.
As we already defined PUSH and POP, and slope is able to do the arithmetic by itself we could do it in only few strokes:
(define ADD1 (lambda () (+ (POP) (POP))))
or even, simulating Forth or HP RPN:
(define ADD1 (lambda () (PUSH (+ (POP) (POP)))))
Well, does it still work?
I could say 'believe me, it does', but we're doing science, not religion here, so...
Let's test it
; we still need the already defined POP and PUSH > (load "stack.slo") ; The F.U.N. starts here: > (define ADD1 (lambda () (PUSH (+ (POP) (POP))))) > (PUSH 10) > (PUSH 10) > (ADD1) > (PEEK)(newline) 20
[Insert screencap from the classical "Frankenstein" movie] "IT... LIVES!!!"
Still fun, but if I did it that way we would be talking about Forth and not slope ;-)
And the code gets optimised at some point, so - stay tuned.
See you then,
-- sam
Response: 20 (Success), text/gemini
| Original URL | gemini://rawtext.club/~samhunter/gemlog/2021-08-13-sloums... |
|---|---|
| Status Code | 20 (Success) |
| Content-Type | text/gemini; charset=utf-8; lang=en |