As it turns out, there is nothing particularly special about the factorial function; a great many numeric functions can be defined recursively in a natural way. For example, let's think about multiplication. When you were first introduced to multiplication (remember that moment? :)), it may have been through a process of 'repeated addition'. That is, 5 × 4 is the same as summing four copies of the number 5. Of course, summing four copies of 5 is the same as summing three copies, and then adding one more -- that is, 5 × 4 = 5 × 3 + 5. This leads us to a natural recursive definition of multiplication:
Example
Example: Multiplication defined recursively
mult n 0 = 0 -- anything times 0 is zero
mult n 1 = n -- anything times 1 is itself
mult n m = (mult n (m - 1)) + n -- recur: multiply by one less, and add an extra copy
Name:
Anonymous2009-04-17 3:24
Why Functional Languages Failed?
Name:
Anonymous2009-04-17 3:25
This is from a haskell tutorial right?
Name:
Anonymous2009-04-17 3:37
Why Procedural Languages Failed
As it turns out, there is nothing particularly special about the factorial function; a great many numeric functions can be defined procedurally in a natural way. For example, let's think about multiplication. When you were first introduced to multiplication (remember that moment? :)), it may have been through a process of 'repeated addition'. That is, 5 × 4 is the same as summing four copies of the number 5. Of course, summing four copies of 5 is the same as summing three copies, and then adding one more -- that is, 5 × 4 = 5 × 3 + 5. This leads us to a natural procedural definition of multiplication:
Example
Example: Multiplication defined procedurally
int mult(int n, int m)
{
int o = 0;
while (m--)
o += n;
return o;
}
As it turns out, there is nothing particularly special about the factorial function; a great many numeric functions can be defined object-orientedly in a natural way. For example, let's think about multiplication. When you were first introduced to multiplication (remember that moment? :)), it may have been through a process of 'repeated addition'. That is, 5 × 4 is the same as summing four copies of the number 5. Of course, summing four copies of 5 is the same as summing three copies, and then adding one more -- that is, 5 × 4 = 5 × 3 + 5. This leads us to a natural object-oriented definition of multiplication:
Example
Example: Multiplication defined object-orientedly
public class Multiplicator {
private int n, m;
public Multiplicator(int n, int m)
{
this.n = n;
this.m = m;
}
public int multiply()
{
if (this.m == 0)
return 0; /* anything times 0 is zero */
if (this.m == 1)
return this.n; /* anything times 1 is itself */
return (new Multiplicator(this.n, this.m - 1).multiply()) + this.n; /* objec: instantiate new multiplicator, and add an extra copy */
}
}
Name:
Anonymous2009-04-17 10:24
I enjoyed this thread, >>5 could have been more enterprise but all in all it was a worthwhile read. Bump above the /g/ threads.