Difference between revisions of "Cookbook/Lists and strings"
< Cookbook
Jump to navigation
Jump to search
Line 76: | Line 76: | ||
! Examples |
! Examples |
||
|- |
|- |
||
− | | accessing the first |
+ | | accessing the first element |
| [http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:head head] |
| [http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:head head] |
||
|<haskell> |
|<haskell> |
||
Line 82: | Line 82: | ||
</haskell> |
</haskell> |
||
|- |
|- |
||
− | | accessing the last |
+ | | accessing the last element |
| [http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v%3Alast last] |
| [http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v%3Alast last] |
||
|<haskell> |
|<haskell> |
||
Line 88: | Line 88: | ||
</haskell> |
</haskell> |
||
|- |
|- |
||
− | | accessing the |
+ | | accessing the element at a given index |
| [http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v%3A!! (!!)] |
| [http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v%3A!! (!!)] |
||
|<haskell> |
|<haskell> |
||
Line 94: | Line 94: | ||
</haskell> |
</haskell> |
||
|- |
|- |
||
− | | accessing the first <code>n</code> |
+ | | accessing the first <code>n</code> elements |
| [http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:take take] |
| [http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:take take] |
||
| <haskell> |
| <haskell> |
||
Line 100: | Line 100: | ||
</haskell> |
</haskell> |
||
|- |
|- |
||
− | | accessing the last <code>n</code> |
+ | | accessing the last <code>n</code> elements |
| [http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:reverse reverse ], [http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:take take] |
| [http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:reverse reverse ], [http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:take take] |
||
| <haskell> |
| <haskell> |
||
Line 106: | Line 106: | ||
</haskell> |
</haskell> |
||
|- |
|- |
||
− | | accessing the <code>n</code> |
+ | | accessing the <code>n</code> elements starting from index <code>m</code> |
| [http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:drop drop], [http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:take take] |
| [http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:drop drop], [http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:take take] |
||
| <haskell> |
| <haskell> |
Revision as of 13:45, 23 April 2009
Lists
In Haskell, lists are what Arrays are in most other languages. Haskell has all of the general list manipulation functions, see also Data.List
.
head [1,2,3] --> 1
tail [1,2,3] --> [2,3]
length [1,2,3] --> 3
init [1,2,3] --> [1,2]
last [1,2,3] --> 3
Furthermore, Haskell supports some neat concepts.
Infinite lists
Prelude> [1..]
The list of all squares:
square x = x*x
squares = map square [1..]
But in the end, you probably don't want to use infinite lists, but make them finite. You can do this with take
:
Prelude> take 10 squares
[1,4,9,16,25,36,49,64,81,100]
List comprehensions
The list of all squares can also be written in a more comprehensive way, using list comprehensions:
squares = [x*x | x <- [1..]]
List comprehensions allow for constraints as well:
-- multiples of 3 or 5
mults = [ x | x <- [1..], mod x 3 == 0 || mod x 5 == 0 ]
Combining lists
Problem | Solution | Examples |
---|---|---|
combining two lists | (++) | "foo" ++ "bar" --> "foobar"
|
combining many lists | concat | concat ["foo", "bar", "baz"] --> "foobarbaz"
|
Accessing sublists
Problem | Solution | Examples |
---|---|---|
accessing the first element | head | head "foo bar baz" --> 'f'
|
accessing the last element | last | last "foo bar baz" --> 'z'
|
accessing the element at a given index | (!!) | "foo bar baz" !! 4 --> 'b'
|
accessing the first n elements
|
take | take 3 "foo bar baz" --> "foo"
|
accessing the last n elements
|
reverse , take | reverse . take 3 . reverse $ "foobar" --> "bar"
|
accessing the n elements starting from index m
|
drop, take | take 4 $ drop 2 "foo bar baz" --> "o ba"
|
Splitting lists
Problem | Solution | Examples |
---|---|---|
splitting a string into a list of words | words | words "foo bar\t baz\n" --> ["foo","bar","baz"]
|
splitting a string into two parts | splitAt | splitAt 3 "foo bar baz" --> ("foo"," bar baz")
|
Strings
Since strings are lists of characters, you can use any available list function.
Multiline strings
"foo\
\bar" --> "foobar"
Converting between characters and values
Problem | Solution | Examples |
---|---|---|
converting a character to a numeric value | ord | import Char
ord 'A' --> 65
|
converting a numeric value to a character | chr | import Char
chr 99 --> 'c'
|
Reversing a string by words or characters
Problem | Solution | Examples |
---|---|---|
reversing a string by characters | reverse | reverse "foo bar baz" --> "zab rab oof"
|
reversing a string by words | words, reverse, unwords | unwords $ reverse $ words "foo bar baz" --> "baz bar foo"
|
reversing a string by characters by words | words, reverse, map, unwords | unwords $ map reverse $ words "foo bar baz" --> "oof rab zab"
|
Converting case
Problem | Solution | Examples |
---|---|---|
converting a character to upper-case | toUpper | import Char
toUpper 'a' --> "A"
|
converting a character to lower-case | toLower | import Char
toLower 'A' --> "a"
|
converting a string to upper-case | toUpper, map | import Char
map toUpper "Foo Bar" --> "FOO BAR"
|
converting a string to lower-case | toLower, map | import Char
map toLower "Foo Bar" --> "foo bar"
|
Interpolation
TODO
Performance
For high performance requirements (where you would typically consider C), consider using Data.ByteString.
Unicode
TODO