Skip to content

Instantly share code, notes, and snippets.

@hiroshi-maybe
Created March 30, 2013 08:41
Show Gist options
  • Select an option

  • Save hiroshi-maybe/5275905 to your computer and use it in GitHub Desktop.

Select an option

Save hiroshi-maybe/5275905 to your computer and use it in GitHub Desktop.
Split an integer to a set of integers (i.e. split_int 5 3 -> [1,1,3], [1,2,2])
split_int :: Int -> Int -> [[Int]]
split_int n set_num = split_int_helper n set_num 1
split_int_helper n 1 _ = [[n]]
split_int_helper n set_num start_idx = do
let last_idx = truncate $ (fromIntegral n)/(fromIntegral set_num)
x <- [start_idx..last_idx]
y <- split_int_helper (n-x) (set_num-1) x
return (x:y)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment