Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Projekt F#, nowy skrypt .fsx
- open System
- let x = 1
- let inline add x y = x + y
- let s1 = add 1 1
- let s2 = add 1.0 1.0
- //typ anonimowy
- let tuple = (1, 2)
- let first, second = tuple
- fst tuple
- let rec factorial1 n =
- if n = 0 || n = 1 then 1
- else n * factorial1 (n-1)
- let factorial2 n =
- let rec factorial' n =
- if n = 0 || n = 1 then 1
- else n * factorial' (n-1)
- factorial' n
- let factorial3 n =
- let rec factorial' acc n =
- if n = 0 || n = 1 then acc
- else factorial' (acc * n) (n-1)
- factorial' 1 n
- //mechanizm dekompozycji (pattern matching)
- let factorial5 n =
- let rec factorial' acc n =
- match n with
- | 0 | 1 -> acc
- | n -> factorial' (acc * n) (n-1)
- factorial' 1 n
- //Highter order-function
- let reduce neutralValue mapFunction n =
- if n <= 0 then failwith "n negative!"
- let rec loop acc n =
- if n <= 0 then acc else loop (mapFunction acc n) (n-1)
- loop neutralValue n
- let product' = reduce 1 (*)
- let sum' n = reduce 0 (+)
- //acc - akumulator - poprzednia zawartość
- let list = [1..5]
- let square = fun x -> x * x
- //mapowanie
- List.map square list
- List.map (fun x -> x * x) list
- //inny sposob zapisu
- [1..5]
- |> List.map square
- let l1 = 1 :: list // bardzo wydajna, tania operacja
- let l2 = list @ list
- List.head list
- List.tail list
- List.isEmpty list
- List.length list
- let isPalindrome xs = xs = List.rev xs
- isPalindrome ['i';'i']
- let f (x:int) = x
- //F# for C# developers - ksiazka
- let x1 = 1
- let mutable x2 = 2
- x2 <- 4
- x2
- //bcl
- let f1 x y z = x + y + z
- let f2 (x, y) = x + y
- let f3 = f1 1
- let rec doubleEverySecondNumber xs =
- match xs with
- | [] -> []
- | x :: y :: ys -> x :: 2 * y :: doubleEverySecondNumber ys
- | x :: ys -> x :: doubleEverySecondNumber ys
- type Expression =
- | Value of double
- | Add of Expression * Expression
- // gwiazdka oznacza tuple'a
- // sub, multiply, divide etc. ...
- let rec eval expression =
- match expression with
- | Value value -> value
- | Add (expr1, expr2) -> (eval expr1) + (eval expr2)
- eval (Value 10.0)
- eval (Add ((Value 10.0), (Value 10.0)))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement