Data.List

We will now introduce several very useful functions in the Data.List module. We’ve already met some of its functions (like map and filter) because the Prelude module imports some functions from Data.List for convenience.

You don’t have to import Data.List via a qualified import because it doesn’t clash with any Prelude names except for those that Prelude already takes from Data.List. Let’s take a look at some of the functions that we haven’t seen before

nub :

nub tells us how many unique elements a list has, that is, it takes a list and weeds out duplicate elements.


ghci 4> nub [1, 2, 3, 4, 3, 2, 1, 2, 3, 4, 3, 2, 1]
[1, 2, 3, 4]

intersperse :

intersperse takes an element and a list and then puts that element in between each pair of elements in the list.


ghci 9> intersperse ’.’ "MONKEY"
"M.O.N.K.E.Y"


ghci 10> intersperse 0 [1, 2, 3, 4, 5, 6]
[1, 0, 2, 0, 3, 0, 4, 0, 5, 0, 6]

intercalate :

intercalate takes a list of lists and a list. It then inserts that list in between all those lists and then flattens the result.


ghci 11> intercalate " " ["hey", "there", "guys"]
"hey there guys"


ghci 12> intercalate [0, 0, 0] [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
[1, 2, 3, 0, 0, 0, 4, 5, 6, 0, 0, 0, 7, 8, 9]

concat and concatMap :

concat flattens a list of lists.


ghci 13> concat ["foo", "bar", "car"]
"foobarcar"


ghci 14> concat [[3, 4, 5], [2, 3, 4], [2, 1, 1]]
[3, 4, 5, 2, 3, 4, 2, 1, 1]

But it will just remove one level of nesting. So if we want to completely flatten [[[2, 3], [3, 4, 5], [2]], [[2, 3], [3, 4]]], which is a list of lists of lists, we have to concatenate it twice.


ghci 15> concat [[[2, 3], [3, 4, 5], [2]], [[2, 3], [3, 4]]]
[[2, 3], [3, 4, 5], [2], [2, 3], [3, 4]]

Doing concatMap is the same as:

  • first mapping a function over a list,
  • then concatenating the list with concat.

ghci 17> replicate 4 1
[1, 1, 1, 1]


ghci 18> map (replicate 4) [1 . . 3]
[[1, 1, 1, 1], [2, 2, 2, 2], [3, 3, 3, 3]]


ghci 19> concatMap (replicate 4) [1 . . 3]
[1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3]

and and or :

and takes a list of boolean values and returns True only if all the values in the list are True.


ghci 20> and $ map (>4) [5, 6, 7, 8]
True

ghci 21> and $ map (≡ 4) [4, 4, 4, 3, 4]
False

or is like and except it returns True if any of the boolean values in a list is True.


ghci 22> or $ map (≡ 4) [2, 3, 4, 5, 6, 1]
True

ghci 23> or $ map (>4) [1, 2, 3]
False

any and all :

any and all take a predicate and then check if any or all the elements in a list satisfy the predicate, respectively

We usually use these two functions instead of

  • mapping over a list first,
  • then doing and or or.

ghci 24> any (≡ 4) [2, 3, 5, 6, 1, 4]
True


ghci 25> all (>4) [6, 9, 10]
True

iterate :

iterate takes a function and a starting value. It applies the function to the starting value, then it applies that function to the result, then it applies the function to that result again etc. It returns all the results in the form of an infinite list.


ghci 28> take 10 $ iterate (*2) 1
[1, 2, 4, 8, 16, 32, 64, 128, 256, 512]


ghci 29> take 3 $ iterate (++"hihi") "haha"
["haha", "hahahihi", "hahahihihihi"]

splitAt :

splitAt takes a number and a list. It then splits the list at that many elements, returning the resulting two lists in a tuple.


ghci 30> splitAt 3 "heyman"
("hey", "man")


ghci 31> splitAt 100 "heyman"
("heyman", "")


ghci 32> splitAt (−3) "heyman"
("", "heyman")


ghci 33> let (a, b) = splitAt 3 "foobar" in b ++ a
"barfoo"

takeWhile and dropWhile :

takeWhile (which we’ve alread met) takes elements from a list while the predicate holds and then when an element is encountered that doesn’t satisfy the predicate, it stops.


ghci 34> takeWhile (>3) [6, 5, 4, 3, 2, 1, 2, 3, 4, 5, 4, 3, 2, 1]
[6, 5, 4]

dropWhile is similar to takeWhile, only it drops all the elements while the predicate is true.


ghci 38> dropWhile (<3) [1, 2, 2, 2, 3, 4, 5, 4, 3, 2, 1]
[3, 4, 5, 4, 3, 2, 1]

sort :

sort simply sorts a list. The type of list elements has to be in the Ord typeclass so that the elements can be sorted.


ghci 43> sort [8, 5, 3, 2, 1, 6, 4, 2]
[1, 2, 2, 3, 4, 5, 6, 8]


ghci 44> sort "This will be sorted soon"
" Tbdeehiillnooorssstw"

group :

group takes a list and groups adjacent elements into sublists if they are equal.


ghci 45> group [1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 2, 2, 2, 5, 6, 7]
[[1, 1, 1, 1], [2, 2, 2, 2], [3, 3], [2, 2, 2], [5], [6], [7]]

inits and tails :

inits and tails are like init and tail, only they recursively apply init / tail to a list until there’s nothing left.


ghci 47> inits "w00t"
["", "w", "w0", "w00", "w00t"]


ghci 48> tails "w00t"
["w00t", "00t", "0t", "t", ""]


ghci 49> let w = "w00t" in zip (inits w) (tails w)
[ ("", "w00t"),("w", "00t"),("w0", "0t"),("w00", "t"),("w00t", "")]

partition :

partition takes a predicate and a list and returns a pair of lists:

  • the first list in the result contains all the elements that satisfy the predicate;
  • the second contains all the ones that don’t.

ghci 60> partition (>3) [1, 3, 5, 6, 3, 2, 1, 0, 3, 7]
([5, 6, 7], [1, 3, 3, 2, 1, 0, 3])

lines and unlines :

lines is a useful function when dealing with files or input from somewhere. It takes a string and returns every line of that string in a separate list.


ghci 84> lines "first line\nsecond line\nthird line"
["first line", "second line", "third line"]

'\n' is the character for a unix newline. Backslashes have special meaning in Haskell strings and characters.

unlines is the inverse function of lines. It takes a list of strings and joins them together using a '\n'


ghci 85> unlines ["first line", "second line", "third line"]
"first line\nsecond line\nthird line\n"

words and unwords :

words and unwords are for splitting a line of text into words or joining a list of words into a text.


xxxx :


ghci 86> words "hey these are the words in this sentence"
["hey", "these", "are", "the", "words", "in", "this", "sentence"]


ghci 87> words "hey these are the words in this\nsentence"
["hey", "these", "are", "the", "words", "in", "this", "sentence"]


ghci 88> unwords ["hey", "there", "mate"]
"hey there mate"

delete :


ghci 89> delete ’h’ "hey there ghang!"
"ey there ghang!"

insert :

insert takes an element and a list of elements that can be sorted and inserts it into a specific position in the list.

The position is determined as follows: insert starts at the beginning of the list, keeps going until it finds an element that’s equal to or greater than the element that we’re inserting, and it does the insertion right before that element.


ghci 101> insert 4 [3, 5, 1, 2, 8, 2]
[3, 4, 5, 1, 2, 8, 2]


ghci 102> insert 4 [1, 3, 4, 4, 1]
[1, 3, 4, 4, 4, 1]

The 4 is inserted right after the 3 and before the 5 in the first example and in between the 3 and 4 in the second example.

If we use insert to insert into a sorted list, the resulting list will still be sorted.


ghci 103> sort [3, 5, 1, 2, 8, 2]
[1, 2, 2, 3, 5, 8]


ghci 104> insert 4 $ sort [3, 5, 1, 2, 8, 2]
[1, 2, 2, 3, 4, 5, 8]

About Author

Myself KarthiQ, I am the author of this blog, I know ways to write a good article but some how I donot have the skills to make it to reach people, would you like help me to reach more people By sharing this Article in the social media.

Share this Article Facebook
Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions

Recent Addition

new tutorial Protractor Online Training : I am starting online training course for Protractor with Typescript from 15th November 2018.

You can attend first 3 classes for free, the total course fee is INR 10,000

The course time would be 8.00 PM(IST) for the first three classes

The course time would be 8.00 PM(IST) for the first three classes

If you are interested to learn then you can join the course by sending email to chercher.tech@gmail.com

or Register below


Subscribe
 
Join My Facebook Group
Join Group