concept unit in category f#

This is an excerpt from Manning's book Get Programming with F#: A guide for .NET developers.
Functions as values. The ability to easily create, use and share functions as a unit of abstraction and composition within a system.
I mentioned that F# doesn’t allow methods to return void. How on earth does this work in F#, then—particularly when the BCL probably has thousands of methods that return void? The answer is that F# has a type called unit. You’ve probably seen this term floating around in IntelliSense occasionally. The unit type is found in place of any method that would in C# normally return void, but unlike void, appears in F# to be a regular object that can get returned from any piece of code. In this way, you can say that every function returns a value—even if that value is unit. Similarly, every function can be thought of as always taking in at least one input value, even if that value is unit.
Let’s look at functions and methods from both the preceding code and the BCL. You can even bind the value of unit to a symbol, like any other normal value!
Why is all this unit business important? What’s wrong with void? One reason is that void is a special case within the C# type system. Normal rules don’t apply to it, which is why in many situations you have two versions of the same type in the BCL. A good example is the Task type in .NET, which has both Task and Task<T>. In F#, all you need is Task<T>, because even a function that returns nothing would be Task<unit>.
unit isn’t quite an object
Unfortunately, despite unifying the type system, at runtime unit doesn’t behave quite like a proper .NET object. For example, don’t try to call GetHashCode() or GetType() on it, because you’ll get a null reference exception. Hopefully, a future version of F# will fix this, but you can still think of unit as a singleton object if it helps you to visualize it.

This is an excerpt from Manning's book F# Deep Dives.
The printfn function is useful for its side effects and not for its result. For such functions, F# provides a type called unit, which doesn’t represent any information. The only possible value of type unit is written as (), and you can also see this as a tuple containing zero elements. So, the line val it : unit = () means the expression has been evaluated to a value () of type unit, which FSI assigned to a temporary variable named it. If you have an expression that returns unit, you can sequentially compose it with other expressions using the semicolon operator, or by using the newline separator as shown in listing 1.
In F#, you can annotate numeric values with information about units, and the compiler ensures that units are used in a consistent way. Consider this F# Interactive session (with the F# Interactive output typeset in italic):

This is an excerpt from Manning's book Real-World Functional Programming with Examples in F# and C#.
Refactoring and unit testing
The F# language and libraries support this type of programming in many ways, and we’ll look at all the important technologies involved. To obtain the data, we can use asynchronous workflows based on the computation expression syntax that we introduced in the previous chapter. Then we’ll look at “bringing a structure” to the data using F# types. We’ll also use units of measure that allow us to specify that a certain value isn’t just a floating-point number but that it has a unit such as square kilometers.
When reading the values of many of the indicators from the XML data, we could just convert them to float values. That would work, because both the surface area and forestation percentages are numbers, but it wouldn’t tell us much about the data. The purpose of converting the data from untyped XML into a typed F# data structure is to annotate it with types that help us understand the meaning of the values. To make the type more specific, we can use units of measure, which we mentioned in chapter 2. Using this feature, we can specify that surface is measured in square kilometers and the area covered by forests in percentage of the total area. Let’s start by looking at a couple of examples that introduce units of measure.
Working with units of measure in F# is very easy, which is why we’ve introduced them as a brief digression in this chapter. We can declare a measure using the type keyword with a special attribute. Strictly speaking, a measure isn’t a type, but we can use it as part of another type. Let’s start by defining two simple measures to represent hours and kilometers:
[<Measure>] type km [<Measure>] type hAs you can see, we’re using the Measure attribute to specify that the type is a measure. This is a special attribute that the F# compiler understands. Instead of defining units ourselves, we could also use the standard set in the FSharp.PowerPack.dll library, but for now we’ll use our own declarations. Now that we have units km and h, we can create values that represent kilometers or hours. Listing 13.15 shows how to create values with units and how to write a function that calculates with them.
Listing 13.15. Writing calculations using units of measure (F#)
> let length = 9.0<km>;; ❶ val length : float<km> = 9.0 > length * length;; ❷ val it : float<km^2> = 81.0 > let distanceInTwoHours(speed:float<km/h>) = ❸ speed * 2.0<h>;; val distanceInTwoHours : float<km/h> -> float<km> ❹ > distanceInTwoHours(30.0<km/h>);; val it : float<km> = 60.0When we want to specify units of a numeric constant, we append the unit in angle brackets to the value ❶. We started by defining a value that represents a length in kilometers. If we write a calculation using value with units, F# automatically infers the units of the result, so we can see that multiplying two distances gives us an area in square kilometers ❷. When specifying units, we can use the conventional notation, so ^ represents a power, / is used for division, and multiplication is written by juxtaposing the units.
The next example shows that we can write functions with parameters that include information about the units. Our sample function takes a speed and returns the distance traveled in two hours ❸. We want the parameter to be specified in kilometers per hour, so we add a type annotation that contains the unit. This is written by placing the unit in angle braces in the same way we do when specifying type arguments of a type such as list<int>. The F# compiler infers the return type for us ❹ just as it does when working with ordinary types. As usual, this provides a useful clue to understanding what a function does when you’re reading it. It’s also a valuable check to avoid making simple mistakes when writing the function—if we were trying to calculate a distance but ended up with a return type using a unit of time, we’d know something was wrong.
In our World Bank data, we’ll use the unit km^2 to represent the total area of a country. So far, so good—but the second indicator that we obtained is provided as a percentage. How can we specify the unit of a percentage? Even though units of measure are primarily used to represent physical units, we can use them to represent percentages as well:
[<Measure>] type percent let coef = 33.0<percent>This code creates a unit for specifying that a number represents a percentage and then defines a constant coef, which has a value of 33 percent. Strictly speaking, the value in percents doesn’t have a unit, because it’s a coefficient, but defining it as a unit is quite useful. To demonstrate, let’s try to calculate 33 percent of a 50 kilometer distance. Since coef represents a coefficient, we can simply multiply the two values:
> 50.0<km> * coef;; val it : float<km percent> = 1650.0This is obviously wrong. We want the result to be in kilometers, but if you look at the inferred type, you can see that the result is in kilometers multiplied by our new percent unit. Since we’re running the code interactively, we can also see that the number is too high, but the great thing about units of measure is that we can spot the error during the type checking without actually running the program. So what went wrong? The problem is that a percentage value represents a coefficient multiplied by 100. To write the calculation correctly, we need to divide the value by 100 percent:
> 50.0<km> * coef / 100.0<percent>;; val it : float<km> = 16.5As you can see, this is much better. We divided the result by 100 percent, which means that we don’t have the percent unit in the result. F# automatically simplifies the units, and it knows that km percent/percent is the same thing as km. This example demonstrates a significant reason for using units of measure: just like other types, they help us catch a large number of errors as early as possible.