|
8 | 8 | -- Because the Prelude module exports some functions from Data.List,
|
9 | 9 | -- we've already met some of its functions like map and filter.
|
10 | 10 |
|
| 11 | +-- SO CHECK BASIC LIST FUNCTIONS (WHICH ALSO PROVIDED IN DEFAULT |
| 12 | +-- PRELUDE MODULE) IN 02_starting_out/lists.hs |
| 13 | + |
11 | 14 | -- We don't have to import Data.List via a qualified import because it doesn't
|
12 | 15 | -- clash with any Prelude names. (Except for those that Prelude already steals from Data.List)
|
13 | 16 |
|
@@ -196,7 +199,7 @@ example_30 = notElem 3 [1,2,5]
|
196 | 199 | -- Maybe is an algebraic data type and its value can either be Just something or Nothing.
|
197 | 200 | example_32 = find (>4) [1,2,3,4,5,6]
|
198 | 201 | -- ==> Just 5
|
199 |
| -example_33 =find (>9) [1,2,3,4,5,6] |
| 202 | +example_33 = find (>9) [1,2,3,4,5,6] |
200 | 203 | -- ==> Nothing
|
201 | 204 | -- a value of the type Maybe can contain either no elements or one element.
|
202 | 205 |
|
@@ -260,5 +263,74 @@ example_46 = words "hey these are the words in this\nsentence"
|
260 | 263 | example_47 = unwords ["hey","there","mate"]
|
261 | 264 | -- ==> "hey there mate"
|
262 | 265 |
|
263 |
| - |
264 |
| - |
| 266 | +-- > nub |
| 267 | +-- It takes a list and weeds out the duplicate elements |
| 268 | +example_48 = nub [1,2,3,4,3,2,1,2,3,4,3,2,1] |
| 269 | +-- ==> [1,2,3,4] |
| 270 | + |
| 271 | +-- > delete |
| 272 | +-- It takes an element and a list and deletes the first occurence of that element in the list. |
| 273 | +example_49 = delete 'h' "hey there ghang!" |
| 274 | +-- ==> "ey there ghang!" |
| 275 | + |
| 276 | +-- > \\ |
| 277 | +-- This is the list difference function. It acts like a set difference, basically. |
| 278 | +-- For every element in the right-hand list, it removes a matching element in the left one. |
| 279 | +example_50 = [1,2,3,4,5] \\ [3,1,5] |
| 280 | +-- ==> [2,4] |
| 281 | +example_51 = [1,2,3,4,5,1] \\ [3,1,5] |
| 282 | +-- ==> [2,4,1] |
| 283 | +-- Doing [1,2,3,4,5] \\ [3,1,5] is like doing delete 3 . delete 1 . delete 5 $ [1,2,3,4,5] |
| 284 | + |
| 285 | +-- > union |
| 286 | +-- It pretty much goes over every element in the second list and appends it to |
| 287 | +-- the first one if it isn't already in yet. |
| 288 | +example_52 = "hey man" `union` "man what's up" |
| 289 | +-- ==> "hey manwt'sup" |
| 290 | +example_53 = [1..7] `union` [5..10] |
| 291 | +-- ==> [1,2,3,4,5,6,7,8,9,10] |
| 292 | + |
| 293 | +-- > intersect |
| 294 | +-- It returns only the elements that are found in both lists. |
| 295 | +example_54 = [1..7] `intersect` [5..10] |
| 296 | +-- ==> [5,6,7] |
| 297 | + |
| 298 | +-- > insert |
| 299 | +-- takes an element and a list of elements that can be sorted and inserts it into |
| 300 | +-- the last position where it's still less than or equal to the next element. |
| 301 | +example_55 = insert 4 [1,2,3,5,6,7] |
| 302 | +-- ==> [1,2,3,4,5,6,7] |
| 303 | + |
| 304 | +-- Since length, take, drop, splitAt, !! and replicate take an Int as one of their |
| 305 | +-- parameters (or return an Int); there is more generic ans usable versions of them. |
| 306 | +-- length > genericLength |
| 307 | +-- take > genericTake |
| 308 | +-- drop > genericDrop |
| 309 | +-- splitAt > genericSplitAt |
| 310 | +-- !! > genericIndex |
| 311 | +-- replicate > genericReplicate |
| 312 | + |
| 313 | +-- The nub, delete, union, intersect and group functions all have their more general |
| 314 | +-- counterparts which takes an equality function and then compare them by using that |
| 315 | +-- equality function. -- So group is equal to groupBy (==) |
| 316 | +-- nub > nubBy |
| 317 | +-- delete > deleteBy |
| 318 | +-- union > unionBy |
| 319 | +-- intersect > intersectBy |
| 320 | +-- group > groupBy |
| 321 | + |
| 322 | +-- The sort, insert, maximum and minimum also have their more general equivalents |
| 323 | +-- which takes a function that determine if one element is greater, smaller or equal |
| 324 | +-- to the other. This function must return one of of LT, EQ or GT. |
| 325 | +-- So sort is the equivalent of (sortBy compare). |
| 326 | +-- sort > sortBy |
| 327 | +-- insert > insertBy |
| 328 | +-- maximum > maximumBy |
| 329 | +-- minimum > minimumBy |
| 330 | +-- This will reverse the list; |
| 331 | +example_56 = sortBy (\x y -> GT) [1,2,3,4,5,6] |
| 332 | +-- ==> [6,5,4,3,2,1] |
| 333 | + |
| 334 | +-- NOTE: When you're dealing with By functions that take an equality function, |
| 335 | +-- you usually do (==) `on` something and when you're dealing with By functions |
| 336 | +-- that take an ordering function, you usually do compare `on` something. |
0 commit comments