• No se han encontrado resultados

Consideraciones de la madera de A mangium luego del proceso de secado.

Consideraciones y recomendaciones prácticas para mejorar la calidad de la madera seca de Acacia mangium Willd.

3. Consideraciones de la madera de A mangium luego del proceso de secado.

7.8.1

Standard List Functions

Recall that the definition of the built-in Haskell list datatype is equivalent to:

data List a = Nil

7.8. MORE LISTS 93 With the exception thatNilis called[]andCons x xsis calledx:xs. This is simply to make pattern matching easier and code smaller. Let’s investigate how some of the standard list functions may be written. Considermap. A definition is given below:

map _ [] = []

map f (x:xs) = f x : map f xs

Here, the first line says that when youmapacross an empty list, no matter what the function is, you get an empty list back. The second line says that when youmapacross a list withxas the head andxsas the tail, the result isfapplied toxconsed onto the result of mappingfonxs.

Thefiltercan be defined similarly:

filter _ [] = []

filter p (x:xs) | p x = x : filter p xs | otherwise = filter p xs

How this works should be clear. For an empty list, we return an empty list. For a non empty list, we return the filter of the tail, perhaps with the head on the front, depending on whether it satisfies the predicatepor not.

We can definefoldras:

foldr _ z [] = z

foldr f z (x:xs) = f x (foldr f z xs)

Here, the best interpretation is that we are replacing the empty list ([]) with a particular value and the list constructor (:) with some function. On the first line, we can see the replacement of[]forz. Using backquotes to makefinfix, we can write the second line as:

foldr f z (x:xs) = x ‘f‘ (foldr f z xs)

From this, we can directly see how:is being replaced byf. Finally,foldl:

foldl _ z [] = z

foldl f z (x:xs) = foldl f (f z x) xs

This is slightly more complicated. Remember,zcan be thought of as the current state. So if we’re folding across a list which is empty, we simply return the current state. On the other hand, if the list is not empty, it’s of the formx:xs. In this case, we get a new state by applingfto the current statezand the current list elementxand then recursively callfoldlonxswith this new state.

There is another class of functions: thezipandunzipfunctions, which respec- tively take multiple lists and make one or take one lists and split them apart. For instance,zipdoes the following:

Prelude> zip "hello" [1,2,3,4,5]

[(’h’,1),(’e’,2),(’l’,3),(’l’,4),(’o’,5)]

Basically, it pairs the first elements of both lists and makes that the first element of the new list. It then pairs the second elements of both lists and makes that the second element, etc. What if the lists have unequal length? It simply stops when the shorter one stops. A reasonable definition forzipis:

zip [] _ = [] zip _ [] = []

zip (x:xs) (y:ys) = (x,y) : zip xs ys

Theunzipfunction does the opposite. It takes a zipped list and returns the two “original” lists:

Prelude> unzip [(’f’,1),(’o’,2),(’o’,3)] ("foo",[1,2,3])

There are a whole slew of zip andunzipfunctions, named zip3, unzip3,

zip4,unzip4and so on; the...3functions use triples instead of pairs; the...4

functions use 4-tuples, etc.

Finally, the function taketakes an integer n and a list and returns the first n elements off the list. Correspondingly,droptakes an integernand a list and returns the result of throwing away the firstnelements off the list. Neither of these functions produces an error; ifnis too large, they both will just return shorter lists.

7.8.2

List Comprehensions

There is some syntactic sugar for dealing with lists whose elements are members of the

Enumclass (see Section 7.6), such asIntorChar. If we want to create a list of all the elements from1to10, we can simply write:

Prelude> [1..10]

[1,2,3,4,5,6,7,8,9,10]

We can also introduce an amount to step by:

Prelude> [1,3..10] [1,3,5,7,9]

Prelude> [1,4..10] [1,4,7,10]

These expressions are short hand forenumFromToandenumFromThenTo, re- spectively. Of course, you don’t need to specify an upper bound. Try the following (but be ready to hit Control+C to stop the computation!):

7.8. MORE LISTS 95

Prelude> [1..]

[1,2,3,4,5,6,7,8,9,10,11,12{Interrupted!}

Probably yours printed a few thousand more elements than this. As we said before, Haskell is lazy. That means that a list of all numbers from 1 on is perfectly well formed and that’s exactly what this list is. Of course, if you attempt to print the list (which we’re implicitly doing by typing it in the interpreter), it won’t halt. But if we only evaluate an initial segment of this list, we’re fine:

Prelude> take 3 [1..] [1,2,3]

Prelude> take 3 (drop 5 [1..]) [6,7,8]

This comes in useful if, say, we want to assign an ID to each element in a list. Without laziness we’d have to write something like this:

assignID :: [a] -> [(a,Int)] assignID l = zip l [1..length l]

Which means that the list will be traversed twice. However, because of laziness, we can simply write:

assignID l = zip l [1..]

And we’ll get exactly what we want. We can see that this works:

Prelude> assignID "hello"

[(’h’,1),(’e’,2),(’l’,3),(’l’,4),(’o’,5)]

Finally, there is some useful syntactic sugar formapandfilter, based on stan- dard set-notation in mathematics. In math, we would write something like{f(x)|x∈ s∧p(x)}to mean the set of all values off when applied to elements ofswhich satisfy p. This is equivalent to the Haskell statementmap f (filter p s). However, we can also use more math-like notation and write[f x | x <- s, p x]. While in math the ordering of the statements on the side after the pipe is free, it is not so in Haskell. We could not have putp xbeforex <- sotherwise the compiler wouldn’t know yet whatxwas. We can use this to do simple string processing. Suppose we want to take a string, remove all the lower-case letters and convert the rest of the letters to upper case. We could do this in either of the following two equivalent ways:

Prelude> map toLower (filter isUpper "Hello World") "hw"

Prelude> [toLower x | x <- "Hello World", isUpper x] "hw"

These two are equivalent, and, depending on the exact functions you’re using, one might be more readable than the other. There’s more you can do here, though. Suppose you want to create a list of pairs, one for each point between (0,0) and (5,7) below the diagonal. Doing this manually with lists and maps would be cumbersome and possibly difficult to read. It couldn’t be easier than with list comprehensions:

Prelude> [(x,y) | x <- [1..5], y <- [x..7]]

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

If you reverse the order of thex <- andy <-clauses, the order in which the space is traversed will be reversed (of course, in that case,ycould no longer depend onxand you would need to makexdepend onybut this is trivial).

Documento similar