Executing Code and Generating a Result

Elixir can generate a result for any expression. The process is similar to when you were in high school solving mathematical equations: to generate a result, you must add or multiply some numbers or change some Xs to Ys. We’ll create expressions for the computer, and the computer will show us the result. The simplest expression is a value, like this:

 iex>​ 42
 42

The number 42 is an expression that evaluates to the value we typed. Let’s try a different expression:

 iex>​ 1 + 1
 2

The number 1 is a value, and + is an operator. Operators compute values and generate a result. We can also combine multiple operators and values:

 iex>​ (2 + 2) * 3
 12
 iex>​ 2 + 2 * 3
 8

Each operator is executed in a ...

Get Learn Functional Programming with Elixir now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.