Skip to content

Instantly share code, notes, and snippets.

@mhinz
Created October 26, 2013 12:15
Show Gist options
  • Save mhinz/7168801 to your computer and use it in GitHub Desktop.
Save mhinz/7168801 to your computer and use it in GitHub Desktop.
IMPLEMENTATION Rabbits
IMPORT Denotation ONLY ++
Nat ONLY nat ! 0 1 2 - + > =
NatConv ONLY `
String ONLY string
StringConv ONLY `
Com ONLY ans:SORT
ComCompose COMPLETELY
Stream ONLY input stdIn readLine
output stdOut writeLine
write : output ** denotation -> com[void]
-- FUN main : com[void]
DEF main ==
write(stdOut, "For which generation do you want to know the number of rabbits? ")
& (readLine(stdIn) & (\\ in. processInput(in`)))
FUN processInput : denotation -> com[void]
DEF processInput(ans) ==
LET generation == !(ans)
bunnies == rabbits(generation)
result == "In the "
++ (generation`)
++ ". generation there are "
++ (bunnies`)
++ " couples of rabbits."
IN writeLine(stdOut, result)
FUN rabbits : nat -> nat
DEF rabbits(generation) ==
IF generation = 0 THEN 1
IF generation = 1 THEN 1
IF generation > 1 THEN rabbits(generation - 1) + rabbits(generation - 2) FI
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment