If I force Haskell to infer the type of a number I'll get Num a => a
. For example:
Prelude> :t 1
1 :: Num a => a
But what does a => a
mean?
Num is a typeclass — a group of types — which includes all types which are regarded as numbers. The (Num a) => part of the signature restricts a to number types – or, in Haskell terminology, instances of Num .
In Haskell read function is used to deal with strings, if you want to parse any string to any particular type than we can use read function from the Haskell programming. In Haskell read function is the in built function, that means we do not require to include or install any dependency for it, we can use it directly.
Haskell is a functional language and it is strictly typed, which means the data type used in the entire application will be known to the compiler at compile time.
The Data Keyword and Constructors In general, we define a new data type by using the data keyword, followed by the name of the type we're defining. The type has to begin with a capital letter to distinguish it from normal expression names. To start defining our type, we must provide a constructor.
1 :: Num a => a
means that 1
has some type a
, where a
is an instance of the Num
typeclass. Note that Num
is not a type, but a typeclass, which describes common properties of various types. The Num
typeclass, for example, describes types that are numeric, and so support basic arithmetic. The native machine integer type Int
is an instance of Num
, as is the arbitrary-sized Integer
, the floating point type Double
, and even the rational number type Rational
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With