Defining EntryPoint with Composition in F #

I tried to better understand the functional way of coding and simply wrote a small program to print the factorial of a user-entered number:

open System

let fact n = let rec factiter init acc = 
                      if init = 0 then acc
                      else factiter (init - 1) init*acc
             factiter n 1


let dropStrArr (argv: string []) = ignore argv

let factComp = Console.ReadLine >> Int32.Parse >> fact >> Console.WriteLine >> fun () -> 0

[<EntryPoint>]
let main argv = (dropStrArr >> factComp) argv

This worked well, but then I thought that mainyou can determine purely in composition and try:

let main = dropStrArr >> factComp

which I thought would work, but although it compiles, it just exits right after launch.

There are different types in two scenarios:    unit -> int when the main one is determined by its argument, compared to    (unit -> int) when using composition.

I probably miss the type system, so my question is why the main meaning cannot be determined using composition here?

+4
1

, F # .

, , FSharpFunc Invoke.

:

let mul a b = a + b
let mul2 = mul 2 //point-free
let mul2P a = mul 2 a //pointed

mul2P , ( #)

static int mul2P(int a) { return mul(2, a); }

while mul2

class mul2Impl : FSharpFunc<int, int>
{       
    public int a;
    mul2Impl(int a) { this.a = a; }

    public override int Invoke(int b)
    {
        return mul(this.a, b);
    }
}

, let main argv, , FSharpFunc

factComp.Invoke(dropStrArr.Invoke(argv));

, main FSharpFunc, , .

+5

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


All Articles