Hello World
-module(main).
-export([hello/0]).
hello() ->
io:format("Hello, world!~n").
Compile and run in the Erlang shell:
c(main).
main:hello().
This defines a tiny Erlang module and a function that prints one line to the shell.
Variables
Variables are single-assignment and start with an uppercase letter.
Name = "Dan",
Count = 1,
Active = true.
These are single-assignment bindings. Once a variable has a value in Erlang, you do not mutate it in place.
Pattern Matching
greet({user, Name}) ->
io:format("Hello, ~s!~n", [Name]).
This matches a tuple argument and pulls the name out directly in the function head.
Erlang Add
-module(add).
-export([add/2]).
add(Param1, Param2) ->
Param1 + Param2.
This just adds the two input numbers with the language’s normal arithmetic and returns the sum.
Erlang Add Border
-module(add_border).
-export([add_border/1]).
add_border(Picture) ->
Width = length(hd(Picture)),
Border = lists:duplicate(Width + 2, $*),
Bordered = [[$*] ++ Row ++ [$*] || Row <- Picture],
[Border] ++ Bordered ++ [Border].
This builds a new grid with a * border around every side. It adds a full top and bottom row, then wraps each existing row from left and right.
Erlang Adjacent Elements Product
-module(adjacent_elements_product).
-export([adjacent_elements_product/1]).
adjacent_elements_product(InputArray) ->
Pairs = lists:zip(InputArray, tl(InputArray)),
lists:max([X * Y || {X, Y} <- Pairs]).
This walks through neighboring values, multiplies each pair, and keeps the biggest product it finds.
Erlang Almost Magic Square
-module(almost_magic_square).
-export([almost_magic_square/1]).
almost_magic_square(A) ->
Rows = chunk3(A),
RowSums = [lists:sum(R) || R <- Rows],
ColSums = [lists:sum([lists:nth(C, R) || R <- Rows]) || C <- [1, 2, 3]],
MaxSum = lists:max(RowSums ++ ColSums),
FinalRows = balance(0, 0, RowSums, ColSums, Rows, MaxSum),
lists:append(FinalRows).
chunk3([]) -> [];
chunk3(List) ->
{Row, Rest} = lists:split(3, List),
[Row | chunk3(Rest)].
balance(I, J, RowSums, ColSums, Rows, MaxSum) when I < 3, J < 3 ->
RowSumI = lists:nth(I + 1, RowSums),
ColSumJ = lists:nth(J + 1, ColSums),
Diff = min(MaxSum - RowSumI, MaxSum - ColSumJ),
Rows1 = add_at(Rows, I, J, Diff),
RowSums1 = add_at_list(RowSums, I, Diff),
ColSums1 = add_at_list(ColSums, J, Diff),
NextI = case lists:nth(I + 1, RowSums1) of MaxSum -> I + 1; _ -> I end,
NextJ = case lists:nth(J + 1, ColSums1) of MaxSum -> J + 1; _ -> J end,
balance(NextI, NextJ, RowSums1, ColSums1, Rows1, MaxSum);
balance(_, _, _, _, Rows, _) ->
Rows.
add_at(Rows, I, J, Diff) ->
[case Idx of
I -> [case Jdx of J -> V + Diff; _ -> V end
|| {Jdx, V} <- lists:zip(lists:seq(0, length(Row) - 1), Row)];
_ -> Row
end || {Idx, Row} <- lists:zip(lists:seq(0, length(Rows) - 1), Rows)].
add_at_list(List, Idx0, Diff) ->
[case I of Idx0 -> V + Diff; _ -> V end
|| {I, V} <- lists:zip(lists:seq(0, length(List) - 1), List)].
This adjusts the matrix toward a matching target sum so the rows and columns line up more like a magic square.
Erlang Are Equally Strong
-module(are_equally_strong).
-export([are_equally_strong/4]).
are_equally_strong(YourLeft, YourRight, FriendsLeft, FriendsRight) ->
max(YourRight, YourLeft) =:= max(FriendsLeft, FriendsRight) andalso
min(YourLeft, YourRight) =:= min(FriendsRight, FriendsLeft).
This compares each person’s strongest and weakest arm. If both pairs match, the result is true.
Erlang Array Change
-module(array_change).
-export([array_change/1]).
array_change([H | T]) ->
{_, Total} = lists:foldl(fun(X, {Prev, Acc}) ->
case X =< Prev of
true ->
New = Prev + 1,
{New, Acc + (New - X)};
false ->
{X, Acc}
end
end, {H, 0}, T),
Total.
This moves left to right and bumps values only when needed so the array becomes strictly increasing.
Erlang Array Maximal Adjacement Difference
-module(array_maximal_adjacent_difference).
-export([array_maximal_adjacent_difference/1]).
array_maximal_adjacent_difference(A) when length(A) < 3 ->
0;
array_maximal_adjacent_difference(A) ->
Triples = lists:zip3(A, tl(A), tl(tl(A))),
lists:max([max(abs(Cur - Prev), abs(Cur - Next)) || {Prev, Cur, Next} <- Triples]).
This checks the gap between each pair of neighbors and returns the largest difference.
Erlang Binary Gap
-module(binary_gap).
-export([binary_gap/1]).
binary_gap(N) ->
Bin = integer_to_list(N, 2),
Trimmed = string:trim(Bin, both, "0"),
Groups = string:split(Trimmed, "1", all),
lists:max([length(G) || G <- Groups]).
This turns the number into binary, ignores zeroes outside the edges, and finds the longest run of zeroes between 1s.
Erlang Bracket
-module(bracket).
-export([bracket/1]).
bracket(S) ->
case close_stack(S, []) of
[] -> 1;
_ -> 0
end.
close_stack([], Stack) -> Stack;
close_stack([$( | T], Stack) -> close_stack(T, [$( | Stack]);
close_stack([$[ | T], Stack) -> close_stack(T, [$[ | Stack]);
close_stack([${ | T], Stack) -> close_stack(T, [${ | Stack]);
close_stack([$) | T], [$( | Stack1]) -> close_stack(T, Stack1);
close_stack([$] | T], [$[ | Stack1]) -> close_stack(T, Stack1);
close_stack([$} | T], [${ | Stack1]) -> close_stack(T, Stack1);
close_stack([$) | _], _) -> error;
close_stack([$] | _], _) -> error;
close_stack([$} | _], _) -> error;
close_stack([_ | T], Stack) -> close_stack(T, Stack).
This uses a simple stack approach: open brackets go in, matching closing brackets pop them out.