Personal tools

Cookbook/Lists and strings

From HaskellWiki

< Cookbook(Difference between revisions)
Jump to: navigation, search
 
Line 1: Line 1:
== 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.
   
=== Combining strings ===
+
= Combining strings =
   
 
{| class="wikitable"
 
{| class="wikitable"
Line 24: Line 22:
 
|}
 
|}
   
=== Accessing substrings ===
+
= Accessing substrings =
   
 
{| class="wikitable"
 
{| class="wikitable"
Line 67: Line 65:
 
|}
 
|}
   
=== Splitting strings ===
+
= Splitting strings =
   
   
Line 87: Line 85:
 
|}
 
|}
   
=== Multiline strings ===
+
= Multiline strings =
 
<haskell>
 
<haskell>
 
"foo\
 
"foo\
Line 93: Line 91:
 
</haskell>
 
</haskell>
   
=== Converting between characters and values ===
+
= Converting between characters and values =
   
 
{| class="wikitable"
 
{| class="wikitable"
Line 116: Line 114:
 
|}
 
|}
   
=== Reversing a string by words or characters ===
+
= Reversing a string by words or characters =
   
 
{| class="wikitable"
 
{| class="wikitable"
Line 143: Line 141:
 
|}
 
|}
   
=== Converting case ===
+
= Converting case =
   
 
{| class="wikitable"
 
{| class="wikitable"
Line 180: Line 178:
 
|}
 
|}
   
=== 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 10:16, 23 April 2009

Since strings are lists of characters, you can use any available list function.

Contents

1 Combining strings

Problem Solution Examples
combining two strings (++)
"foo" ++ "bar"                  --> "foobar"
combining many strings concat
concat ["foo", "bar", "baz"]    --> "foobarbaz"

2 Accessing substrings

Problem Solution Examples
accessing the first character head
head "foo bar baz"      --> 'f'
accessing the last character last
last "foo bar baz"      --> 'z'
accessing the character at a given index (!!)
"foo bar baz" !! 4      --> 'b'
accessing the first n characters take
take 3 "foo bar baz"    --> "foo"
accessing the last n characters TODO TODO
accessing the n characters starting from index m drop, take
take 4 $ drop 2 "foo bar baz"    --> "o ba"

3 Splitting strings

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")

4 Multiline strings

"foo\
\bar"               --> "foobar"

5 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'

6 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"

7 Converting case

Problem Solution Examples
converting a character to upper-case toUpper
import Char
toUpper 'a'            --> "A"
converting a string to upper-case toUpper, map
import Char
map toUpper "Foo Bar"  --> "FOO BAR"
converting a character to lower-case toLower
import Char
toLower 'A'            --> "a"
converting a string to lower-case toLower, map
import Char
map toLower "Foo Bar"  --> "foo bar"

8 Interpolation

TODO

9 Performance

For high performance requirements (where you would typically consider C), consider using Data.ByteString.

10 Unicode

TODO