[Haskell-cafe] How to dynamic plan in Haskell?

David Kraeutmann kane at kane.cx
Sat May 18 06:12:50 UTC 2019


This is also an interesting solution:

sums :: [[Integer]]
sums = do
  x <- [1..9]
  go x [x] (10 - x)

  where
    go x xs r
      | r > 0
      = do
        x <- [1..min x r]
        go x (x:xs) (r - x)

      | otherwise = return xs


On 05/18/2019 02:00 AM, Magicloud Magiclouds wrote:
> I see. Did not got the filterM part in mind. Thanks.
>
> On Sat, May 18, 2019 at 1:48 PM Francesco Ariis <fa-ml at ariis.it> wrote:
>> Hello,
>>
>> On Sat, May 18, 2019 at 12:33:00PM +0800, Magicloud Magiclouds wrote:
>>> I solved the question. But I could not figure out a FP style solution.
>>>
>>> Question:
>>>
>>> 1 - 9, nine numbers. Show all the possible combinations that sum up to
>>> 10. Different orders are counted as the same.
>> A possible solution takes advantage of powersets with the [] Monad.
>>
>>     λ> :m +Control.Monad
>>     λ> f cs = filterM (\x -> [True, False]) cs
>>     λ> filter ((==10) . sum) (f [1..10])
>>
>> _______________________________________________
>> Haskell-Cafe mailing list
>> To (un)subscribe, modify options or view archives go to:
>> http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
>> Only members subscribed via the mailman list are allowed to post.
> _______________________________________________
> Haskell-Cafe mailing list
> To (un)subscribe, modify options or view archives go to:
> http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
> Only members subscribed via the mailman list are allowed to post.



More information about the Haskell-Cafe mailing list