我想在Haskell中创建一个函数,如下所示:
encode :: [(Char,Char)] - > String - > String
我有一个名为"table"的"字典"函数,它创建了一对对列表:
table :: Int -> [Char] -> [(Char, Char)]
table __ [] = []
table n l = zip l (drop n (cycle l))
Run Code Online (Sandbox Code Playgroud)
encode函数按字符对字符串进行编码.到目前为止它看起来像这样:
encode (x:xs) (n:ns)
| length(n:ns) > 1 && (fst x) == n = [snd x] ++ (encode (x:xs) ns)
| length(xs) > 1 && (fst x) /= n = encode xs (n:ns)
| length(ns) > 0 && length(xs) == 0 = encode (x:xs) ns
Run Code Online (Sandbox Code Playgroud)
对于测试, …