atbusbook@aol.com wrote:

lets say you want a generic numerical algorithom like sum

Ruby

def sum lst

lst.inject(0){|total,current| total*current}

endJava // i dont know if there is a numeric super class for numbers

class Sum{

public static int sum(int[] lst){

int total = 0;

for(int current : lst){

total+=current;

}

return total;

}

// repeat for all other number types

}

Any more to add? Your question is not exactly clear. Are you asking how

to do higher order procedures in Python? Here's sum as a higher-order

procedure... It sums the numbers from a to b (or any thing that can be

compared and added with "<" and "+").

def Sum(term, next, a, b):

if(a > b):

return 0

else:

return (term(a) + (Sum(term, next, next(a), b)

This function takes two functions (term and next) and two objects (in

our case numbers) representing the range to sum across. Now let's say

you want to add the numbers 1 through 10. The term for the summation

would just be the number, so the term function would simply be a

function called identity, and could be defined as

def identity(x):

return x

To get the next term, we are just adding one each time (i.e. 1, 2, 3,

4, ...). So our next function is

def increment(x):

return x += 1

Then we call Sum(identity, increment, 1, 10), and we get 1 + 2 + 3 + 4

+ and so on.

Now what if you wanted the sum of the CUBES of 1 through 10? Easy, we

replace the term function with a function cube(x) that returns x*x*x.

Sum(cube,increment,1,10)

Hence each term in our summation is the cube, but we are still

incrementing by one each time, so we get (1^3 + 2^3 + 3^3 + 4^3 + ...)

Similarly we can change the next function to skip certain number, meet

certain requirement, do some tranformation, whatever. The fact that

python accept and uses functions as parameters to other functions is

awesome in terms of power, but it can get a little mind boggling

sometimes.