Wiki2
MonadPrimes

Factor number using monads

Test using monads to factor number

primes = [2, 3, 5, 7, 11, 13, 17]

factorOnce :: Int -> [Int]
factorOnce n = case factor of Just d    -> [d, div n d]
                              otherwise -> []
  where
    factor = L.find (\m -> mod n m == 0) primes

factor :: [Int] -> [Int]
factor ns | length ns' == length ns = ns
          | otherwise               = factor ns'
  where
    ns' = L.filter (>1) (ns >>= factorOnce)