function values and simple values
Do not assign bindings
Let's take a look at this simple function:
Let 1
What does "X" mean? "X" means:
1: Receive some values from the input domain
2: We can first use the name "X" to represent the value so that it is referenced later.
It is called "binding" by name,and this "x" is the "determined value" of the input value.
If we calculate this function with 5 as the input value, we will replace the "X" we see in the original definition with "5", in a way that is much like a find-and-replace on text.
Let 1 5// Replace "x" with "5"/// ADD1 5 = 5 + 1 = 6// result is 6
It is important to understand that this is not a distribution. "X" is not a "slot" or a variable just assigns a value to it, and can assign another value to it later. This is just the name "X" and is worth a one-time association. This value is a defined value and cannot be changed. And, once determined, "X" cannot be changed. Associates one value at a time, always associating a value.
An important point of view in functional thinking: No variables, only values .
function Value
If you think a little more, you will find the name "add1" itself, bound to the "input value plus one function". The function itself is independent of the name it is bound to.
If you enter "letadd1 x=x+1", you are telling the F # compiler that when he sees add1 , it will replace it with a function that adds one to the input. "add1" is called a function value.
To understand that the function is independent of its name, try:
Let 1 Let 55
You can see "Add1" and "Plus1" referencing (binding) the same function.
You can always recognize a function because they have the standard signature "Domain->range". Below is a generic type of function value:
Val functionname:domain, Range
Simple Values
Think of an operation where no input always returns an integer of 5
This is a "constant" operation.
How do you write in F #? We'll tell the F # compiler that every time we see the name "C", it will replace the integer 5.
Let 5
When it executes, returns:
Val int 5
This time there is no arrow mapping, just an integer. The difference is that there is an equal sign after the actual value is printed. The F # compiler knows that this binding always returns a value of 5.
In other words, we define a constant, which is a simple value in F #.
You can always distinguish between function values and simple values, because all the simple values have signatures like this:
Val type = constant // Note that there is no arrow
Simple value comparison function value
Unlike C #, what is important in F # is that function values and simple values are a little different. They can all be bound by names (with the Let keyword) and passed. In fact, one of the key facets of functional programming is that functions can be used as input values for other functions , which we can quickly see.
Note that the function values and the simple values are subtly different. A function always has a field and a range and must provide a parameter to get a result. Once a simple value is determined, no further calculations are required. Because of their differences, we have to define a "constant function" that returns 5.
Let Fun ()5 // orlet5
The signatures of these functions are:
Val int
Rather than this:
Val int 5
After more unit, function syntax and anonymous functions.
"value" vs. "Object"
In functional programming languages like F #, many things are called "values." In object-oriented languages like C #, many things are called "objects". So what's the difference between them?
We already know in the front that the value is just a member of the domain. Integer fields, string fields, function fields, integer-to-string mappings, and so on. In principle, values cannot be changed. And the value does not have any additional behavior.
object, the standard definition is the encapsulation of data structures with related behaviors (methods). In general, objects are stateful (can be changed), and all actions that change internal must be provided by the object itself (with "." Symbol).
In F #, there are also primitive types that behave like objects, for example, you can "." Out to get the length of the string.
" ABC ". Length
However, in general, we should avoid using "objects" for standard values, keeping it because of instances of real classes or other values of burst member methods.
named Values
The standard naming conventions for values and function names, basically, any alphabetic string, including underscores. There are several extras:
You can use single quotes in the name, except that you can't put it in the first position. For example:
A'b'c begin' //valid names
The final single quotation mark is usually the flag used to emit a variant version of the value:
Let f = x let F' = derivative f let f' = derivative f'
or define the transformation of the keyword:
Let if ' b t f = if B then T else F
You can use double quotation marks in any string to make it a valid identifier.
"This is a name" " 123" //valid names
You might want to use a double quote identifier when
Unlike c#,f# 's functions and worthy naming conventions, the first letter starts with lowercase instead of uppercase (the camel is named instead of Pascal) unless it is specifically intended for exposure to others. NET language. However, the type and module are capitalized in the first letter.
Please correct me for the wrong translation.
Original address: http://fsharpforfunandprofit.com/posts/function-values-and-simple-values/
Translation Directory Portal: http://www.cnblogs.com/JayWist/p/5837982.html
function values and simple values