Online Book Reader

Home Category

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

By Root 512 0
[(>4),(<10),odd] creates a function that will take a number and feed it to all of the predicates in [(>4),(<10),odd] and return a list of Booleans. It turns a list with the type (Num a) => [a -> Bool] into a function with the type (Num a) => a -> [Bool]. Pretty neat, huh?

Because lists are homogenous, all the functions in the list must be functions of the same type. You can’t have a list like [ord, (+3)], because ord takes a character and returns a number, whereas (+3) takes a number and returns a number.

When used with [], sequenceA takes a list of lists and returns a list of lists. It actually creates lists that have all possible combinations of their elements. For illustration, here’s the preceding example done with sequenceA and then done with a list comprehension:

ghci> sequenceA [[1,2,3],[4,5,6]]

[[1,4],[1,5],[1,6],[2,4],[2,5],[2,6],[3,4],[3,5],[3,6]]

ghci> [[x,y] | x <- [1,2,3], y <- [4,5,6]]

[[1,4],[1,5],[1,6],[2,4],[2,5],[2,6],[3,4],[3,5],[3,6]]

ghci> sequenceA [[1,2],[3,4]]

[[1,3],[1,4],[2,3],[2,4]]

ghci> [[x,y] | x <- [1,2], y <- [3,4]]

[[1,3],[1,4],[2,3],[2,4]]

ghci> sequenceA [[1,2],[3,4],[5,6]]

[[1,3,5],[1,3,6],[1,4,5],[1,4,6],[2,3,5],[2,3,6],[2,4,5],[2,4,6]]

ghci> [[x,y,z] | x <- [1,2], y <- [3,4], z <- [5,6]]

[[1,3,5],[1,3,6],[1,4,5],[1,4,6],[2,3,5],[2,3,6],[2,4,5],[2,4,6]]

(+) <$> [1,2] <*> [4,5,6] results in a nondeterministic computation x + y, where x takes on every value from [1,2] and y takes on every value from [4,5,6]. We represent that as a list that holds all of the possible results. Similarly, when we call sequenceA [[1,2],[3,4],[5,6]], the result is a nondeterministic computation [x,y,z], where x takes on every value from [1,2], y takes on every value from [3,4] and so on. To represent the result of that nondeterministic computation, we use a list, where each element in the list is one possible list. That’s why the result is a list of lists.

When used with I/O actions, sequenceA is the same thing as sequence! It takes a list of I/O actions and returns an I/O action that will perform each of those actions and have as its result a list of the results of those I/O actions. That’s because to turn an [IO a] value into an IO [a] value, to make an I/O action that yields a list of results when performed, all those I/O actions must be sequenced so that they’re then performed one after the other when evaluation is forced. You can’t get the result of an I/O action without performing it.

Let’s sequence three getLine I/O actions:

ghci> sequenceA [getLine, getLine, getLine]

heyh

ho

woo

["heyh","ho","woo"]

In conclusion, applicative functors aren’t just interesting, they’re also useful. They allow us to combine different computations—such as I/O computations, nondeterministic computations, computations that might have failed, and so on—by using the applicative style. Just by using <$> and <*>, we can employ normal functions to uniformly operate on any number of applicative functors and take advantage of the semantics of each one.

Chapter 12. Monoids

This chapter features another useful and fun type class: Monoid. This type class is for types whose values can be combined together with a binary operation. We’ll cover exactly what monoids are and what their laws state. Then we’ll take a look at some monoids in Haskell and how they can be of use.

First, let’s take a look at the newtype keyword, because we’ll be using it a lot when we delve into the wonderful world of monoids.

Wrapping an Existing Type into a New Type


So far, you’ve learned how to make your own algebraic data types by using the data keyword. You’ve also seen how to give existing types synonyms with the type keyword. In this section, we’ll look at how to make new types out of existing data types by using the newtype keyword. We’ll also talk about why we would want to do that in the first place.

In Chapter 11, you saw a couple of ways for the list type to be an applicative functor. One way is to have <*> take every function out of the list that is its left parameter and apply that to every value in the list that

Return Main Page Previous Page Next Page

®Online Book Reader