123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475 |
- let myInt = 5
- let myFloat = 3.14
- let myString = "Hello"
-
- let twoToFive = [2;3;4;5]
- let oneToFive = 1 :: twoToFive // New first element
- let zeroToFive = [0;1] @ twoToFive // Concatenation
-
- let square x = x * x
- square 3
-
- let add x y = x + y
- add 2 3
-
- let evens list =
- let isEven x = x % 2 = 0 // Inline function
- List.filter isEven list // Basic libary function
-
- evens oneToFive
-
- let sumOfQuaresTo100 =
- List.sum ( List.map square [1..100] )
-
- let sumOfSquaresTo100piped =
- [1..100] |> List.map square |> List.sum
-
- let sumOfSquaresTo100WithFun =
- [1..100] |> List.map (fun x->x*x) |> List.sum // `fun` is like a lambda
-
- // F# Functions return implicitly
-
- let simplePatternMatch =
- let x = "a"
- match x with
- | "a" -> printfn "x is a"
- | "b" -> printfn "x is b"
- | _ -> printfn "x is something else"
-
- let validValue = Some(99)
- let invalidValue = None
-
- let optionPatternMatch input =
- match input with
- | Some i -> printfn "Input is an int=%d" i // similar to c format strings
- | None -> printfn "Input missing"
-
- optionPatternMatch validValue
- optionPatternMatch invalidValue
-
- // Stuff with data types
- let twoTuple = 1,2
- let threeTuple = "a",2,true
-
- type Person = {First:string; Last:string} // Record types have named fields
- let person1 = {First="john"; Last="roberts"}
-
- type Temp =
- | DegreesC of float
- | DegreesF of float
-
- let temp = DegreesF 98.6
-
- type Employee =
- | Worker of Person
- | Manager of Employee list
-
- let worker = Worker person1
-
- // Printing stuff out
- printfn "Printing an int %i, a float %f, a bool %b" 1 2.0 true
- printfn "A string %s, and something generic %A" "hello" [1;2;3;4]
-
- // You can also print complex types that have been created
- printfn "twoTuple=%A,\nPerson=%A,\nTemp=%A,\nEmployee=%A"
- twoTuple person1 temp worker
|