Skip to content

Commit e06404e

Browse files
committed
simplify dropWhile example
1 parent 4768afc commit e06404e

1 file changed

Lines changed: 7 additions & 7 deletions

File tree

source_md/modules.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -266,13 +266,13 @@ ghci> dropWhile (<3) [1,2,2,2,3,4,5,4,3,2,1]
266266
```
267267

268268
We're given a list that represents the value of a stock by date.
269-
The list is made of tuples whose first component is the stock value, the second is the year, the third is the month and the fourth is the date.
269+
The list is made of tuples whose first component is the stock value, the second is the date.
270270
We want to know when the stock value first exceeded one thousand dollars!
271271

272272
```{.haskell:ghci}
273-
ghci> let stock = [(994.4,2008,9,1),(995.2,2008,9,2),(999.2,2008,9,3),(1001.4,2008,9,4),(998.3,2008,9,5)]
274-
ghci> head (dropWhile (\(val,y,m,d) -> val < 1000) stock)
275-
(1001.4,2008,9,4)
273+
ghci> let stock = [(994.4,"2008-9-1"),(995.2,"2008-9-2"),(999.2,"2008-9-3"),(1001.4,"2008-9-4"),(998.3,"2008-9-5")]
274+
ghci> head $ dropWhile (\(val,_) -> val < 1000) stock
275+
(1001.4,"2008-9-4")
276276
```
277277

278278
`span`{.label .function} is kind of like `takeWhile`, only it returns a pair of lists.
@@ -411,13 +411,13 @@ Its result is `Maybe a`.
411411
That's kind of like having the type of `[a]`, only a value of the type `Maybe a` can contain either no elements or one element, whereas a list can contain no elements, one element, or several elements.
412412

413413
Remember when we were searching for the first time our stock went over $1000.
414-
We did `head (dropWhile (\(val,y,m,d) -> val < 1000) stock)`.
414+
We did `head (dropWhile (\(val,_) -> val < 1000) stock)`.
415415
Remember that `head` is not really safe.
416416
What would happen if our stock never went over $1000?
417417
Our application of `dropWhile` would return an empty list and getting the head of an empty list would result in an error.
418-
However, if we rewrote that as `find (\(val,y,m,d) -> val > 1000) stock`, we'd be much safer.
418+
However, if we rewrote that as `find (\(val,_) -> val > 1000) stock`, we'd be much safer.
419419
If our stock never went over $1000 (so if no element satisfied the predicate), we'd get back a `Nothing`.
420-
But if there was a valid answer in that list, we'd get, say, `Just (1001.4,2008,9,4)`.
420+
But if there was a valid answer in that list, we'd get, say, `Just (1001.4,"2008-9-4")`.
421421

422422
`elemIndex`{.label .function} is kind of like `elem`, only it doesn't return a boolean value.
423423
It maybe returns the index of the element we're looking for.

0 commit comments

Comments
 (0)