Hope someone helps find out where my mistake is. Call g 3 4 0 2 (M.empty,0) [] , I would expect [[2,1,0,1]] . Instead, I see [[2,1,0,1],[2,1,0,1]] .
It is assumed that the program accumulates various digital figures of length m by adding various numbers to the list, returning back when it reaches n-1 and up when it reaches 0 . An obvious problem occurs in the middle when a function is called recursively for both up and down directions.
If I comment line 11 as follows:
else gnm (digitCount + 1) (lastDigit + 1) (hash',hashCount') (lastDigit:digits) -- gnm (digitCount + 1) (lastDigit - 1) (hash',hashCount') (lastDigit:digits)
I get the correct result []
As with commenting out line 11 and changing line 10 to:
else gnm (digitCount + 1) (lastDigit - 1) (hash',hashCount') (lastDigit:digits)
And again the correct result [[2,1,0,1]]
Why, when I call g twice with the ++ operator, do I get two [2,1,0,1] instead of one? In my opinion, each result in g should be different, because in any recursive call, another order accumulates (or should) accumulate.
Thanks in advance.
import qualified Data.Map as M g :: Int -> Int -> Int -> Int -> (M.Map Int Bool, Int) -> [Int] -> [[Int]] gnm digitCount lastDigit (hash,hashCount) digits | digitCount == m = if test then [reverse digits] else [] | otherwise = if lastDigit == 0 then gnm (digitCount + 1) (lastDigit + 1) (hash',hashCount') (lastDigit:digits) else if lastDigit == n - 1 then gnm (digitCount + 1) (lastDigit - 1) (hash',hashCount') (lastDigit:digits) else gnm (digitCount + 1) (lastDigit + 1) (hash',hashCount') (lastDigit:digits) ++ gnm (digitCount + 1) (lastDigit - 1) (hash',hashCount') (lastDigit:digits) where test = hashCount == n (hash',hashCount') = if test then (M.empty,hashCount) else case M.lookup lastDigit hash of Just anyting -> (hash,hashCount) Nothing -> (M.insert lastDigit True hash,hashCount + 1)