Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //imperative
- let sumSqr n =
- let mutable res = 0
- for i in 1..n do
- res <- res + i*i
- res
- let x = System.Console.ReadLine()|> System.Int32.Parse
- sumSqr 10
- //common
- let rec sumSqrF n =
- if n = 1 then 1
- else n*n + sumSqrF (n - 1)
- sumSqr 10
- // List comprehension
- let sumSqrG n =
- [ for i in 1..n -> i*i] |> List.sum
- sumSqrG 10
- [ for i in 1..10 do yield i*i; yield i*i*i ]
- //Point-Free // a' -> (a' -> b') -> 'b
- let (>>) f g = (fun x -> g (f x)) // let (|>) x f = f x let (<|) f x = f x (very usefull)
- let sumSqrPF =
- List.map (fun x -> x * x)
- >> List.filter (fun x -> x % 2 = 0)
- >> List.sum
- sumSqrPF [1..10]
- let flip f x y = f y x
- let even = (flip (%) 2) >> ( (=) 0) // чётность\
- let sqr x = x * x
- let sumSqrPFn n =
- [1..n]
- |> List.map sqr
- |> List.filter even
- |> List.sum
- sumSqrPFn 10
- let add x y = x + y
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement