How To Work On Lists - HaskellWiki
How To Work On Lists - HaskellWiki
From HaskellWiki
Contents
1 How do I?
1.1 Basics
1.2 Finding / searching
1.3 Adding
1.4 Deleting
1.5 Testing various conditions
1.6 Modifying the list or its elements
1.7 Lists and IO
2 Notes about speed
2.1 Fast operations
2.2 Slower operations
3 GHC Data.List functions
1 How do I?
Given any list
xs
, how do I...?
1.1 Basics
Get the size of the list.
length xs
reverse xs
xs !! n
(Related:
head xs
returns the first element of the list.)
(Related:
last xs
returns the last element of the list.)
Get a list of all elements that match some condition.
filter my_test xs
(Returns everything that passes the test.)
minimum xs
maximum xs
(Works not just for numbers but anything that is a member of the
Ord
class. In particular, that includes characters and strings.)
1.3 Adding
new_element : xs
xs ++ [new_element]
Generally, you will have to split the list into two smaller lists, put the new element to in the middle,
and then join everything back together. For example:
let (ys,zs) = splitAt n xs in ys ++ [new_element] ++ zs
list1 ++ list2
1.4 Deleting
drop n xs
(Related:
tail xs
removes just one element.)
(Related:
init xs
removes just the last element.)
Make a new list containing just the first N elements from an existing list.
take n xs
Split a list into two smaller lists (at the Nth position).
splitAt n xs
This is tricky. AFAIK, there is no built-in function that does this. You have to split the list in two,
remove the element from one list, and then join them back together, like this:
let (ys,zs) = splitAt n xs in ys ++ (tail zs)
(Related:
tail xs
removes the first element.)
(Related:
init xs
removes the last element. Slow if the list is big.)
null xs
any my_test xs
all my_test xs
map my_function xs
Find or write a function to convert foo into bar, and then apply it to the whole list using
map
.
Number the elements of a list (so I can process each one differently according to its position).
zip xs [0..]
(For example,
zip ['a','b','c'] [0..]
gives
[('a',0),('b',1),('c',2)]
.)
It's not in the book, but it's easy when you know how:
map ($ my_element) xs
sum xs
(Related:
product xs
will multiply all the elements together instead of adding them.)
Sort a list.
my_element `elem` xs
Turn a list of IO actions into one IO action that returns a list of results:
sequence xs
You could map the IO function over your list (resulting in a list of actions) and then perform them
using the trick above. But it's much simpler to do this:
mapM my_action xs
or
mapM_ my_action xs
where
xs !! n
take n xs
drop n xs
splitAt n xs
Any function which needs to process the entire list obviously gets slower as the list gets bigger. The
following all slow down as the list
xs
gets larger:
length xs
list1 ++ list2
(speed depends only on the size of
list1
)
last xs
map my_fn xs
filter my_test xs
zip my_fn list1 list2
(speed depends on the smallest of the two lists - as does the size of the result list!)
x `elem` xs
sum xs
minimum xs
maximum xs
3 GHC
Data.List
functions
The
Data.List
module has many functions for sorting, modifying and building lists.
Retrieved from "https://wiki.haskell.org/index.php?title=How_to_work_on_lists&oldid=14773"
Category:
How to