自然数の分割
Posted feedbacks - Haskell
comprehension大活躍。
1 2 | partitionNum n 1 = [[n]]
partitionNum n m = [(n-x):xs | x <- [0..n], xs <- partitionNum x (m-1)]
|
高階関数を使った版。 コード長くなった、読みづらくなった。orz
1 2 3 4 5 | import List
partitionNum n m = iterate f ([[]]: repeat []) !! m !! n where
f xs = tail $ snd $ mapAccumL g (repeat []) [0..] where
g (y:ys) i = (zipWith (++) (map (map (i:)) xs) ys, y)
|





herumi
#4099()
Rating1/1=1.00
[ reply ]