Skip to content

Instantly share code, notes, and snippets.

@raek
Created June 12, 2010 02:20
Show Gist options
  • Save raek/435333 to your computer and use it in GitHub Desktop.
Save raek/435333 to your computer and use it in GitHub Desktop.
(pi "The constant pi") 3.14 def
(area-of-disk
"Calculate the area of a cirlce disk."
(radius -- area))
(dup * pi *)
defn
(area-of-ring
"Calculate the area of a ring."
(outer inner -- area))
(area-of-disk swap area-of-disk swap -)
defn
(area-of-triangle
"Calculate the area of a triangle."
(base height -- area))
(* 2 /)
defn
(area-of-rectangle
"Calculate the area of a rectangle."
(side-a side-b -- area))
(*)
defn
(factorial (n -- n-fac)
"Calculate the factorial of n.")
(dup 0 =
(drop 1)
(dup dec factorial *)
if)
defn
(fib (n -- nth-fib))
(dup 0 =
(drop 0)
(dup 1 =
(drop 1)
(dup dec fib swap dec dec fib +)
if)
if)
defn
(fib (n -- nth-fib))
(dup 0 = if
(drop 0)
(dup 1 = if
(drop 1)
(dup dec fib swap dec dec fib +)))
defn
(fib (n -- nth-fib))
(cond
((dup 0 =) (drop 0)
(dup 1 =) (drop 1)
(true) (dup dec fib swap dec dec fib)))
(fib (n -- nth-fib))
(((dup 0 =) (drop 0)
(dup 1 =) (drop 1)
(true) (dup dec fib swap dec dec fib +))
cond)
defn
Variants
- Special evaluation
- Immediate data, or
- Lists are not evaluated (like Smalltalk code blocks)
- Words, invoke function or push value
- symbols invoke functions, keywords pushes values
- Function/block stack contract
- fixed numer of pops, fixed number of pushes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment