Skip to content

Instantly share code, notes, and snippets.

@joseanpg
Last active August 29, 2015 14:14
Show Gist options
  • Save joseanpg/aa60cf1d529fb98925c2 to your computer and use it in GitHub Desktop.
Save joseanpg/aa60cf1d529fb98925c2 to your computer and use it in GitHub Desktop.
@Jose_A_Alonso Exercitium: Particiones de enteros positivos
-- www.glc.us.es/~jalonso/exercitium/particiones-de-enteros-positivos/
zen :: ([a] -> [a]) -> [a] -> [a] -> [a]
zen f a b =
let a' = f a
b' = b ++ a
in case a' of
[] -> b'
_ -> zen f a' b'
particiones n = zen cultivarParticiones [[n]] []
cultivarParticion particion =
case particion of
[x] -> [[x-z,z] | z <- [1 .. (div x 2)]] -- z <= x - z
(x:y:t) -> [(x-z):z:y:t| z <- [y .. (div x 2)]] -- z <= x - z
cultivarParticiones particiones =
particiones >>= cultivarParticion
gen f = concat . takeWhile (/=[]) . iterate f
particiones = gen cultivarParticiones . return . return
cultivarParticion particion = case particion of
[x] -> [[x-z,z] | z <- [1 .. (div x 2)]]
(x:y:t) -> [(x-z):z:y:t| z <- [y .. (div x 2)]]
cultivarParticiones particiones = particiones >>= cultivarParticion
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment