Saturday, 30 April 2016

Haskell: Char datatype

Char is used to store single Unicode character.


variable.hs
-- Unicode characters
char1, char2, char3 :: Char
char1 = 'a'
char2 = 'Ø'
char3 = 'ダ'

*Main> :load variable.hs
[1 of 1] Compiling Main             ( variable.hs, interpreted )
Ok, modules loaded: Main.
*Main> 
*Main> char1
'a'
*Main> 
*Main> char2
'\216'
*Main> 
*Main> char3
'\12480'

How to represent a character?
There are two ways to specify a charcater in Haskell.
         a. Specifying the character in signle quotes.
                  var1 = 'a'

         b. Specify the charcater by using the unicode value.
                  var2 = '\97'   -- in decimal

                  var3 = '\x61' -- in Hexa decimal
Prelude> let var1 = 'a'
Prelude> let var2 = '\97'
Prelude> let var3 = '\x61'
Prelude> 
Prelude> var1
'a'
Prelude> var2
'a'
Prelude> var3
'a'


You can check the type of a variable using :t command.

Prelude> :t var1
var1 :: Char
Prelude> 
Prelude> :t var3
var3 :: Char







Previous                                                 Next                                                 Home

No comments:

Post a Comment