Created
July 18, 2018 01:38
-
-
Save sliptype/472a0752ab514963f27d74a7da28f4bd to your computer and use it in GitHub Desktop.
Multiplicative Partitions in Purescript (Purescript by Example, Chapter 4.11 Exercise 4)
This file contains 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
module Factorizations where | |
import Prelude | |
import Control.MonadZero (guard) | |
import Data.Array ((..), sort, nub, snoc) | |
factorizations :: Int -> Array (Array Int) | |
factorizations = nub <<< map sort <<< factors | |
where | |
factors :: Int -> Array (Array Int) | |
factors 1 = [[1]] | |
factors i = do | |
j <- 1 .. (i - 1) | |
guard (i `mod` j == 0) | |
k <- factors j | |
pure $ snoc k $ i / j | |
-- factorizations 10 | |
-- [[1,10],[1,2,5]] | |
-- factorizations 12 | |
-- [[1,12],[1,2,6],[1,2,2,3],[1,3,4]] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment