Advertisement
Gumanitariy

lab2\1

Nov 12th, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
F# 0.49 KB | None | 0 0
  1. // Solution
  2. // Number in List: 4 -> task 5
  3. // In this sample, the goal was to compute the length of the list
  4.  
  5. // Method 1: Library Function
  6. let sum1 = List.sumBy(fun x -> x*x)
  7.  
  8. // Method 2: Recursion
  9. let rec sum2 = function
  10.   | []   -> 0
  11.   | h::t -> sum2(t) + h*h
  12.  
  13. // Method 3: Tail Rec
  14. let sum3 =
  15.   let rec sum3' acc = function
  16.  | [] -> acc
  17.  | h::t -> sum3' (acc + h*h) t
  18.   sum3' 0
  19.  
  20. let ar = [1..10]
  21. printfn "Example:\nSum1: %i Sum2: %i Sum3: %i" (sum1 ar) (sum2 ar) (sum3 ar)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement