With Elixir
a = 100000000000000000
b = 1_000_000_000
hex = 0xcadbef
bin = 0b010101010
oct = 0o146723Module, name, arity
iex> sum = fn (a, b) -> a + b end
iex> sum.(5,6)
11
iex> sum.(1.0, 5)
6.0
iex> mul = fn
(a, b) -> a * b
end
iex> square = fn a -> a * a endiex> sum = fn (a,b) when is_integer(a) and is_integer(b) -> a + b end
iex> sum.(5,6)
11
iex> sum.(1.0, 5)
** (FunctionClauseError) no function clause matching in :erl_eval."-inside-an-interpreted-fun-"/2
iex> mul = fn (a,b) when is_number(a) and is_number(b) -> a * b end
iex> mul.(5,6)
30
iex> mul.(1.0, 5)
5.0when
iex> func = fn (:sum, {a,b}) -> a + b end
iex> func.(:sum, {5, 6})
11
iex> func.(:mul, {5, 6})
iex> head = fn [h | _t] = list -> {list, h} end
iex> head.([1,2,3])
{[1, 2, 3], 1}iex> sum = &(&1 + &2)
iex> sum.(5, 6)
11
iex> sum.(1.0, 5)
6.0
iex> mul = &(&1 * &2)
iex> mul.(5, 6)
30
iex> ## Try defining cube using & operator& operator
iex> math = fn
(:sum, {a, b}) -> a + b
(:mul, {a, b}) -> a * b
(:sub, {a, b}) -> a - b
(:abs, a) when a < 0 -> -a
(:abs, a) when a > 0 -> a
(:div, {a, b}) when b != 0 -> a / b
end
iex> math.(:sum, {5,6})
11
iex> math.(:abs, -5)
5
iex> math.(:sub, {5,6})
-1
iex> math.(:abs, 0)
???defmodule MyMath do
# Simple function
def sum(a, b) do
a + b
end
# Function with guard clauses
def div(a, b) when is_number(a) and is_number(b) and b != 0 do
a / b
end
# Short form
def mul(a, b), do: a * b
# Function with multiple clauses
def abs(a) when a < 0, do: -a
def abs(a) when a >= 0, do: a
end