F # Study Notes (function basics ),

Source: Internet
Author: User

F # Study Notes (function basics ),

Because it is just a note, you can only record what you think you should pay attention to and need to understand.

Function Definition F # has its own Syntax:

[<EntryPoint>]let main argv =     let f1 = fun x -> x * x - 2 * x    let f2 x =         let x1 = x - 2        x * x1    printfn "%i" (f2 10)    printfn "%i" (f1 10)    0 

F1 is easy to understand, because basically all statements that support lambda expressions are the first one, but the returned values are inferred by default.

F2 omitted the func and-> symbols. At first glance, the two functions are the same.

If the function has multiple parameters, the types will be different. Here I tried it in the console:

> let g2 x y z = 1.0 + x*x + y*y + z*z;;val g2 : x:float -> y:float -> z:float -> float

It is found that its type is curry-based. The test result is as follows:

[<EntryPoint>] let main argv = let g2 x y z = 1 + x + y + z let a = g2 10 let B = a 11 let c = B 12 printfn "% I "c 0 // return the integer exit code

Use ref reference parameters to pass values:

[<EntryPoint>] let main argv = let a1, b1 = ref 6, ref 9 let rChange ref x y = let t =! X: =! X +! Y: =! Y-t (x, y) let z2 = (rChange ref a1 b1) let c, d = z2 printfn "% I" (c. Value) 0 // return the integer exit code

The parameter is prefixed with the function. Here, f (x) = 2 multiplied by the square of x + 3, g (x) = root number x/2, and g (f (x ))

[<EntryPoint>] let main argv = let f x = 2.0 * x + 3.0 let g x = sqrt (x/2.0) let y = g (f 3.0) let d = 3.0 |> f |> g printfn "% f" y printfn "% f" d 0 // return the integer exit code

Let y = g (f 3.0) the compound function is used here. You can use the pipe operator to prefix the parameter: let d = 3.0 |> f |> g

The previously defined functions are all curry functions. Therefore, you cannot use multiple parameters as a front-end function. If you want to use multiple parameters as front-end functions, you should define them as follows,

[<EntryPoint>] let main argv = let f2 = fun (a, B)-> a * a + B * B let g1 x = x * (x-1) let y1 = (3, 4) |> f2 |> g1 0 // return the integer exit code

The definition of f2 is a general function definition. The two parameters are not separated, while the curry function is a high-order function. The definition of scala is like def first (x: Int) = (y: Int) => x + y

Operator as a function:

[<EntryPoint>] let main argv = let a1 = (+) 2 5 let a2 = (*) 3 4 printfn "% I" a1 printfn "% I" a2 0 // return the integer exit code

Using the prefix operator is to use + as a function.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.