learning F#, it seems pretty cool, favourite functional language so far.
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. let myInt = 5
  2. let myFloat = 3.14
  3. let myString = "Hello"
  4. let twoToFive = [2;3;4;5]
  5. let oneToFive = 1 :: twoToFive // New first element
  6. let zeroToFive = [0;1] @ twoToFive // Concatenation
  7. let square x = x * x
  8. square 3
  9. let add x y = x + y
  10. add 2 3
  11. let evens list =
  12. let isEven x = x % 2 = 0 // Inline function
  13. List.filter isEven list // Basic libary function
  14. evens oneToFive
  15. let sumOfQuaresTo100 =
  16. List.sum ( List.map square [1..100] )
  17. let sumOfSquaresTo100piped =
  18. [1..100] |> List.map square |> List.sum
  19. let sumOfSquaresTo100WithFun =
  20. [1..100] |> List.map (fun x->x*x) |> List.sum // `fun` is like a lambda
  21. // F# Functions return implicitly
  22. let simplePatternMatch =
  23. let x = "a"
  24. match x with
  25. | "a" -> printfn "x is a"
  26. | "b" -> printfn "x is b"
  27. | _ -> printfn "x is something else"
  28. let validValue = Some(99)
  29. let invalidValue = None
  30. let optionPatternMatch input =
  31. match input with
  32. | Some i -> printfn "Input is an int=%d" i // similar to c format strings
  33. | None -> printfn "Input missing"
  34. optionPatternMatch validValue
  35. optionPatternMatch invalidValue
  36. // Stuff with data types
  37. let twoTuple = 1,2
  38. let threeTuple = "a",2,true
  39. type Person = {First:string; Last:string} // Record types have named fields
  40. let person1 = {First="john"; Last="roberts"}
  41. type Temp =
  42. | DegreesC of float
  43. | DegreesF of float
  44. let temp = DegreesF 98.6
  45. type Employee =
  46. | Worker of Person
  47. | Manager of Employee list
  48. let worker = Worker person1
  49. // Printing stuff out
  50. printfn "Printing an int %i, a float %f, a bool %b" 1 2.0 true
  51. printfn "A string %s, and something generic %A" "hello" [1;2;3;4]
  52. // You can also print complex types that have been created
  53. printfn "twoTuple=%A,\nPerson=%A,\nTemp=%A,\nEmployee=%A"
  54. twoTuple person1 temp worker