Personal tools

Cookbook/Lists and strings

From HaskellWiki

< Cookbook(Difference between revisions)
Jump to: navigation, search
m
 
(22 intermediate revisions by 3 users not shown)
Line 1: Line 1:
Since strings are lists of characters, you can use any available list function.
+
== Lists ==
   
= Lists =
+
In Haskell, lists are what Arrays are in most other languages.
   
In Haskell, lists are what Arrays are in most other languages. Haskell has all of the general list manipulation functions, see also <hask>Data.List</hask>.
+
=== Creating simple lists ===
   
<haskell>
+
{| class="wikitable"
head [1,2,3] --> 1
+
|-
tail [1,2,3] --> [2,3]
+
! Problem
length [1,2,3] --> 3
+
! Solution
init [1,2,3] --> [1,2]
+
! Examples
last [1,2,3] --> 3
+
|-
  +
| creating a list with given elements
  +
| -
  +
|<haskell>
  +
3 : 12 : 42 : [] --> [3,12,42]
  +
'f' : 'o' : 'o' : [] --> "foo"
 
</haskell>
 
</haskell>
+
|-
Furthermore, Haskell supports some neat concepts.
+
| creating a list with stepsize 1
+
| -
== Infinite lists ==
+
|<haskell>
<haskell>
+
[1..10] --> [1,2,3,4,5,6,7,8,9,10]
Prelude> [1..]
+
['a'..'z'] --> "abcdefghijklmnopqrstuvwxyz"
 
</haskell>
 
</haskell>
+
|-
The list of all squares:
+
| creating a list with different stepsize
<haskell>
+
| -
square x = x*x
+
|<haskell>
squares = map square [1..]
+
[1,3..10] --> [1,3,5,7,9]
  +
['a','c'..'z'] --> "acegikmoqsuwy"
 
</haskell>
 
</haskell>
+
|-
But in the end, you probably don't want to use infinite lists, but make them finite. You can do this with <hask>take</hask>:
+
| creating an infinite constant list
+
| -
<haskell>
+
|<haskell>
Prelude> take 10 squares
+
[1,1..] --> [1,1,1,1,1,...
[1,4,9,16,25,36,49,64,81,100]
 
 
</haskell>
 
</haskell>
  +
|-
  +
| creating an infinite list with stepsize 1
  +
| -
  +
| <haskell>
  +
[1..] --> [1,2,3,4,5,...
  +
</haskell>
  +
|}
   
== 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 48: Line 55:
   
   
== Combining lists ==
+
=== Combining lists ===
   
 
{| class="wikitable"
 
{| class="wikitable"
Line 56: Line 63:
 
! Examples
 
! Examples
 
|-
 
|-
| combining two strings
+
| combining two lists
 
| [http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v%3A%2B%2B (++)]
 
| [http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v%3A%2B%2B (++)]
 
|<haskell>
 
|<haskell>
 
"foo" ++ "bar" --> "foobar"
 
"foo" ++ "bar" --> "foobar"
  +
[42,43] ++ [60,61] --> [42,43,60,61]
 
</haskell>
 
</haskell>
 
|-
 
|-
| combining many strings
+
| combining many lists
 
| [http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:concat concat]
 
| [http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:concat concat]
 
| <haskell>
 
| <haskell>
Line 69: Line 77:
 
|}
 
|}
   
== Accessing sublists ==
+
=== Accessing sublists ===
   
 
{| class="wikitable"
 
{| class="wikitable"
Line 77: Line 85:
 
! Examples
 
! Examples
 
|-
 
|-
| accessing the first character
+
| 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 83: Line 91:
 
</haskell>
 
</haskell>
 
|-
 
|-
| accessing the last character
+
| 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 89: Line 97:
 
</haskell>
 
</haskell>
 
|-
 
|-
| accessing the character at a given index
+
| 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 95: Line 103:
 
</haskell>
 
</haskell>
 
|-
 
|-
| accessing the first <code>n</code> characters
+
| 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 101: Line 109:
 
</haskell>
 
</haskell>
 
|-
 
|-
| accessing the last <code>n</code> characters
+
| 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 107: Line 115:
 
</haskell>
 
</haskell>
 
|-
 
|-
| accessing the <code>n</code> characters starting from index <code>m</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>
Line 114: Line 122:
 
|}
 
|}
   
== Splitting lists ==
+
=== Splitting lists ===
   
   
Line 128: Line 136:
 
</haskell>
 
</haskell>
 
|-
 
|-
| splitting a string into two parts
+
| splitting a list into two parts
 
| [http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v%3AsplitAt splitAt]
 
| [http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v%3AsplitAt splitAt]
 
| <haskell>splitAt 3 "foo bar baz" --> ("foo"," bar baz")
 
| <haskell>splitAt 3 "foo bar baz" --> ("foo"," bar baz")
Line 134: Line 142:
 
|}
 
|}
   
