fuzion-lang.dev — The Fuzion Language Portal
JavaScript seems to be disabled. Functionality is limited.
Fuzion
•
Idioms
•
Idiom # 202: Sum of squares
Idiom # 202: Sum of squares
See
programming-idioms.org
:
Code
# using sum and map sum := sum (a.map x->x*x) # using map and fold sum := a.map x->x*x .fold i32.type.sum # using a loop for sum := 0, sum + q v in a q := v*v else say "sum is $sum" # using a tail-recursive function sum_of_squares (sum i32, s list i32) i32 => s ? nil => sum | c Cons => sum_of_squares (sum + c.head**2) c.tail
What are effects?
Running Example
ex202 is a := [0, 8, 15, 47, 11] # using sum and map sum := sum (a.map x->x*x) say "sum is $sum" # using map and fold sum := a.map x->x*x .fold i32.type.sum say "sum is $sum" # using a loop for sum := 0, sum + q v in a q := v*v else say "sum is $sum" # using a tail-recursive function sum_of_squares (sum i32, s list i32) i32 => s ? nil => sum | c Cons => sum_of_squares (sum + c.head**2) c.tail say "sum is {sum_of_squares 0 a.as_list}"
What are effects?
next: Idiom # 203: Calculate mean and standard deviation