4/27 I forgot if I posted this before or not, but here's another fun
problem: Write the factorial function in any language of your
choice without using any iterative constructs or named functions.
-- ilyas
\_ Here's another fun problem: Write your next major project without
using user defined functions or subroutines utilizing only goto
commands. After you have completed that task try climbing the
stairs of your favorite building while hopping only on one foot.
--williamc
\_ Why lift weights? It's such an artificial problem! Almost
no one actually needs to move weights around. Just lift
your arms instead. Why prove stuff only using ZFC axioms?
Just add whatever axioms you need, why restrict yourself to
so little? -- ilyas
\_ the only reasonable reason to do any of those is for
fun. lifting weights is stupid.
\_ Either for fun or to develop a skill, or for reasons
of maintainability (that's the example of proving using
as little as possible). At any rate, I am of the opinion
that folks that look down on these sorts of problems
as 'useless in the real world' have a dangerous case
of blinders coupled with an incurable case of vanity.
Certainly, nothing appearing in SICP is stupid.
-- ilyas
\_ don't mind williamc. he's generally proved himself to be
kind of a dumbass anyway. in any case, perhaps ken thompson
put it best:
"In college, before video games, we would amuse ourselves
by posing programming exercises. One of the favorites was
to write the shortest self-reproducing program. Since this
is an exercise divorced from reality, the usual vehicle was
FORTRAN. Actually, FORTRAN was the language of choice for
the same reason that three-legged races are popular."
\_ three-legged races are popular? -tom
\_ do we forget our childhoods so quickly?
\_ Many many years ago, after I told my dad that I taught
myself how to program, he gave me that problem. I did
it (in Pascal), but it turned out that he expected me to
read the program file from disk and print it out!
\_ WOW, YOU'RE SMRT!!!
\_ Or trying going one more day without pissing in your boss's
coffee mug. This one is much harder than the above but it *can*
be done!
\_ you already asked this, and anyone who's taken CS61A has seen it as
the extra-for-experts problem to assignment 1 (or was it 2?)
\_ float factorial(int i) {
const float f[] = {1, 1, 2, 6, 24, 120, ... and so on ... };
return f[i];
}
\_ of course, this doesn't work for arbitrary n, and "factorial" is
a named function.
\_ Then how do you write a C function without a name? And which
language would let you write the said function while
supporting arbitary n anyway?
\_ And finally we come to the purpose of this question...
\_ Take CS61A sometime.
\_ CS61A tells me the former or the latter?
\_ well, since C doesn't support anonymous functions,
and since 61A doesn't cover C: the latter.
\_ Sterling's formula?
\_ Exact answers only please. I respect your good attempt though.
-- ilyas
\_ ; replace ___ with the value you want factorialed
((lambda (f n) (if (<= n 1) 1 (* n (f f (- n 1)))))
(lambda (f n) (if (<= n 1) 1 (* n (f f (- n 1)))))
___)
\_ Nicely done. The keyword for this sort of trickery is
'continuation passing style.' -- ilyas |