-- generates names in the following order
-- a, b, c ... z, aa, ba, ca, ... za, ab, bb, cb ...
nextName :: String -> String
nextName [] = "a"
nextName (x:xs) = if x == 'z' then 'a' : nextName xs else succ x : xs
-- verify if the number of names generated is as expected.
countNames :: String -> String -> Int
countNames start end = loop 1 start
where
loop acc next =
if next == end then
acc
else
loop (acc + 1) (nextName next)
countNames "a" "zzzzzz"
在 ghci 中运行
在我的 com 上运行它会占用整个内存,并且需要花费大量时间才能完成。
如果有人指出发生空间泄漏的位置和原因,将不胜感激?