Created
October 26, 2013 12:15
-
-
Save mhinz/7168801 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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