Advertisement
Guest User

Untitled

a guest
Mar 18th, 2014
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
F# 0.93 KB | None | 0 0
  1. //imperative
  2. let sumSqr n =
  3.     let mutable res = 0
  4.     for i in 1..n do
  5.         res <- res + i*i
  6.     res
  7.  
  8. let x = System.Console.ReadLine()|> System.Int32.Parse
  9.  
  10. sumSqr 10
  11. //common
  12. let rec sumSqrF n =
  13.     if n = 1 then 1
  14.              else n*n + sumSqrF (n - 1)
  15. sumSqr 10
  16. // List comprehension
  17. let sumSqrG n =
  18.     [ for i in 1..n -> i*i] |> List.sum
  19.  
  20. sumSqrG 10
  21. [ for i in 1..10 do yield i*i; yield i*i*i ]
  22. //Point-Free                      // a' -> (a' -> b') -> 'b
  23. let (>>) f g = (fun x -> g (f x)) // let (|>) x f = f x  let (<|) f x = f x (very usefull)
  24.  
  25. let sumSqrPF =
  26.     List.map (fun x -> x * x)
  27.     >> List.filter (fun x -> x % 2 = 0)
  28.     >> List.sum
  29.  
  30. sumSqrPF [1..10]
  31.  
  32. let flip f x y = f y x
  33.  
  34. let even = (flip (%) 2) >> ( (=) 0) // чётность\
  35. let sqr x = x * x
  36.  
  37.  
  38. let sumSqrPFn n =
  39.     [1..n]
  40.     |> List.map sqr
  41.     |> List.filter even
  42.     |> List.sum
  43.  
  44. sumSqrPFn 10
  45.  
  46. let add x y = x + y
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement