haskell - implementing a per-digit counter using the list monad -
So, I was looking at this question, and made an ugly solution for this problem. While trying to clean it, I started checking the understanding of the list and the list monad. What I have decided to do, was to implement a counter counter using the list Monad. Given the input sequence of digits, [1, 2]
, I wanted to generate an output sequence that looked like this:
[[0, 0 ], [0, 1], [0, 2], [1, 0], [1, 1], [1, 2]]
That is, the list in that category Value of all elements in
Haskell.OR says:
The bound function is added to all the possible values in the input list and as a result, lists are added to list all the possible outcomes. .
Great! It looks right ... the code I have written here is written to prepare the solution:
count :: [integer] -> [[Integer]] Count [] = [] Calculate (x: xs) = - Receive all possible sequences for the remaining digits, remDigits :: [[Integer]] remDigits = in the calculation xs - drag a possible sequence to the remainder Let's next point & lt; - remDigits - Drag all possible values for the current digit y
But calling count
generates empty list, and I do not know why, first of all, As a single-case case for singleton list, I
Count :: [integer] - & gt; [[Integer]] count [] = [] calculation [n] = map (\ x -> [x]) [0 ... n] count (x: xs) = do y & lt; - [0..x] NextDigits & lt; - Count xs return (y: nextDigits) Main = Print $ count [1] Print $ count [1,2]
Comments
Post a Comment