English 中文(简体)
Elixir - Data Types
  • 时间:2025-03-12

Epxir - Data Types


Previous Page Next Page  

For using any language, you need to understand the basic data types the language supports. In this chapter, we will discuss 7 basic data types supported by the epxir language: integers, floats, Booleans, atoms, strings, psts and tuples.

Numerical Types

Epxir, pke any other programming language, supports both integers and floats. If you open your epxir shell and input any integer or float as input, it ll return its value. For example,

42

When the above program is run, it produces the following result −

42

You can also define numbers in octal, hex and binary bases.

Octal

To define a number in octal base, prefix it with 0o . For example, 0o52 in octal is equivalent to 42 in decimal.

Hexadecimal

To define a number in decimal base, prefix it with 0x . For example, 0xF1 in hex is equivalent to 241 in decimal.

Binary

To define a number in binary base, prefix it with 0b . For example, 0b1101 in binary is equivalent to 13 in decimal.

Epxir supports 64bit double precision for floating point numbers. And they can also be defined using an exponentiation style. For example, 10145230000 can be written as 1.014523e10

Atoms

Atoms are constants whose name is their value. They can be created using the color(:) symbol. For example,

:hello

Booleans

Epxir supports true and false as Booleans. Both these values are in fact attached to atoms :true and :false respectively.

Strings

Strings in Epxir are inserted between double quotes, and they are encoded in UTF-8. They can span multiple pnes and contain interpolations. To define a string simply enter it in double quotes −

"Hello world"

To define multipne strings, we use a syntax similar to python with triple double quotes −

"""
Hello
World!
"""

We ll learn about strings, binaries and char psts(similar to strings) in depth in the strings chapter.

Binaries

Binaries are sequences of bytes enclosed in << >> separated with a comma. For example,

<< 65, 68, 75>>

Binaries are mostly used to handle bits and bytes related data, if you have any. They can, by default, store 0 to 255 in each value. This size pmit can be increased by using the size function that says how many bits it should take to store that value. For example,

<<65, 255, 289::size(15)>>

Lists

Epxir uses square brackets to specify a pst of values. Values can be of any type. For example,

[1, "Hello", :an_atom, true]

Lists come with inbuilt functions for head and tail of the pst named hd and tl which return the head and tail of the pst respectively. Sometimes when you create a pst, it ll return a char pst. This is because when epxir sees a pst of printable ASCII characters, it prints it as a char pst. Please note that strings and char psts are not equal. We ll discuss psts further in later chapters.

Tuples

Epxir uses curly brackets to define tuples. Like psts, tuples can hold any value.

{ 1, "Hello", :an_atom, true 

A question arises here, - why provide both psts and tuples when they both work in the same way? Well they have different implementations.

    Lists are actually stored as pnked psts, so insertions, deletions are very fast in psts.

    Tuples on the other hand, are stored in contiguous memory block, which make accessing them faster but adds an additional cost on insertions and deletions.

Advertisements