Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # How F# functions work.
- Functions in functional programming take some time to get used to, but I want to try to simplify it a little.
- ## Expressions
- In functional programming, nearly everything is an expression. Although you must eventually *do* something, generally you should try to isolate those parts by themselves.
- In a language like Java, `if` is a statement:
- int x;
- if (this) x = 6;
- else x = 5;
- A functional version might be:
- int x = if this then 6 else 5
- The point is that there are no "statements"; everything is an expression
- ## Binding
- 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.
- In F#, `let` is how you bind a name to a value. The word that comes after the `let` is the name. For example,
- let x = 5
- `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.
- let x = 5 in x + 7 // This evaluates to 12
- 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:
- let x = 5 in
- let y = 6 in
- x + y // evaluates to 11
- F# actually lets you replace `in` with either a newline or a semicolon, so you could also write:
- let x = 5
- let y = 6
- x + y
- But keeep in mind that the two snippets are exactly the same thing.
- ## Functions
- A function is something which takes one input and returns one output. In F#, `fun` is how you make a function. For example:
- (fun x -> x + 5)
- To apply the function, simply place the argument after the function, like so:
- (fun x -> x + 5) 52 // This would result in 57
- Now, you probably don't want to rewrite the function every time you call it, so let's use `let`!!
- let add_five = (fun x -> x + 5) in
- add_five 52
- Let's make this a little more general, by making a function which makes functions that add numbers.
- let adder_factory = (fun number_to_add -> (fun x -> x + number_to_add)) in
- let add_five = adder_factory 5 in
- add_five 52
- We don't even have to name `add_five` if we don't have to:
- let adder_factory = (fun number_to_add -> (fun x -> x + number_to_add)) in
- (adder_factory 5) 52
- Let me do some transformations:
- let adder_factory = (fun y -> (fun x -> x + y)) in
- (adder_factory 5) 52
- We don't actually need the parenthesis at the end, since F# reads it left-to-right.
- let adder_factory = (fun y -> (fun x -> x + y)) in
- adder_factory 5 52
- 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.
- 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.
- let adder_factory = (fun y x -> x + y) in
- adder_factory 5 52
- If you are binding a function, you can put the arguments next to the name, like the following:
- let adder_factory y x = x + y in
- adder_factory 5 52
- 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.
- let adder_factory = (fun number_to_add -> (fun x -> x + number_to_add)) in
- let add_five = adder_factory 5 in
- add_five 52
- What if we just simplified the `adder_factory` binding?
- let adder_factory y x = x + y in
- let add_five = adder_factory 5 in
- add_five 52
- 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.
- Now, operators in F# are exactly the same as functions, but you have to be careful with how you put them places.
- 5 + 6 // 11
- This expression is the same as the following:
- (+) 5 6
- 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`
- let (+) y x = x + y in
- let add_five = (+) 5 in
- add_five 52
- 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