Factor: Guess the number

My first useful factor program.


! A guessing game
USING: calendar random strings namespaces prettyprint math.parser io io.files math kernel combinators ;

IN: guess

! lets choose our number
SYMBOL: answer

! initialize the generator
now timestamp>unix-time init-random

! get a number [1..100] and put it in answer
100 random answer set

answer get .

! keep asking the user
: start-guessing ( -- )
"Enter number: " print
! get the number as a string
stdio get stream-readln
! convert to integer
string>number

! push on answer
answer get
! now stack top is : input, answer
{
{ [ 2dup < ] [ "try higher" print start-guessing ] }
{ [ 2dup > ] [ "try lower" print start-guessing ] }
{ [ 2dup = ] [ "Thats right!" print ] }
} cond

;


You might want to try it in the UI, the command line listener seems to have issues flushing the I/O streams. To run go:

"<path>/<filename>" run-file


Notice the lack of looping in Factor ( it's available but not recommended ) and using recursion in its place.