Skip to content

Instantly share code, notes, and snippets.

@derdewey
Created April 12, 2011 07:13
Show Gist options
  • Save derdewey/915092 to your computer and use it in GitHub Desktop.
Save derdewey/915092 to your computer and use it in GitHub Desktop.
A non-working way of listing factors in haskell
isDivisor :: (Integral a) => a -> a -> b
isDivisor number divisor = ((number `mod` divisor) == 0)
l_factors :: (Integral a) => a -> a -> [a] -> [a]
l_factors 1 target list = list
l_factors divisor target list
| isDivisor target divisor = (l_factors divisor-1 target/divisor divisor:list)
| otherwise = (l_factors divisor-1 target divisor:list)
[1 of 1] Compiling Main ( problem3.hs, interpreted )
problem3.hs:9:36:
Couldn't match type `a' with `a -> [a] -> [a]'
`a' is a rigid type variable bound by
the type signature for
l_factors :: Integral a => a -> a -> [a] -> [a]
at problem3.hs:7:1
In the return type of a call of `l_factors'
In the first argument of `(-)', namely `l_factors divisor'
In the first argument of `(:)', namely
`l_factors divisor - 1 target / divisor divisor'
problem3.hs:9:63:
Couldn't match type `a' with `a -> a'
`a' is a rigid type variable bound by
the type signature for
l_factors :: Integral a => a -> a -> [a] -> [a]
at problem3.hs:7:1
The function `divisor' is applied to one argument,
but its type `a' has none
In the second argument of `(/)', namely `divisor divisor'
In the second argument of `(-)', namely
`1 target / divisor divisor'
problem3.hs:10:21:
Couldn't match type `a' with `a -> [a] -> [a]'
`a' is a rigid type variable bound by
the type signature for
l_factors :: Integral a => a -> a -> [a] -> [a]
at problem3.hs:7:1
In the return type of a call of `l_factors'
In the first argument of `(-)', namely `l_factors divisor'
In the first argument of `(:)', namely
`l_factors divisor - 1 target divisor'
Failed, modules loaded: none.
[1 of 1] Compiling Main ( problem3.hs, interpreted )
problem3.hs:5:29:
Could not deduce (b ~ Bool)
from the context (Integral a)
bound by the type signature for
isDivisor :: Integral a => a -> a -> b
at problem3.hs:5:1-56
`b' is a rigid type variable bound by
the type signature for isDivisor :: Integral a => a -> a -> b
at problem3.hs:5:1
In the expression: ((number `mod` divisor) == 0)
In an equation for `isDivisor':
isDivisor number divisor = ((number `mod` divisor) == 0)
Failed, modules loaded: none.
@BMeph
Copy link

BMeph commented Apr 12, 2011

l_factors :: (Integral a) => a -> a -> [a] -> [a]
l_factors 1 target list = list
l_factors divisor target list
| isDivisor target divisor = l_factors (divisor-1) (div target divisor) (divisor:list)
| otherwise = l_factors (divisor-1) target (divisor:list)

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