HaskellWiki

Haskell | Wiki community | Recent changes
Random page | Special pages

 

Not logged in
Log in | Help

How to work on lists

Categories: How to

Contents

1 How do I…?

Given any list xs, how do I...?

1.1 Basics

length xs
reverse xs

1.2 Finding / searching

xs !! n
(Related: head xs returns the first element of the list.)
(Related: last xs returns the last element of the list.)
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.)
take n xs
splitAt n xs
(Returns a tuple of two lists.)
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.)
Haskell has a function called filter which will do this for you. Beware though: it should really be named 'select' instead. For example, filter odd xs returns a list of odd numbers. That is, it deletes everything that is not odd.

1.5 Testing various conditions

null xs
any my_test xs
all my_test xs

1.6 Modifying the list or its elements

map my_function xs
Assuming you only want to apply function f to elements for which function p returns true, you can do this:
map (\x -> if p x then f x else x) xs
Find or write a function to convert foo into bar, and then apply it to the whole list using map.
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.)
You'll need to import Data.List first, but then you can just do sort xs.
my_element `elem` xs

1.7 Lists and IO

Turn a list of IO actions into one IO action that returns a list of results: sequence xs
Prelude> sequence [putStr "hello ", putStrLn "world"]
hello world
[(),()]
(Note: you might want to use sequence_ instead, like in the above case, if your actions only return ())
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
mapM f xs = sequence (map f xs) and similarly for sequence_.

2 Notes about speed

Haskell lists are ordinary single-linked lists. (Look up the term in any book on data structures.) This gives them certain speed properties which are well worth knowing.

2.1 Fast operations

The following operations are always 'fast':

2.2 Slower operations

Any function that does something with the Nth element or the first N elements generally gets slower as N increases. The following all slow down as n gets larger:

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:


3 GHC Data.List functions

The Data.List module has many functions for sorting, modifying and building lists.

Retrieved from "http://www.haskell.org/haskellwiki/How_to_work_on_lists"

This page has been accessed 3,363 times. This page was last modified 14:55, 1 August 2007. Recent content is available under a simple permissive license.