C.3 Primitive Types

Haskell has the following primitive types: fixed precision integer (Int), arbitrary precision integer (Integer), single precision real (Float), boolean (Bool), and character (Char). The type of a string is [Char] (i.e., a list of characters); the type String is an alias for [Char]. The interpreter command :type <expression> (also :t <expression>) returnsthetypeof <expression>:

A set of 10 code lines in Haskell with five primitive types: Fixed precision integer, arbitrary precision integer, single precision real, Boolean, and character.
Description

Notice from lines 1–10 that Haskell uses type inference. The :: double-colon symbol associates a value with a type and is read as “is of type.” For instance, the expression a :: Char indicates that ′a′ is of type Char. This explains the responses of the interpreter on lines 2, 4, 6, 8, and 10 when an expression is entered prefaced with a :type. The responses from the interpreter for the expressions 3 (line 8) and 3.3 (line 10) require some explanation. In response to the expression :type 3 (line 7), the interpreter prints 3 :: Num a => a (line 8). Here, the a means “any type” and is called a type variable. Identifiers for type variables must begin with a lowercase letter (traditionally a, b, and so on are used). Before we can explain the meaning of the entire expression 3 :: Num a => a (line 8), we must first discuss type classes.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.137.184.3