CJam - 142 / GolfScript - 144
{" had| a |farm|68, |68 8|here|Old MacDonald765|, E-I-E-I-O|10,
And on that 5 he7690,
With3 2 and3 t2,
Here4t24everyw23,
10!"'|/~A,{`/\*}/}:F;
Usage: "cow""moo"F
For GolfScript replace '|
with "|"
and A
with 10
Explanation:
The key part is A,{`/\*}/
:
A,{...}/
executes the block for each number from 0 to 9 (A=10)
`
converts the number to string
/\*
does a string replace: if we have on the stack "bar" "foo 1 baz" "1"
then /
splits the string resulting in ["foo " " baz"]
, \
swaps this array with the previous item ("bar") and *
joins the array resulting in "foo bar baz"
So the code replaces each number in the main string with a string that's previously on the stack. We have the animal and the sound, then " had", " a ", etc and finally ", E-I-E-I-O" and the main string, "10,...!". To avoid using too many quotes, I put all the strings (except the parameters) in one string, then split it and dumped the resulting array ('|/~
)
The main string goes through the following transformations:
10,
And on that 5 he7690,
With3 2 and3 t2,
Here4t24everyw23,
10!
replace "0"
with ", E-I-E-I-O"
:
1, E-I-E-I-O,
And on that 5 he769, E-I-E-I-O,
With3 2 and3 t2,
Here4t24everyw23,
1, E-I-E-I-O!
replace "1"
with "Old MacDonald765"
:
Old MacDonald765, E-I-E-I-O,
And on that 5 he769, E-I-E-I-O,
With3 2 and3 t2,
Here4t24everyw23,
Old MacDonald765, E-I-E-I-O!
replace "2"
with "here"
, then "3"
with "68 8"
etc.
8 corresponds to the sound, and 9 to the animal.
echoes the following
. Do you mean the function should print it out or return it? \$\endgroup\$a oink
or should that be made to bean oink
? \$\endgroup\$