0

First of all, yeah I know that I should use ghc instead (but we are forced to use hugs in the course)

So I try to generate all permutations of [1 .. 9] but when evaluating this, hugs throws an error: "ERROR - Garbage collection fails to reclaim sufficient space"

Is there any quickfix or roundabout for this?

4

1 回答 1

3

问题很可能不是因为 Hugs,而是因为您的排列函数是以防止垃圾收集的方式编写的,或者只是分配了过多的内存。

以下排列定义适用[1..9]于 GHC 和 Hugs 对我来说(尽管permutations [1..9]在 Hugs 中要求垃圾收集器在我的计算机上被调用 58 次)

permutations :: [a] -> [[a]]
permutations [] = [[]]
permutations (x:xs) = concatMap insertEverywhere (permutations xs)
  where insertEverywhere [] = [[x]]
        insertEverywhere (y:ys) = (x:y:ys): (map (y:) $ insertEverywhere ys)
于 2018-04-22T17:46:40.967 回答