= Strings =
+
== Strings ==
  +
  +
Since strings are lists of characters, you can use any available list function.
   
== Multiline strings ==
+
=== Multiline strings ===
 
<haskell>
 
<haskell>
 
"foo\
 
"foo\
Line 142: Line 150:
 
</haskell>
 
</haskell>
   
== Converting between characters and values ==
+
=== Converting between characters and values ===
   
 
{| class="wikitable"
 
{| class="wikitable"
Line 153: Line 161:
 
| [http://haskell.org/ghc/docs/latest/html/libraries/base/Data-Char.html#v:ord ord]
 
| [http://haskell.org/ghc/docs/latest/html/libraries/base/Data-Char.html#v:ord ord]
 
|<haskell>
 
|<haskell>
import Char
+
import Data.Char
 
ord 'A' --> 65
 
ord 'A' --> 65
 
</haskell>
 
</haskell>
Line 160: Line 168:
 
| [http://haskell.org/ghc/docs/latest/html/libraries/base/Data-Char.html#v%3Achr chr]
 
| [http://haskell.org/ghc/docs/latest/html/libraries/base/Data-Char.html#v%3Achr chr]
 
| <haskell>
 
| <haskell>
import Char
+
import Data.Char
 
chr 99 --> 'c'
 
chr 99 --> 'c'
 
</haskell>
 
</haskell>
 
|}
 
|}
   
== Reversing a string by words or characters ==
+
=== Reversing a string by words or characters ===
   
 
{| class="wikitable"
 
{| class="wikitable"
Line 192: Line 200:
 
|}
 
|}
   
== Converting case ==
+
=== Converting case ===
   
 
{| class="wikitable"
 
{| class="wikitable"
Line 203: Line 211:
 
| [http://haskell.org/ghc/docs/latest/html/libraries/base/Data-Char.html#v%3AtoUpper toUpper]
 
| [http://haskell.org/ghc/docs/latest/html/libraries/base/Data-Char.html#v%3AtoUpper toUpper]
 
|<haskell>
 
|<haskell>
import Char
+
import Data.Char
 
toUpper 'a' --> "A"
 
toUpper 'a' --> "A"
 
</haskell>
 
</haskell>
Line 210: Line 218:
 
| [http://haskell.org/ghc/docs/latest/html/libraries/base/Data-Char.html#v%3AtoLower toLower]
 
| [http://haskell.org/ghc/docs/latest/html/libraries/base/Data-Char.html#v%3AtoLower toLower]
 
| <haskell>
 
| <haskell>
import Char
+
import Data.Char
 
toLower 'A' --> "a"
 
toLower 'A' --> "a"
 
</haskell>
 
</haskell>
Line 217: Line 225:
 
| [http://haskell.org/ghc/docs/latest/html/libraries/base/Data-Char.html#v%3AtoUpper toUpper], [http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:map map]
 
| [http://haskell.org/ghc/docs/latest/html/libraries/base/Data-Char.html#v%3AtoUpper toUpper], [http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:map map]
 
|<haskell>
 
|<haskell>
import Char
+
import Data.Char
 
map toUpper "Foo Bar" --> "FOO BAR"
 
map toUpper "Foo Bar" --> "FOO BAR"
 
</haskell>
 
</haskell>
Line 224: Line 232:
 
| [http://haskell.org/ghc/docs/latest/html/libraries/base/Data-Char.html#v%3AtoLower toLower], [http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:map map]
 
| [http://haskell.org/ghc/docs/latest/html/libraries/base/Data-Char.html#v%3AtoLower toLower], [http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:map map]
 
| <haskell>
 
| <haskell>
import Char
+
import Data.Char
 
map toLower "Foo Bar" --> "foo bar"
 
map toLower "Foo Bar" --> "foo bar"
 
</haskell>
 
</haskell>
 
|}
 
|}
   
== 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

Latest revision as of 02:44, 7 August 2013

Contents

[edit] 1 Lists

In Haskell, lists are what Arrays are in most other languages.

[edit] 1.1 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,1,...
creating an infinite list with stepsize 1 -
[1..]                 --> [1,2,3,4,5,...

[edit] 1.2 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 ]


[edit] 1.3 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"

[edit] 1.4 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"

[edit] 1.5 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")

[edit] 2 Strings

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

[edit] 2.1 Multiline strings

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

[edit] 2.2 Converting between characters and values

Problem Solution Examples
converting a character to a numeric value ord
import Data.Char
ord 'A'    --> 65
converting a numeric value to a character chr
import Data.Char
chr 99     --> 'c'

[edit] 2.3 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"

[edit] 2.4 Converting case

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

[edit] 2.5 Interpolation

TODO

[edit] 2.6 Performance

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

[edit] 2.7 Unicode

TODO