Difference between revisions of "Cookbook/Lists and strings"
< Cookbook
Jump to navigation
Jump to search
(smaller headlines) |
|||
Line 1: | Line 1: | ||
− | = Lists = |
+ | == Lists == |
In Haskell, lists are what Arrays are in most other languages. |
In Haskell, lists are what Arrays are in most other languages. |
||
− | == Creating simple lists == |
+ | === Creating simple lists === |
{| class="wikitable" |
{| class="wikitable" |
||
Line 45: | Line 45: | ||
|} |
|} |
||
− | == List comprehensions == |
+ | === List comprehensions === |
The list of all squares can also be written in a more comprehensive way, using list comprehensions: |
The list of all squares can also be written in a more comprehensive way, using list comprehensions: |
||
Line 61: | Line 61: | ||
− | == Combining lists == |
+ | === Combining lists === |
{| class="wikitable" |
{| class="wikitable" |
||
Line 83: | Line 83: | ||
|} |
|} |
||
− | == Accessing sublists == |
+ | === Accessing sublists === |
{| class="wikitable" |
{| class="wikitable" |
||
Line 128: | Line 128: | ||
|} |
|} |
||
− | == Splitting lists == |
+ | === Splitting lists === |
Line 148: | Line 148: | ||
|} |
|} |
||
− | = Strings = |
+ | == Strings == |
Since strings are lists of characters, you can use any available list function. |
Since strings are lists of characters, you can use any available list function. |
||
− | == Multiline strings == |
+ | === Multiline strings === |
<haskell> |
<haskell> |
||
"foo\ |
"foo\ |
||
Line 158: | Line 158: | ||
</haskell> |
</haskell> |
||
− | == Converting between characters and values == |
+ | === Converting between characters and values === |
{| class="wikitable" |
{| class="wikitable" |
||
Line 181: | Line 181: | ||
|} |
|} |
||
− | == Reversing a string by words or characters == |
+ | === Reversing a string by words or characters === |
{| class="wikitable" |
{| class="wikitable" |
||
Line 208: | Line 208: | ||
|} |
|} |
||
− | == Converting case == |
+ | === Converting case === |
{| class="wikitable" |
{| class="wikitable" |
||
Line 245: | Line 245: | ||
|} |
|} |
||
− | == Interpolation == |
+ | === Interpolation === |
TODO |
TODO |
||
− | == Performance == |
+ | === Performance === |
For high performance requirements (where you would typically consider |
For high performance requirements (where you would typically consider |
||
C), consider using [http://hackage.haskell.org/packages/archive/bytestring/latest/doc/html/Data-ByteString.html Data.ByteString]. |
C), consider using [http://hackage.haskell.org/packages/archive/bytestring/latest/doc/html/Data-ByteString.html Data.ByteString]. |
||
− | == Unicode == |
+ | === Unicode === |
TODO |
TODO |
Revision as of 11:30, 9 July 2009
Lists
In Haskell, lists are what Arrays are in most other languages.
Creating simple lists
Problem | Solution | Examples |
---|---|---|
creating a list with given elements | - | 3 : 12 : 42 : [] --> [3,12,42]
'f' : 'o' : 'o' : [] --> "foo"
|
creating a list with stepsize 1 | - | [1..10] --> [1,2,3,4,5,6,7,8,9,10]
['a'..'z'] --> "abcdefghijklmnopqrstuvwxyz"
|
creating a list with different stepsize | - | [1,3..10] --> [1,3,5,7,9]
['a','c'..'z'] --> "acegikmoqsuwy"
|
creating an infinite constant list | - | [1..] --> [1,1,1,1,1,...
|
creating an infinite list with stepsize 1 | - | [1,2..] --> [1,2,3,4,5,...
|
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"
[42,43] ++ [60,61] --> [42,43,60,61]
|
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 list 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