Skip to content

Instantly share code, notes, and snippets.

@rferraz
Created January 22, 2011 21:17
Show Gist options
  • Save rferraz/791491 to your computer and use it in GitHub Desktop.
Save rferraz/791491 to your computer and use it in GitHub Desktop.
A demo spell program
account (balance) = { balance: balance }
account#deposit (account amount) = { balance: account.balance + amount }
account#withdraw (account amount) =
? account.balance < amount -> error#signal "Insufficient funds!"
? _ -> { balance: account.balance - amount }
main () =
do
account#deposit account 10
account#deposit account 200
acount#withdraw account 30
with
account <- account 10
fib#recursive (value) =
? value <= 1 -> value
? _ -> (fib#recursive (value - 1)) + (fib#recursive (value - 2))
fib#iterative (value) = round (phi ** value / sq5)
with
sq5 <- sqrt 5
phi <- (1 + sq5) / 2
filter (predicate list) =
? null list -> list
? predicate (head list) -> (head list) : filter predicate (tail list)
? _ -> filter predicate (tail list)
array#partition (array predicate) = (filter predicate array) , (filter (invert predicate) array)
quicksort (array) =
? array#len array <= 1 -> array
? _ -> quicksort' array
with
quicksort' (array) = quicksort (first partition) ++ quicksort (last partition)
with
pivot <- array#midpoint array
partition <- array#partition array { element | element < pivot }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment