Hi , thanks John -- forgot, but tried it now, works great !
Great Charles,
It could also be written in Lisp without much problems imho.
-------------------------
;; CatSimul
(define stack nil)
(define (pop*) (pop stack))
(define (push* x) (push x stack))
(define (dup*) (push (first stack) stack))
(define (swap*)
(let ((a (pop stack))
(b (pop stack)))
(push a stack)
(push b stack)))
(define (clr*) (setq stack nil))
(define (+*) (push* (+ (pop*) (pop*))))
(define (print*) (print (pop*)))
(define (rot*)
(let ((a (pop*))
(b (pop*))
(c (pop*)))
(push* b) (push* a) (push* c)))
(define-macro (do* i f)
(dotimes (void (eval i) )
(dolist (j (eval f)) (eval j) )))
(define (fibo x)
(clr*)
(do* 2 '((push* 1L)))
(do* x '((dup*) (rot*) (+*) ))
(swap*) (print*) (print " OK ")
)
----------------------------REPL
> (time (fibo 4000))
6457488449094817353137694901536959564441390064015134270840757759817721035903408891444947780728724174
3760741523783818897499227009742183152482019062763550798743704275106856470216307593623057388506776767
2020696704775060888952943005092911660239478668417638539538139822817039366653699227090953080068213995
2478072104995582919140702994362208777929645917401261014865952038117045259114133194933608057714170864
5783606636081941915217355115810993973945783493983844592749672661361548061615756595818944317619922097
3699176769740582063418920881445493379744229521401326215683407010162734227278277627261530663030930529
8205175744474242803310752241946621965578041310175950523161722257829248608100239121878518929967575776
6920269402348733644662725774717740924068828300186439425921761082545463164628807702653752619616157324
434040342057336683279284098590801501L OK 78.125
-----------------
that's less than 0.1 sec (with an interpreter)
the stack is typeless here
can contain strings, lambda's, numbers ... other stacks
etc...
maybe an own stack for the iterations and maybe one for the formulae (lambda's) ...
Syntax can be completely changed by writing some syntax-macro's ... numbers already evaluate to themselves, but function could be changed into symbols (here it are still functions) then the parens can go, the sequence can be changed and numbers will be automatically placed on the stack ... but as mixing data and code is no problem .....
just a simulation of course ;.. ,, thanks for the links .. appreciated !!
best Rob
the code does
A
B
--------------------> DUP ROT +
A+B
B
(on the stack)
mmm, an idea -- a numeric stack processed with Oxygen - and a (slower) typeless controled by Lisp -- that should be something
----- better syntax :
(define stack nil)
(define lambda-stack nil)
(define $cat 0)
(define (clr-lambda-stack) (setq lambda-stack nil))
(define-macro (cat#)
(doargs (i)
(if (number? i)
(push i stack)
(if (list? i) (push i lambda-stack)
(let ((s (string i)))
(cond ((= s "dup") (push (stack 0) stack))
((= s ".") (println (pop stack) " ok" ))
((= s "drop") (pop stack))
((= s "+" ) (push (+ (pop stack) (pop stack)) stack ))
((= s "swap") (let ((a (pop stack))
(b (pop stack))) (push a stack) (push b stack)))
((= s "do")
(dotimes (void (pop stack)) (eval (cons 'cat# (first lambda-stack))))
(clr-lambda-stack))
((= s "rot") (let (( a (pop stack))
(b (pop stack)) (c (pop stack)))
(push b stack) (push a stack) (push c stack)))
((= s "$cat") (push $cat stack))
))))) )
now the only parens used are to put composed parts on a "lambda-stack" for iteration etc...
it is called recursively, but only one level / parens ... they can be nested ....
> (cat# 1L 1L (dup rot +) 4000 do swap .) -- all that's needed for a fibonacci
6457488449094817353137694901536959564441390064015134270840757759817721035903408891444947780728724174
3760741523783818897499227009742183152482019062763550798743704275106856470216307593623057388506776767
2020696704775060888952943005092911660239478668417638539538139822817039366653699227090953080068213995
2478072104995582919140702994362208777929645917401261014865952038117045259114133194933608057714170864
5783606636081941915217355115810993973945783493983844592749672661361548061615756595818944317619922097
3699176769740582063418920881445493379744229521401326215683407010162734227278277627261530663030930529
8205175744474242803310752241946621965578041310175950523161722257829248608100239121878518929967575776
6920269402348733644662725774717740924068828300186439425921761082545463164628807702653752619616157324
434040342057336683279284098590801501L ok
171.875