Last active
February 26, 2017 03:11
-
-
Save otaconix/052b980a3fd506e91b92c3bee96b74d5 to your computer and use it in GitHub Desktop.
cond construct
This file contains 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
rand := (Random value * 99 + 1) round | |
rand println | |
cond := method( | |
if(call message argCount % 2 != 0, Exception raise("cond needs pairs of arguments (arg count: #{call message argCount})" interpolate)); | |
inner := method(c, i, | |
if (i >= c message argCount, Exception raise("cond needs at least one condition to be true")); | |
if (c evalArgAt(i), return c evalArgAt(i + 1), return inner(c, i + 2))) | |
inner(call, 0)) | |
for(i, 1, 10, | |
write("[#{i}] What's your guess? " interpolate) | |
guess := File standardInput readLine asNumber | |
cond( | |
guess == rand, "You guessed it!"; break, | |
guess > rand, "Too high", | |
guess < rand, "Too low" | |
) println) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage: