Haskell – Data Type

Haskell

Preface

In this section we will learn the data types available in Haskell.

Declare the type

First we must know how to declare the data type for a variable. The declaration syntax is:

<variable name> :: <type_name>

For example:

main :: IO()
a :: Int
b :: Bool
main = return()

The basic styles

Haskell has several styles as well as in other languages.

Bool

Bring true / false (True / False). For example:

main :: IO()

a :: Bool
a = False

b :: Bool 
b = True

main = return()

Int

This is the data type that stores the signed integer, the word value domain [-229, 229-1].

main :: IO()

i :: Int
i = 2020

j :: Int
j = -99999999
main = return()

Integer

This is also a data type for storing integers, but can store very large numbers, and even use up memory in the device to save.

main :: IO()
bi :: Integer
bi = 94919521698492921921919551295194198981621984623...
main = return()

Char

This is the character storage data type. For example:

main :: IO()
c :: Char
c="a"
main = return()

Note that we can only wrap characters in single quotes ', not quotation marks ".

String

Also stores characters, but is a string of many characters.

main :: IO()
c :: String
c = "Phocode - Haskell"
main = return()

Other than CharWe have to wrap the strings String in double quotes ".

Float / Double

This is a data type that stores decimal numbers, the difference is that Float only stores 32 bits and Double is 64 bits. (However, it is said that in modern versions of Haskell, both Float and Double use 64 bits for storage.)

main :: IO()

f :: Float
f = 3.1415

d :: Double
d = 9.9999999999

main = return()

List

This is the type of list. Like arrays in C ++ or Java, lists in Haskell only store elements of the same type. To declare a list, we use syntax [tên_kiểu], For example:

main :: IO()
list :: [Int]  -- List số nguyên
list = [1, 2, 3, 4, 5]
main = return()

Tuple

If you’ve programmed in Python before, then you will be familiar with this tuple, we can treat it as simple as a list but can store many elements with many different data types. To declare tuple, we use syntax (tên_kiểu_1, tên_kiểu_2,.... tên_kiểu_n)Unlike list, we declare how many types in tuple we must assign exactly how many elements, for example:

main :: IO()
list :: (Int, Int, String)
list = (16, 5, "phocode.com")
-- list = ('a', 5, "Hello") --- Wrong because the first element is of the wrong type
-- list = (1, 2, "Hello", 4, 5) -- Error because the number of elements is 5 while declaring only 3
main = return()

Haskell can assign dynamic types

For more flexibility in variable declaration, we may not need to use a data type declaration statement (with 2 signs ::), but just assign the data then Haskell will automatically assign the data type to that variable for us.

main :: IO()
num = 3 -- Int
char="a" -- Char
str = "Phocode" -- String
fl = 3.14 -- Float
main = return()

Variables in Haskell are immutable

That is, the value of that variable cannot be changed.

main :: IO()
num = 3
num = 4 -- Error Multiple declaration of 'num'
main = return()

Hope this helps!

Source link

Leave a Reply

Your email address will not be published. Required fields are marked *