serious stuff
www.cubbi.com
personal
programming
fibonacci numbers
algorithms
benchmarks
forth examples
postscript examples
muf examples
joy examples
j examples
scheme examples
hope examples
ocaml examples
haskell examples
prolog examples
c++ examples
java examples
assembly language examples
fortran examples
c examples
sh examples
awk examples
perl examples
tcl examples
asmix
hacker test
resume
science
martial arts
fun stuff
www.cubbi.org
|
Joy
Date: 1999
Type: Concatenative language
Usage: academic
ALGORITHM 1A: NAIVE BINARY RECURSION |
"numlib" libload .
DEFINE fib == [small] [] [pred dup pred] [+] binrec .
DEFINE f == [negative] [dup [0 swap - fib] dip odd [] [neg] branch] [fib] ifte .
DEFINE fprint == dup 'd 3 0 format "th Fibonacci number is " concat putchars f .
DEFINE main == [argc 2 =]
[argv second 10 strtol fprint]
["Usage: joy f1a.joy " putchars] ifte.
main .
|
ALGORITHM 2A-3: DATA STRUCTURE - SIMPLE LIST |
DEFINE f == [1 1] swap [[[+] nullary] infra] times 1 at .
DEFINE f_print == dup 'd 3 0 format "th Fibonacci number is " concat putchars f .
45 f_print .
|
ALGORITHM 2B: SIMPLE RECURSION |
DEFINE f == [1 1] dip [small] [pop swap pop] [pred [dup [swap] dip +] dip] tailrec .
DEFINE f_print == dup 'd 3 0 format "th Fibonacci number is " concat putchars f .
45 f_print .
|
ALGORITHM 2C: NON-RECURSIVE LOOP |
DEFINE f == [0 1] dip [swap [+] unary] times popd .
DEFINE f_print == dup 'd 3 0 format "th Fibonacci number is " concat putchars f .
45 f_print .
|
ALGORITHM 3A: MATRIX EQUATION |
"mtrlib" libload.
DEFINE mp ==
[small] [pop] [2 div rollup dupd]
[dup mm-mul-m rolldown null [popd] [mm-mul-m] branch] linrec .
DEFINE f == [[1 1][1 0]] swap mp first first .
DEFINE f_print == dup 'd 3 0 format "th Fibonacci number is " concat putchars f .
45 f_print .
|
ALGORITHM 3B: FAST RECURSION |
DEFINE f ==
[[[2 <][pop 1 1]] [[2 =][pop 2 1]]
[[2 rem 1 =] [pred 2 /] [
dupd dupd dupd dup [+ *] dip swapd dup
[* +] dip dup * rolldown dup * + ]]
[[2 rem 0 =] [2 / pred] [
dupd dupd dup [+ dup dup * rolldown dup * + rotate] dip
dupd * rollup * + ]]
[[]] ] condlinrec pop.
DEFINE f_print == dup 'd 3 0 format "th Fibonacci number is " concat putchars f .
45 f_print .
|
ALGORITHM 3C: BINET'S FORMULA |
DEFINE f == 1 5 sqrt dup rollupd + 2 / swap succ dupd swap dupd 1 swap - swap
pow rollup pow swap - swap / trunc .
DEFINE f_print == dup 'd 3 0 format "th Fibonacci number is " concat putchars f .
45 f_print .
|
|