Average (J)

From LiteratePrograms
Jump to: navigation, search

We want to define a function, named "avr", to find the average of a vector.

<<average.ijs>>=
avr=:definition

To get the average we need to sum the elements of the vector, and divide it by the number of elements.

<<definition>>= 
sum%#

We get the sum by inserting the verb "+" between each pair of elements with the adverb "/"

 
<<sum>>= 
+/
Download code
hijacker
hijacker
hijacker
hijacker