Transforming Lists
Learn how to transform lists using bounded recursive functions.
We'll cover the following...
Transforming lists using recursion
In daily programming, we often face situations where we need to transform things into other things. Lists are the main actors in the routine of data transformations. Some examples include transforming debit accounts into blocked accounts, draft blog posts into published posts, strings into data structures, and user inputs into table rows. Data is immutable in functional programming, so when we transform data, we’re actually building new data. The transformation process in lists requires repetitive steps, and we can use recursive functions to do it. Let’s see how we can create new lists using recursion in Elixir.
The [head | tail]
syntax is useful for destructuring arguments, but it’s also useful for constructing new lists.
iex> [:a | [:b, :c]]
#Output -> [:a, :b, :c]
iex> [:a, :b | [:c]]
#Output -> [:a, :b, :c]
iex> [:a, :b, :c]
#Output -> [:a, :b, :c]
Try the above code in your IEx to see how it works:
Using that syntax, the ...