F # lambda expression without parameters

I am new to F # and I want to use a lambda expression without parameters. This works great:

let a = fun () -> 2
a()

It returns 2.

But now I want him to come back 6:

let a = 3* (fun () -> 2)
a()

But you will get a static error that has too many arguments or cannot be used in this context.

But this also works:

let a = 3* (fun _ -> 2) 4
a

Note that a can no longer be used as a function like ()

Can you explain this?

+4
source share
2 answers

, fun() -> 2 . , , , . , - . , , . . , a(). () (, - F #, ), . 2. .

, : . . .

, ... ? . , ? ? - , .

, 6 2, :

let a = fun() -> 2*3

, , , - (, ) , :

let a = fun() -> 2
let b = fun() -> a() * 3

- .

-, fun _ -> 2. . . , . : " , , , ". , . , 4 . 4, . 4 . , (fun _ -> 2) 4. " , 2, 4".

, , :

let a = fun _ -> 2
let b = a 4   // b = 2

, , 4 3. 6. . . , (, ). , , . .

+7

. , , , , :

let a = 3* (fun () -> 2)()

, a = 6

+2

Source: https://habr.com/ru/post/1668444/


All Articles