Why is my application running top to bottom

I thought I understood the execution of the F # code, but it is clear that I missed something. When I run the following

#!/usr/bin/env fsharpi let a = System.Console.WriteLine("Function A") let b = System.Console.WriteLine("Function B") let c = System.Console.WriteLine("Function C") c b a 

I get the output:

 Function A Function B Function C 

Therefore, for some reason, it performs functions when it reads them instead of calls to functions that are in the reverse order.

Why is this?

+5
source share
1 answer

I think you do not understand what this line means:

 let a = System.Console.WriteLine("Function A") 

It assigns the result from System.Console.WriteLine("Function A") to a . If you run it, you will see that a printed as unit :

 val a : unit = () 

And at that time, "Function A" was already written to the console.

Perhaps you need a function, not a value:

 let a() = System.Console.WriteLine("Function A") 

It can be called using a() . If you put it all together:

 let a() = System.Console.WriteLine("Function A") let b() = System.Console.WriteLine("Function B") let c() = System.Console.WriteLine("Function C") c() b() a() 

You will get what you expect:

 Function C Function B Function A 
+7
source

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


All Articles