Online Book Reader

Home Category

Learn You a Haskell for Great Good! - Miran Lipovaca [12]

By Root 505 0
length has its own type. So the tuple (True, 'a') has a type of (Bool, Char), and ('a','b','c') has a type of (Char, Char, Char). 4 == 5 will always return False, so its type is Bool.

Functions also have types. When writing our own functions, we can choose to give them an explicit type declaration. This is generally considered to be good practice (except when writing very short functions). From here on, we’ll give all the functions that we make type declarations.

Remember the list comprehension we made in Chapter 1—the one that filters out a string’s lowercase letters? Here’s how it looks with a type declaration:

removeNonUppercase :: [Char] -> [Char]

removeNonUppercase st = [ c | c <- st, c `elem` ['A'..'Z']]

The removeNonUppercase function has a type of [Char] -> [Char], meaning that it takes one string as a parameter and returns another as a result.

But how do we specify the type of a function that takes several parameters? Here’s a simple function that takes three integers and adds them together:

addThree :: Int -> Int -> Int -> Int

addThree x y z = x + y + z

The parameters and the return type are separated by -> characters, with the return type always coming last in the declaration. (In Chapter 5, you’ll see why they’re all separated with ->, instead of having a more explicit distinction.)

If you want to give your function a type declaration, but are unsure as to what it should be, you can always just write the function without it, and then check it with :t. Since functions are expressions, :t works on them in the same way as you saw at the beginning of this section.

Common Haskell Types

Let’s take a look at some common Haskell types, which are used for representing basic things like numbers, characters, and Boolean values. Here’s an overview:

Int stands for integer. It’s used for whole numbers. 7 can be an Int, but 7.2 cannot. Int is bounded, which means that it has a minimum value and a maximum value.

Note

We’re using the GHC compiler, where the range of Int is determined by the size of a machine word on your computer. So if you have a 64-bit CPU, it’s likely that the lowest Int on your system is -263, and the highest is 263.

Integer is also used to store integers, but it’s not bounded, so it can be used to represent really big numbers. (And I mean really big!) However, Int is more efficient. As an example, try saving the following function to a file:

factorial :: Integer -> Integer

factorial n = product [1..n]

Then load it into GHCi with :l and test it:

ghci> factorial 50

30414093201713378043612608166064768844377641568960512000000000000

Float is a real floating-point number with single precision. Add the following function to the file you’ve been working in:

circumference :: Float -> Float

circumference r = 2 * pi * r

Then load and test it:

ghci> circumference 4.0

25.132742

Double is a real floating-point number with double the precision. Double-precision numeric types use twice as many bits to represent numbers. The extra bits increase their precision at the cost of hogging more memory. Here’s another function to add to your file:

circumference' :: Double -> Double

circumference' r = 2 * pi * r

Now load and test it. Pay particular attention to the difference in precision between circumference and circumference'.

ghci> circumference' 4.0

25.132741228718345

Bool is a Boolean type. It can have only two values: True and False.

Char represents a Unicode character. It’s denoted by single quotes. A list of characters is a string.

Tuples are types, but their definition depends on their length as well as the types of their components. So, theoretically, there is an infinite number of tuple types. (In practice, tuples can have at most 62 elements—far more than you’ll ever need.) Note that the empty tuple () is also a type, which can have only a single value: ().

Type Variables

It makes sense for some functions to be able to operate on various types. For instance, the head function takes a list and returns the head element

Return Main Page Previous Page Next Page

®Online Book Reader