You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
270
270
We want to know when the stock value first exceeded one thousand dollars!
271
271
272
272
```{.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")
276
276
```
277
277
278
278
`span`{.label .function} is kind of like `takeWhile`, only it returns a pair of lists.
@@ -411,13 +411,13 @@ Its result is `Maybe a`.
411
411
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.
412
412
413
413
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)`.
415
415
Remember that `head` is not really safe.
416
416
What would happen if our stock never went over $1000?
417
417
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.
419
419
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")`.
421
421
422
422
`elemIndex`{.label .function} is kind of like `elem`, only it doesn't return a boolean value.
423
423
It maybe returns the index of the element we're looking for.
0 commit comments