Skip to content

Commit 742b6c1

Browse files
committed
Complete 07/Data.List and add 07/Data.Function
1 parent 663a935 commit 742b6c1

File tree

3 files changed

+110
-4
lines changed

3 files changed

+110
-4
lines changed

07_modules/module-data-function.hs

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
-- # http://learnyouahaskell.com/modules#data-list
2+
3+
-- LOAD THIS FILE WITH ":l module-data-function" within repl (ghci)
4+
-- and reload with ":r"
5+
6+
import Data.List
7+
import Data.Function -- this is our boy
8+
9+
-- > on
10+
11+
-- signature;
12+
-- on :: (b -> b -> c) -> (a -> b) -> a -> a -> c
13+
-- f `on` g = \x y -> f (g x) (g y)
14+
15+
-- example;
16+
-- (==) `on` (> 0)
17+
-- equals to
18+
-- \x y -> (x > 0) == (y > 0)
19+
groupByAdjacentSign = groupBy ((==) `on` (> 0))
20+
-- => groupByAdjacentSign [1,-2,-3,4,5,-6,7,8,-9,-10]
21+
-- ==> [[1],[-2,-3],[4,5],[-6],[7,8],[-9,-10]]
22+
23+
-- example 2;
24+
-- compare `on` length
25+
-- equals to
26+
-- \x y -> length x `compare` length y
27+
sortListsByLength :: [[a]] -> [[a]]
28+
sortListsByLength = sortBy (compare `on` length)
29+
-- => sortListsByLength [[1,2,3,4], [1,2], [1], [1,2,3]]
30+
31+
-- NOTE: When you're dealing with By functions that take an equality function,
32+
-- you usually do (==) `on` something and when you're dealing with By functions
33+
-- that take an ordering function, you usually do compare `on` something.

07_modules/module-data-list.hs

+75-3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
-- Because the Prelude module exports some functions from Data.List,
99
-- we've already met some of its functions like map and filter.
1010

11+
-- SO CHECK BASIC LIST FUNCTIONS (WHICH ALSO PROVIDED IN DEFAULT
12+
-- PRELUDE MODULE) IN 02_starting_out/lists.hs
13+
1114
-- We don't have to import Data.List via a qualified import because it doesn't
1215
-- clash with any Prelude names. (Except for those that Prelude already steals from Data.List)
1316

@@ -196,7 +199,7 @@ example_30 = notElem 3 [1,2,5]
196199
-- Maybe is an algebraic data type and its value can either be Just something or Nothing.
197200
example_32 = find (>4) [1,2,3,4,5,6]
198201
-- ==> Just 5
199-
example_33 =find (>9) [1,2,3,4,5,6]
202+
example_33 = find (>9) [1,2,3,4,5,6]
200203
-- ==> Nothing
201204
-- a value of the type Maybe can contain either no elements or one element.
202205

@@ -260,5 +263,74 @@ example_46 = words "hey these are the words in this\nsentence"
260263
example_47 = unwords ["hey","there","mate"]
261264
-- ==> "hey there mate"
262265

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.

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -39,5 +39,6 @@
3939
- [function composition [.]](/06_higher-order-functions/function-composition.hs)
4040
7. Modules ([Book Page](http://learnyouahaskell.com/modules))
4141
- [loading](/07_modules/loading.hs)
42-
- [WIP] [Data.List](/07_modules/module-data-list.hs)
42+
- [Data.List](/07_modules/module-data-list.hs)
43+
- [Data.Function](/07_modules/module-data-function.hs)
4344
- ...

0 commit comments

Comments
 (0)