Last active
August 29, 2015 14:14
-
-
Save joseanpg/aa60cf1d529fb98925c2 to your computer and use it in GitHub Desktop.
@Jose_A_Alonso Exercitium: Particiones de enteros positivos
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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