Online Book Reader

Home Category

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

By Root 460 0

Couldn't match expected type `(t, t1)'

against inferred type `(t2, t3, t4)'

In the expression: (8, 11, 5)

In the expression: [(1, 2), (8, 11, 5), (4, 5)]

In the definition of `it': it = [(1, 2), (8, 11, 5), (4, 5)]

Haskell also considers tuples that have the same length but contain different types of data to be distinct types of tuples. For example, you can’t make a list of tuples like [(1,2),("One",2)], because the first is a pair of numbers, and the second is a pair containing a string followed by a number.

Tuples can be used to easily represent a wide variety of data. For instance, if we wanted to represent someone’s name and age in Haskell, we could use a triple: ("Christopher", "Walken", 55).

Remember, tuples are of a fixed size—you should only use them when you know in advance how many elements you’ll need. The reason tuples are so rigid in this way is that, as mentioned, the size of a tuple is treated as part of its type. Unfortunately, this means that you can’t write a general function to append an element to a tuple—you’d have to write a function for appending to a pair (to produce a triple), another one for appending to a triple (to produce a 4-tuple), another one for appending to a 4-tuple, and so on.

Like lists, tuples can be compared with each other if their components can be compared. However, unlike lists, you can’t compare two tuples of different sizes.

Although there are singleton lists, there’s no such thing as a singleton tuple. It makes sense when you think about it: a singleton tuple’s properties would simply be those of the value it contains, so distinguishing a new type wouldn’t give us any benefit.

Using Pairs


Storing data in pairs is very common in Haskell, and there are some useful functions in place to manipulate them. Here are two functions that operate on pairs:

fst takes a pair and returns its first component:

ghci> fst (8, 11)

8

ghci> fst ("Wow", False)

"Wow"

snd takes a pair and—surprise!—returns its second component:

ghci> snd (8, 11)

11

ghci> snd ("Wow", False)

False

Note

These functions only operate on pairs. They won’t work on triples, 4-tuples, 5-tuples, etc. We’ll go over extracting data from tuples in different ways a bit later.

The zip function is a cool way to produce a list of pairs. It takes two lists, then “zips” them together into one list by joining the matching elements into pairs. It’s a really simple function, but it can be very useful when you want to combine two lists in a particular way or traverse two lists simultaneously. Here’s a demonstration:

ghci> zip [1,2,3,4,5] [5,5,5,5,5]

[(1,5),(2,5),(3,5),(4,5),(5,5)]

ghci> zip [1..5] ["one", "two", "three", "four", "five"]

[(1,"one"),(2,"two"),(3,"three"),(4,"four"),(5,"five")]

Notice that because pairs can have different types in them, zip can take two lists that contain elements of different types. But what happens if the lengths of the lists don’t match?

ghci> zip [5,3,2,6,2,7,2,5,4,6,6] ["im","a","turtle"]

[(5,"im"),(3,"a"),(2,"turtle")]

As you can see in the above example, only as much of the longer list is used as needed—the rest is simply ignored. And because Haskell uses lazy evaluation, we can even zip finite lists with infinite lists:

ghci> zip [1..] ["apple", "orange", "cherry", "mango"]

[(1,"apple"),(2,"orange"),(3,"cherry"),(4,"mango")]

Finding the Right Triangle


Let’s wrap things up with a problem that combines tuples and list comprehensions. We’ll use Haskell to find a right triangle that fits all of these conditions:

The lengths of the three sides are all integers.

The length of each side is less than or equal to 10.

The triangle’s perimeter (the sum of the side lengths) is equal to 24.

A triangle is a right triangle if one of its angles is a right angle (a 90-degree angle). Right triangles have the useful property that if you square the lengths of the sides forming the right angle and then add those squares, that sum is equal to the square of the length of the side that’s opposite the right angle. In the picture, the sides that lie

Return Main Page Previous Page Next Page

®Online Book Reader