Does F # have free functions?

Should each function be inside a type like in C #? Or does F # have free functions?

Also what about the functions that I saw throughout some F # code, for example, cos, sin, etc. Are they written to Math.Cos, Math.Sin?

Also why do they provide cos, sin, etc., and not Math.Cos, Math.Sin?

+4
source share
4 answers

F # may have functions in modules (instead of classes).

The CLR implements these internal ones as:

as a CLR class that has only static elements

They look like "free functions" within the module.

+7
source

As Reed mentioned, functions can be defined within a la modules of other ML languages, but they can also be defined on types such as other .NET languages ​​(and in fact modules are also compiled for classes). Inside F #, modules can be open ed to access functions without having to use the module name each time, and modules are automatically created for each file if they are not explicitly declared.

As for the individual cos functions, etc. Instead of just relying on the built-in .NET Math.Cos , etc., there may be several reasons:

  • Homogeneity: cos looks much more idiomatic in F # than calling the static System.Math.Cos method.
  • Static restrictions: cos works not only on float in F #, but also works on float32 and on any type that the static cos method provides, so you can create your own types, you can use the cos function.

I would suggest that truly free features are not allowed, because there are many .NET languages ​​that do not provide convenient access to them, so using them can become an obstacle to cross-language compatibility. The CLR itself supports free functions, and some languages, such as C ++ / CLI, use them, but most languages ​​compile functions into classes (even if this is not the way it looks from the point of view of the source code, as in F #).

+7
source

The answer to @Reed is good. I just wanted to add that in F # you can “open” modules so that the functions associated with the module can be called in an unqualified manner, and this is where various functions and top-level operators occur, for example

http://msdn.microsoft.com/en-us/library/ee353754(VS.100).aspx

That is, F # is still subject to CLR mechanisms, which means that every function must be in a class, but F # modules are mapped to static classes in the CLR, modules can be “opened” to provide functions at the top, and several modules “automatically” open "so that the most common / convenient features are immediately available.

Having many common functions available is very useful for scripts or interactive sessions where you do not want to open a bunch of namespaces or write 15 characters "System.Math.Cos" just to calculate the cosine.

+4
source

Joan,

Instead of all these questions, which features of F # support or do not support, perhaps you better start working with a book in F #. Consider these two resources as a starting point:

http://en.wikibooks.org/wiki/F_Sharp_Programming

And:

F # Survival Guide

There is also a Chris Smith book, which is an excellent opening text.

Programming F #

Maybe you want to read them and work through them, and then you can ask more questions asked. Just an idea.

+3
source

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


All Articles