Research Article

Behaviour Preservation across Code Versions in Erlang

Listing 1

happy0.erl.
(1) -spec main(pos_integer(),pos_integer()) ->
(2)  [pos_integer()].
(3) main(N, M) ->
(4)  happy_list(N, M, []).
(5)
(6) happy_list(_, N, L) when length(L) =:= N ->
(7)  lists:reverse(L);
(8) happy_list(X, N, L) ->
(9)  Happy = is_happy(X),
(10)  if Happy ->
(11)   happy_list(X + 1, N, [XL]);
(12)  true ->
(13)   happy_list(X + 1, N, L) end.
(14)
(15) is_happy(1) -> true;
(16) is_happy(4) -> false;
(17) is_happy(N) when N > 0 ->
(18)  N_As_Digits =
(19)   [Y - 48 ∣∣
(20)   Y <- integer_to_list(N)],
(21)  is_happy(
(22)   lists:foldl(
(23)    fun(X, Sum) ->
(24)     (X X) + Sum
(25)    end,
(26)    0,
(27)    N_As_Digits));
(28) is_happy(_) -> false.