Skip to content

Instantly share code, notes, and snippets.

@gin0606
Last active August 29, 2015 14:07
Show Gist options
  • Save gin0606/10a5e503b3884098ed27 to your computer and use it in GitHub Desktop.
Save gin0606/10a5e503b3884098ed27 to your computer and use it in GitHub Desktop.
primeFactorization :: (Integral a) => a -> [a]
primeFactorization 0 = []
primeFactorization 1 = []
primeFactorization 2 = [2]
primeFactorization x =
let fstf = firstFactor x
xx = (div x fstf)
in [fstf] ++ primeFactorization xx
firstFactor :: Integral a => a -> a
firstFactor 0 = 0
firstFactor 1 = 1
firstFactor x = firstFactor' x 2
where firstFactor' :: Integral a => a -> a -> a
firstFactor' xx n
| xx `mod` n == 0 = n
| otherwise = firstFactor' xx (n + 1)
@gin0606
Copy link
Author

gin0606 commented Oct 3, 2014

ghciでprimeFactorization 1呼ぶと怒られる

    No instance for (Show a0) arising from a use of ‘print’
    The type variable ‘a0’ is ambiguous
    Note: there are several potential instances:
      instance Show Double -- Defined in ‘GHC.Float’
      instance Show Float -- Defined in ‘GHC.Float’
      instance (Integral a, Show a) => Show (GHC.Real.Ratio a)
        -- Defined in ‘GHC.Real’
      ...plus 24 others
    In a stmt of an interactive GHCi command: print it

@gin0606
Copy link
Author

gin0606 commented Oct 3, 2014

Integralの割り算はdiv使うっぽい

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment