Saturday 30 April 2016

Haskell: Strings are lists internally


In Haskell String are implements as list of characters internally.
Prelude> let hello = ['h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd']
Prelude> hello
"hello world"
Prelude> 
Prelude> :t hello
hello :: [Char]
Prelude> 
Prelude> 'h':'e':'y':' ':hello
"hey hello world"
Prelude> 


You can define string using square brackets (or) by using double quotes "".
Prelude> let str1 = "Hello"
Prelude> let str2 = ['H', 'e', 'l', 'l', 'o']
Prelude> 
Prelude> str1
"Hello"
Prelude> str2
"Hello"
Prelude> 
Prelude> :t str1
str1 :: [Char]
Prelude> :t str2
str2 :: [Char]
Prelude> 
Prelude> str1 == str2
True
Prelude> [1, 2, 3] == [1, 2]
False


Previous                                                 Next                                                 Home

No comments:

Post a Comment