Beta
Two new Erlang functions here, beta/2 which calculates the β function at offset n, and allBetas/1, which computes all β and returns them in a list [could be a tuple if you want
]
-module(rinder).
-compile(export_all).
beta(X,0) -> -1;
beta(X,1) -> 0;
beta(X,J) ->
I= 1+beta(X,J-1),
Cond=element(I,X),
Cond2=element(J,X),
case Cond of
Cond2 -> I;
_ -> beta(X,I)
end.allBetas(X) ->
XX=list_to_tuple(X),
allBetas(XX,[-1,0],2,size(XX)).
allBetas(X,Acc,J,M) when J>M->
Acc;
allBetas(X,Acc,J,M) ->
I=1+beta(X,J-1),
Cond=element(I,X),
Cond2=element(J,X),
case Cond of
Cond2 ->
allBetas(X,lists:flatten([Acc,I]),J+1,M);
_ ->
allBetas(X,lists:flatten([Acc,element(I+1,list_to_tuple(Acc))]),J+1,M)
end.log_to_screen(A,B) -> io:format(A,B).
rinder:beta(list_to_tuple("abacabac"),3).
1
Yes I know too lazy to provide a conversion from list to tuple. Next time…
rinder:allBetas("abacabac").
[-1,0,0,1,0,1,2,3,4]
which thankfully matches what Christian calculated…
Since Erlang’s tuples and lists start at 1, the line
allBetas(X,lists:flatten([Acc,element(I+1,list_to_tuple(Acc))]),J+1,M)
has I + 1 instead of i in the algorithm (else β[j] < -- β[i]).
Erlang
