Research Article

Behaviour Preservation across Code Versions in Erlang

Listing 2

happy1.erl.
(1) is_happy(X, XS) ->
(2)  if
(3)   X == 1 -> true;
(4)   X < 1 -> false;
(5)   true ->
(6)    case member(X, XS) of
(7)     true -> false;
(8)     false ->
(9)      is_happy(sum(map(fun(Z) -> ZZ end,
(10)       [Y - 48 ∣∣ Y <- integer_to_list(X)])),
(11)        [XXS])
(12)    end
(13)  end.
(14) happy(X, Top, XS) ->
(15)  if
(16)   length(XS) == Top -> sort(XS);
(17)   true ->
(18)    case  is_happy(X,[]) of
(19)     true -> happy(X + 1, Top, [XXS]);
(20)     false -> happy(X + 1,Top, XS)
(21)    end
(22)  end.
(23)
(24) -spec main(pos_integer(),pos_integer()) ->
(25)  [pos_integer()].
(26) main(N, M) ->
(27)  happy(N, M, []).
(28)