Skip to content

Instantly share code, notes, and snippets.

@sliptype
Created July 18, 2018 01:38
Show Gist options
  • Save sliptype/472a0752ab514963f27d74a7da28f4bd to your computer and use it in GitHub Desktop.
Save sliptype/472a0752ab514963f27d74a7da28f4bd to your computer and use it in GitHub Desktop.
Multiplicative Partitions in Purescript (Purescript by Example, Chapter 4.11 Exercise 4)
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