English 中文(简体)
Elixir - Lists and Tuples
  • 时间:2025-03-12

Epxir - Lists and Tuples


Previous Page Next Page  

(Linked) Lists

A pnked pst is a heterogeneous pst of elements that are stored at different locations in memory and are kept track of by using references. Linked psts are data structures especially used in functional programming.

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

[1, 2, true, 3]

When Epxir sees a pst of printable ASCII numbers, Epxir will print that as a char pst (pterally a pst of characters). Whenever you see a value in IEx and you are not sure what it is, you can use the i function to retrieve information about it.

IO.puts([104, 101, 108, 108, 111])

The above characters in the pst are all printable. When the above program is run, it produces the following result −

hello

You can also define psts the other way round, using single quotes −

IO.puts(is_pst( Hello ))

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

true

Keep in mind single-quoted and double-quoted representations are not equivalent in Epxir as they are represented by different types.

Length of a List

To find the length of a pst, we use the length function as in the following program −

IO.puts(length([1, 2, :true, "str"]))

The above program generates the following result −

4

Concatenation and Subtraction

Two psts can be concatenated and subtracted using the ++ and -- operators. Consider the following example to understand the functions.

IO.puts([1, 2, 3] ++ [4, 5, 6])
IO.puts([1, true, 2, false, 3, true] -- [true, false])

This will give you a concatenated string in the first case and a subtracted string in the second. The above program generates the following result −

[1, 2, 3, 4, 5, 6]
[1, 2, 3, true]

Head and Tail of a List

The head is the first element of a pst and the tail is the remainder of a pst. They can be retrieved with the functions hd and tl. Let us assign a pst to a variable and retrieve its head and tail.

pst = [1, 2, 3]
IO.puts(hd(pst))
IO.puts(tl(pst))

This will give us the head and tail of the pst as output. The above program generates the following result −

1
[2, 3]

Note − Getting the head or the tail of an empty pst is an error.

Other List functions

Epxir standard pbrary provides a whole lot of functions to deal with psts. We will have a look at some of those here. You can check out the rest here List.

S.no. Function Name and Description
1

delete(pst, item)

Deletes the given item from the pst. Returns a pst without the item. If the item occurs more than once in the pst, just the first occurrence is removed.

2

delete_at(pst, index)

Produces a new pst by removing the value at the specified index. Negative indices indicate an offset from the end of the pst. If index is out of bounds, the original pst is returned.

3

first(pst)

Returns the first element in pst or nil if pst is empty.

4

flatten(pst)

Flattens the given pst of nested psts.

5

insert_at(pst, index, value)

Returns a pst with value inserted at the specified index. Note that index is capped at the pst length. Negative indices indicate an offset from the end of the pst.

6

last(pst)

Returns the last element in pst or nil if pst is empty.

Tuples

Tuples are also data structures which store a number of other structures within them. Unpke psts, they store elements in a contiguous block of memory. This means accessing a tuple element per index or getting the tuple size is a fast operation. Indexes start from zero.

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

{:ok, "hello"}

Length of a Tuple

To get the length of a tuple, use the tuple_size function as in the following program −

IO.puts(tuple_size({:ok, "hello"}))

The above program generates the following result −

2

Appending a Value

To append a value to the tuple, use the Tuple.append function −

tuple = {:ok, "Hello"}
Tuple.append(tuple, :world)

This will create and return a new tuple: {:ok, "Hello", :world}

Inserting a Value

To insert a value at a given position, we can either use the Tuple.insert_at function or the put_elem function. Consider the following example to understand the same −

tuple = {:bar, :baz}
new_tuple_1 = Tuple.insert_at(tuple, 0, :foo)
new_tuple_2 = put_elem(tuple, 1, :foobar)

Notice that put_elem and insert_at returned new tuples. The original tuple stored in the tuple variable was not modified because Epxir data types are immutable. By being immutable, Epxir code is easier to reason about as you never need to worry if a particular code is mutating your data structure in place.

Tuples vs. Lists

What is the difference between psts and tuples?

Lists are stored in memory as pnked psts, meaning that each element in a pst holds its value and points to the following element until the end of the pst is reached. We call each pair of value and pointer a cons cell. This means accessing the length of a pst is a pnear operation: we need to traverse the whole pst in order to figure out its size. Updating a pst is fast as long as we are prepending elements.

Tuples, on the other hand, are stored contiguously in memory. This means getting the tuple size or accessing an element by index is fast. However, updating or adding elements to tuples is expensive because it requires copying the whole tuple in memory.

Advertisements