Type Inference Give the∨ alue a name You do not have to specify the types of F# constructs except F# uses let bindings to bind nam when the compiler let <identifier> cannot conclusively let num= I0 deduce the type let str ="F# The type of a function F# binds function names to lambda value is specitied by using let <function-name>= <la the -> token Hence let squarelt= fun n squarelt2 has type //more concise Int-> Int let squarelt2n=n为n
Give the Value a Name • F# uses let bindings to bind names to values: • F# binds function names to lambda expressions: let <identifier> = <value> let num = 10 let str = "F#" let <function-name> = <lambda-expression> let squareIt = fun n -> n * n //more concise syntax let squareIt2 n = n * n Type Inference You do not have to specify the types of F# constructs except when the compiler cannot conclusively deduce the type The type of a function value is specified by using the -> token. Hence squareIt2 has type: int -> int
Store the value in a data structure / Lists let integerList =[ 1; 2; 3: 4: 5: 6:7] let stringlist=[" one": two", three"] //In F#, functions can be stored in a list, as long as the functions have the same signature let squarelt= fun n->n*n let doublet= fun n->2*n let funList=[ squarelt; doublet ∥/ Tuples // A tuple does not require its elements to be of the same type let more MixedTuple=( num, "two", 3.3, squarelt
Store the Value in a Data Structure // Lists. let integerList = [ 1; 2; 3; 4; 5; 6; 7 ] let stringList = [ "one"; "two"; "three" ] // In F#, functions can be stored in a list, as long as the functions have the same signature. let squareIt = fun n -> n * n let doubleIt = fun n -> 2 * n let funList = [ squareIt; doubleIt ] // Tuples. // A tuple does not require its elements to be of the same type. let moreMixedTuple = ( num, "two", 3.3, squareIt )
ass a tunction as an argument In the simplest cases, it is similar to using delegate type as parameter type in C# code Passing a method call as an argument in C# code It is the common abstractions in F#. That is, you focus on function compositions than the implementation of argument function let applyIt fun op arg ->op arg System. Console. WriteLine (applyIt squareIt num) System. Console. WriteLine(squareIt num)
Pass a function as an argument • In the simplest cases, it is similar to • using delegate type as parameter type in C# code • Passing a method call as an Argument in C# code • It is the common abstractions in F#. That is, you focus on function compositions than the implementation of argument function. let applyIt = fun op arg -> op arg System.Console.WriteLine(applyIt squareIt num) System.Console.WriteLine(squareIt num)
Return a function from a function call It is to define a new function from a function call. and to bind a name to the new function the implicit currying in F# function declarations
Return a function from a function call • It is to define a new function from a function call, and to bind a name to the new function. • the implicit currying in F# function declarations
let compose fun op1 op2-> fun n-> op1 (op2 n) let compose2= fun op1 op2-> let fun ToReturn fun n-> op1 (op2 n) fun ToReturn //Or, integrating the more concise syntax: let compose op1 op2 let fun ToReturn fun n->op1(op2 n) fun ToReturn
let compose = fun op1 op2 -> fun n -> op1 (op2 n) let compose2 = fun op1 op2 -> let funToReturn = fun n -> op1 (op2 n) funToReturn // Or, integrating the more concise syntax: let compose3 op1 op2 = let funToReturn = fun n -> op1 (op2 n) funToReturn