Advertisement
Guest User

Untitled

a guest
Oct 5th, 2018
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
F# 4.25 KB | None | 0 0
  1. // single line comments use a double slash
  2. (* multi line comments use (* . . . *) pair -end of multi line comment- *)
  3.  
  4. // ======== "Variables" (but not really) ==========
  5. // The "let" keyword defines an (immutable) value
  6. let myInt = 5
  7. let myFloat = 3.14
  8. let myString = "hello"  //note that no types needed
  9.  
  10. // ======== Lists ============
  11. let twoToFive = [2;3;4;5]        // Square brackets create a list with
  12.                                  // semicolon delimiters.
  13. let oneToFive = 1 :: twoToFive   // :: creates list with new 1st element
  14. // The result is [1;2;3;4;5]
  15. let zeroToFive = [0;1] @ twoToFive   // @ concats two lists
  16.  
  17. // IMPORTANT: commas are never used as delimiters, only semicolons!
  18.  
  19. // ======== Functions ========
  20. // The "let" keyword also defines a named function.
  21. let square x = x * x          // Note that no parens are used.
  22. square 3                      // Now run the function. Again, no parens.
  23.  
  24. let add x y = x + y           // don't use add (x,y)! It means something
  25.                               // completely different.
  26. add 2 3                       // Now run the function.
  27.  
  28. // to define a multiline function, just use indents. No semicolons needed.
  29. let evens list =
  30.    let isEven x = x%2 = 0     // Define "isEven" as an inner ("nested") function
  31.    List.filter isEven list    // List.filter is a library function
  32.                               // with two parameters: a boolean function
  33.                               // and a list to work on
  34.  
  35. evens oneToFive               // Now run the function
  36.  
  37. // You can use parens to clarify precedence. In this example,
  38. // do "map" first, with two args, then do "sum" on the result.
  39. // Without the parens, "List.map" would be passed as an arg to List.sum
  40. let sumOfSquaresTo100 =
  41.    List.sum ( List.map square [1..100] )
  42.  
  43. // You can pipe the output of one operation to the next using "|>"
  44. // Here is the same sumOfSquares function written using pipes
  45. let sumOfSquaresTo100piped =
  46.    [1..100] |> List.map square |> List.sum  // "square" was defined earlier
  47.  
  48. // you can define lambdas (anonymous functions) using the "fun" keyword
  49. let sumOfSquaresTo100withFun =
  50.    [1..100] |> List.map (fun x->x*x) |> List.sum
  51.  
  52. // In F# returns are implicit -- no "return" needed. A function always
  53. // returns the value of the last expression used.
  54.  
  55. // ======== Pattern Matching ========
  56. // Match..with.. is a supercharged case/switch statement.
  57. let simplePatternMatch =
  58.    let x = "a"
  59.    match x with
  60.     | "a" -> printfn "x is a"
  61.     | "b" -> printfn "x is b"
  62.     | _ -> printfn "x is something else"   // underscore matches anything
  63.  
  64. // Some(..) and None are roughly analogous to Nullable wrappers
  65. let validValue = Some(99)
  66. let invalidValue = None
  67.  
  68. // In this example, match..with matches the "Some" and the "None",
  69. // and also unpacks the value in the "Some" at the same time.
  70. let optionPatternMatch input =
  71.    match input with
  72.     | Some i -> printfn "input is an int=%d" i
  73.     | None -> printfn "input is missing"
  74.  
  75. optionPatternMatch validValue
  76. optionPatternMatch invalidValue
  77.  
  78. // ========= Complex Data Types =========
  79.  
  80. // Tuple types are pairs, triples, etc. Tuples use commas.
  81. let twoTuple = 1,2
  82. let threeTuple = "a",2,true
  83.  
  84. // Record types have named fields. Semicolons are separators.
  85. type Person = {First:string; Last:string}
  86. let person1 = {First="john"; Last="Doe"}
  87.  
  88. // Union types have choices. Vertical bars are separators.
  89. type Temp =
  90.     | DegreesC of float
  91.     | DegreesF of float
  92. let temp = DegreesF 98.6
  93.  
  94. // Types can be combined recursively in complex ways.
  95. // E.g. here is a union type that contains a list of the same type:
  96. type Employee =
  97.   | Worker of Person
  98.   | Manager of Employee list
  99. let jdoe = {First="John";Last="Doe"}
  100. let worker = Worker jdoe
  101.  
  102. // ========= Printing =========
  103. // The printf/printfn functions are similar to the
  104. // Console.Write/WriteLine functions in C#.
  105. printfn "Printing an int %i, a float %f, a bool %b" 1 2.0 true
  106. printfn "A string %s, and something generic %A" "hello" [1;2;3;4]
  107.  
  108. // all complex types have pretty printing built in
  109. printfn "twoTuple=%A,\nPerson=%A,\nTemp=%A,\nEmployee=%A"
  110.          twoTuple person1 temp worker
  111.  
  112. // There are also sprintf/sprintfn functions for formatting data
  113. // into a string, similar to String.Format.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement