The reality of Stefan Scholl

"I hope there's pudding"

99 Bottles of Beer in Factor

99 Bottles of Beer in Factor.

A naïve implementation from someone who is just beginning to (re)learn the language and doesn't want to (nor can) show off the flashy features, like so many other examples of 99 Bottles of Beer.

USING: combinators io kernel make math math.parser sequences ;
IN: 99bottles

: bottles ( n -- str )
{
{ 0 [ "no more bottles" ] }
{ 1 [ "1 bottle" ] }
[ number>string " bottles" append ]
} case ;

: verse-0 ( n -- )
drop
"No more bottles of beer on the wall, no more bottles of beer.
Go to the store and buy some more, 99 bottles of beer on the wall."
print ;

: verse-n ( n -- )
[ dup bottles % " of beer on the wall, " % dup bottles %
" of beer.\nTake one down and pass it around, " %
1 - bottles % " of beer on the wall.\n" % ] "" make
print ;

: verse ( n -- )
dup 0 number= [ verse-0 ] [ verse-n ] if ;

: 99bottles ( -- )
100 reverse [ verse ] each ;

MAIN: 99bottles
Tags: Factor
Comments

No future for Factor on MacOS X 10.4

Slava Pestov: "Daniel Ehrenberg is planning on writing an Objective C 2.0 binding,
and once this is done I don't want to keep the old Objective C bridge
around anymore. At this point, we will drop 10.4 support." http://permalink.gmane.org/gmane.comp.lang.factor.general/1322
Tags: Factor
Comments

Randomizing a sequence in Factor

In Trivial Sequence Shuffling, Leslie P. Polzer posted an easy and short version of a sequence shuffler in Common Lisp. The sort function has to do all the work. Not a recommended way to shuffle your sequences, but a short one.

The Factor version isn't very long, too:

USING: sorting random ;
: seqrnd ( seq -- rndseq ) [ 2drop { -1 0 1 } random ] sort ;

! Testing it:
"Trivial Sequence Shuffling in Factor" seqrnd .
"rloFt necanagi ilcfefquh SneuiSrivT"

But you should rather use a Fisher-Yates shuffle.

UPDATE (2009-11-08):
Doesn't work anymore in current versions of Factor. Needs a few changes:

USING: sorting math.order random ;
: seqrnd ( seq -- rndseq ) [ 2drop { +lt+ +eq+ +gt+ } random ] sort ;

! Testing it:
"Trivial Sequence Shuffling in Factor" seqrnd >string .
"cfie S onirateS vT lrcqFuengihufilna"
Tags: Factor
Comments