Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Solution
- // In this sample, the goal was to compute the length of the list
- // Method 1: Library Function
- let ourList =
- [
- 2;
- 4;
- 13;
- 26;
- 15;
- 29;
- 130;
- 0;
- -7;
- ]
- let sum1 = List.sumBy(fun el ->
- if el % 13 = 0 then
- el
- else
- 0
- )
- // Method 2: Recursion
- let rec sum2 = function
- | [] -> 0
- | h::t ->
- if h % 13 = 0 then sum2(t) + h
- else sum2(t)
- // Method 3: Tail Rec
- let sum3 =
- let rec sum3' acc i = function
- | [] -> acc
- | h::t ->
- if i % 2 = 0 then sum3' (acc+h) (i+1) t
- else sum3' (acc) (i+1) t
- sum3' 0 0
- let main =
- printfn "%10d simple" (sum1 ourList)
- printfn "%10d recursion" (sum2 ourList)
- printfn "%10d tail recursion" (sum3 ourList)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement