我正在Haskell中实现Burrows-Wheeler Transformation。生成所有循环字符串的组合并将其存储在矩阵中作为变换的第一步。我正在使用Haskell List来构造矩阵。该列表将原始单词存储在列表头上,并将其循环组合存储在尾部。
Here is an Example of a transformed word
我编写了一个输出第一个循环字符串的函数。但是,如果我再次将该函数作为递归调用,我将面临无限循环。
这是函数
type BWT = [String] -- List of Strings for Burrows Wheeler matrix
samplebwt = ["BANANA$"] -- Sample input
rotateBWT:: BWT -> BWT
rotateBWT a = if (head (last a)) /= '$'
then [tail (head a) ++ [head (head a)]] ++ rotateBWT [tail (head a) ++ [head (head a)]]
else a
rotateBWT samplebwt
-- returns ["ANANA$B"]
--expected output ["ANANA$B", "NANA$BA", "ANA$BNA", "NA$BANA", "A$BANAN", "$BANANA"]
我错过了什么?
答案 0 :(得分:1)
当您对不包含rotateBWT
字符的字符串调用$
时,您可以进入无限循环。
您有一个解决方案可以生成您在示例中显示的输出:
type BWT = [String] -- List of Strings for Burrows Wheeler matrix
genBWTmatrix :: String -> BWT
genBWTmatrix str = rotateBWT [str ++ "$"]
rotateBWT :: BWT -> BWT
rotateBWT a = if (head l) == '$' then a
else a ++ rotateBWT n
where l = last a
n = [(tail l) ++ [head l]]
main = mapM_ putStrLn $ genBWTmatrix "BANANA" -- Prints: BANANA$
-- ANANA$B
-- NANA$BA
-- ANA$BAN
-- NA$BANA
-- A$BANAN
-- $BANANA