Advertisement
Guest User

Untitled

a guest
Jul 21st, 2017
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.53 KB | None | 0 0
  1. # How F# functions work.
  2.  
  3. Functions in functional programming take some time to get used to, but I want to try to simplify it a little.
  4.  
  5. ## Expressions
  6.  
  7. In functional programming, nearly everything is an expression. Although you must eventually *do* something, generally you should try to isolate those parts by themselves.
  8.  
  9. In a language like Java, `if` is a statement:
  10.  
  11. int x;
  12. if (this) x = 6;
  13. else x = 5;
  14.  
  15. A functional version might be:
  16.  
  17. int x = if this then 6 else 5
  18.  
  19. The point is that there are no "statements"; everything is an expression
  20.  
  21. ## Binding
  22.  
  23. A "binding" in a programming language is when a name is given to a specific value. This makes the most sense in a functional programming language because the value that the name refers to cannot be change.
  24.  
  25. In F#, `let` is how you bind a name to a value. The word that comes after the `let` is the name. For example,
  26.  
  27. let x = 5
  28.  
  29. `x` is the name, `5` is the value. But remember how everything is an expression? What does this evaluate to? To fix this, you need to use the `in` keyword.
  30.  
  31. let x = 5 in x + 7 // This evaluates to 12
  32.  
  33. So, `let` is how you bind a value to a name inside a certain expression. What if you want to bind multiple values in the same expression? Well, you can do it twice:
  34.  
  35. let x = 5 in
  36. let y = 6 in
  37. x + y // evaluates to 11
  38.  
  39. F# actually lets you replace `in` with either a newline or a semicolon, so you could also write:
  40.  
  41. let x = 5
  42. let y = 6
  43. x + y
  44.  
  45. But keeep in mind that the two snippets are exactly the same thing.
  46.  
  47. ## Functions
  48.  
  49. A function is something which takes one input and returns one output. In F#, `fun` is how you make a function. For example:
  50.  
  51. (fun x -> x + 5)
  52.  
  53. To apply the function, simply place the argument after the function, like so:
  54.  
  55. (fun x -> x + 5) 52 // This would result in 57
  56.  
  57. Now, you probably don't want to rewrite the function every time you call it, so let's use `let`!!
  58.  
  59. let add_five = (fun x -> x + 5) in
  60. add_five 52
  61.  
  62. Let's make this a little more general, by making a function which makes functions that add numbers.
  63.  
  64. let adder_factory = (fun number_to_add -> (fun x -> x + number_to_add)) in
  65. let add_five = adder_factory 5 in
  66. add_five 52
  67.  
  68. We don't even have to name `add_five` if we don't have to:
  69.  
  70. let adder_factory = (fun number_to_add -> (fun x -> x + number_to_add)) in
  71. (adder_factory 5) 52
  72.  
  73. Let me do some transformations:
  74.  
  75. let adder_factory = (fun y -> (fun x -> x + y)) in
  76. (adder_factory 5) 52
  77.  
  78. We don't actually need the parenthesis at the end, since F# reads it left-to-right.
  79.  
  80. let adder_factory = (fun y -> (fun x -> x + y)) in
  81. adder_factory 5 52
  82.  
  83. Notice that `adder_factory` is basically a function that takes two arguments; in F# there are actually no functions that take more than one argument; instead, they are just nested functions like I did here.
  84.  
  85. That would be really inconvenient if it were the only way to program, so F# provides some syntactical convenience to make things look nicer. You can merge the nested functions into one function that looks like it takes two arguments.
  86.  
  87. let adder_factory = (fun y x -> x + y) in
  88. adder_factory 5 52
  89.  
  90. If you are binding a function, you can put the arguments next to the name, like the following:
  91.  
  92. let adder_factory y x = x + y in
  93. adder_factory 5 52
  94.  
  95. At this point, you can't really tell that `adder_factory` is a nested function, but that's really what it is. Let's go back to the unsimplified form and take a different route to simplifying it.
  96.  
  97. let adder_factory = (fun number_to_add -> (fun x -> x + number_to_add)) in
  98. let add_five = adder_factory 5 in
  99. add_five 52
  100.  
  101. What if we just simplified the `adder_factory` binding?
  102.  
  103. let adder_factory y x = x + y in
  104. let add_five = adder_factory 5 in
  105. add_five 52
  106.  
  107. This would seem very weird if you thought that `adder_factory` was a function that took two arguments, but since we know it's really just a nested function in disguise, we are okay with it.
  108.  
  109. Now, operators in F# are exactly the same as functions, but you have to be careful with how you put them places.
  110.  
  111. 5 + 6 // 11
  112.  
  113. This expression is the same as the following:
  114.  
  115. (+) 5 6
  116.  
  117. You need to use parenthesis if you want to put the parameters after the function because `+` is an operator and operators are special. You could actually define `+` just like we did with `adder_factory`
  118.  
  119. let (+) y x = x + y in
  120. let add_five = (+) 5 in
  121. add_five 52
  122.  
  123. Of course, we did define `+` in terms of itself, but you can overlook that, can't you?
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement