[Haskell-cafe] List algorithm

Mark T.B. Carroll mark at ixod.org
Mon May 21 16:34:43 EDT 2007


Mark.Carroll at Aetion.com (Mark T.B. Carroll) writes:

> alg n m =
>     case signum m of
>       -1 -> []
>       0 -> [[]]
>       1 -> [ x : xs | x <- [1..n], xs <- alg n (m - x) ]

FWIW it's faster if you do some memoising:

algMemo n m = lookupMemo m
    where
      memo = [[]] : map helper [1..m]
      lookupMemo m = if m < 0 then [] else memo !! m
      helper m' = [ x : xs | x <- [1..n], xs <- lookupMemo (m' - x) ]

-- Mark



More information about the Haskell-Cafe mailing list