ブロックソートによる符号化

30分プログラム、その793。anarchy golf - BWTにインスパイアされて、ブロックソートによる符号化にチャレンジしてみました。
復号は明日やります。

使い方

1> bwt:bwt("cacao").
{3,"ccoaa"}

ソースコード

-module(bwt).
-compile([export_all]).

shift(N, Xs) ->
    {Ys, Zs} = lists:split(N, Xs),
    Zs ++ Ys.

index(_X, [], _Index) ->
     -1;
index(X, [X|_],Index) ->
    Index;
index(X, [_|Xs], Index) ->
    index(X,Xs,Index+1).

index(X,Xs)->
    index(X,Xs,1).

bwt(Xs) ->
    Xss = lists:sort([ shift(N,Xs) || N <- lists:seq(0,length(Xs)-1)]),
    { index(Xs,Xss), [ lists:last(Ys) || Ys <- Xss ] }.