Factorial in haiku!
Smalltalk
(evaluate in a workspace; opens a dialog, asks for a number and prints the result on stdout):
"in" "this" 'poem,' "you" "must"
"pronounce" "characters" "like" "these:"
"return(^)," "and" "times(*);" "please".
"but" 'never' "in" "here"
"tell" "anyone" "about" "those"
"few" "parentheses".
"otherwise" "these" "words"
"are" "stupid" "and" "this" "coded"
"rhyme" "is" "wasted" Time.
"let" "us" "now" "begin"
"by" "defining," "in" Object
"and" compile: "the" "rhyme:"
'fac: "with" arg"ument"
"to" "compare" arg <"against" 2 ">"
"and" ifTrue: [ ^"return"
"["1] "or" ifFalse: "then"
["return"^ arg *"times" "the" "result"
"of" ("my"self ")getting"
"the" fac:"torial"
"of" "the" "number" arg "minus"-
1 "[(yes," "its" "easy")]'.
("Let" "me" "my"self "ask"
"for" "a" "number," "to" "compute"
"the" fac:"torial"
("by" "opening" "a"
"nice" Dialog "which" "sends" "a"
request: "asking" "for"
'the Number to use'
"(which" "is" "(" "treated" ) asNumber)
"then" print "the" "result".
I tried to bring in some reflection ("in this poem") and kigo as well. Also, some western style rhyme elements are included (please->these, time->rhyme); however, being neither native speaker of Japanese, nor of English, forgive any stylistic details ;-)
Java - 2 haikus
protected static
int factorial(int n) {
if (n == 0) {
return n + 1;
} return factorial(n
- 1) * n;}
Even when the question isn't code-golf, I often catch myself golfing the answer. In this case, I golfed the number of haikus.
I pronounce it so:
protected static
int factorial int n
if n is zeroreturn n plus one
return factorial n
minus one times n
Test program:
class Factorial { // class Factorial
public static void main(String[] // public static void main string
command_line_run_args) { // command line run args
int i = 0; // int i is zero
while (7 != 0) // while seven is not zero
System.out. // System dot out dot
println(i + "!" // print line i plus not
+ " = " + factorial( // plus is plus factorial
i += 1));} // i plus equals 1
protected static
int factorial(int n) {
if (n == 0) {
return n + 1;
} return factorial(n
- 1) * n;}}
Note that this program starts outputting 0
s fast; that is a result of overflow. You could easily get larger correct numbers by changing each int
to long
.
Standard pronunciations for System.out.println
and public static void main(String[] args)
are reflected in the program.
Haskell
fact :: Int -> Int -- fact is Int to Int
fact x = product (range x) -- fact x is product range x
range x = [1..x] -- range x is 1 [pause] x
Haskell education time:
- The
range x
function creates a list of integers from 1 up to the value ofx
. - The
fact x
function multiplies all the values of the listrange x
together to compute the result. - The first line says that the
fact
function takes an integer and returns an integer.