flang.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.sum # using a loop for sum := 0, sum + q v in a q := v*v else sum # using a tail-recursive function sumOfSquares (sum i32, s list i32) i32 is s ? nil => sum | c Cons => sumOfSquares (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.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 sumOfSquares (sum i32, s list i32) i32 is s ? nil => sum | c Cons => sumOfSquares (sum + c.head**2) c.tail say "sum is {sumOfSquares 0 a.as_list}"
What are effects?
next: Idiom # 203: Calculate mean and standard deviation