So, I play with this:
factors :: Integral a => a -> [a] factors n = filter (\d -> n `rem` d == 0) . takeWhile (\d -> d*d <= n) $ [ 1 .. ] abundants_perfects_deficients :: Integral a => ([a],[a],[a]) abundants_perfects_deficients = foldr switch ([],[],[]) [1..] where switch :: Integral a => a -> ([a],[a],[a]) -> ([a],[a],[a]) switch n (as,ps,ds) = let t = sum (factors n) in if t < n then (as,ps,n:ds) else if t == n then (as,n:ps,ds) else (n:as,ps,ds)
And while I have abundants_perfects_deficients
, I would prefer to have three values: abundants
, perfects
and deficients
all of the type Integral a -> [a]
.
One thing that doesn't work:
abundants,perfects,deficients :: Integral a => [a] (abundants,perfects,deficients) = abundants_perfects_deficients
Since this limits three to all, they must be the same a
.
I tried to do something one by one, so they will not mutually limit, but this also did not work:
perfects :: Integral a => [a] (_,perfects,_) = abundants_perfects_deficients
Because the compiler could not figure out how to convert a value of type forall a. Integral a => ([a],[a],[a])
forall a. Integral a => ([a],[a],[a])
into type (t1, forall a. Integral a => [a], t2)
.
Which seems complicated enough.
Now I know that I can implement them separately (just perfects = filter isPerfect [1..]
) or restrict them to the same type ( (abundants,perfects,deficients) = abundants_perfects_deficients
works fine if abundants,perfects,deficients :: [Integer]
), but
- I like to use general information to create all three
- I want to not just be limited to
Integer
s
ideas?
Change Excitingly it works:
abundants :: Integral a => [a] abundants = f as where as :: [Integer] (as,_,_) = abundants_perfects_deficients f :: Integral a => [Integer] -> [a] f = map fromInteger
But this is not so:
abundants_perfects_deficients' :: (Integral a,Integral p, Integral d) => ([a],[p],[d]) abundants_perfects_deficients' = (f as, f ps, f ds) where as,ps,ds :: [Integer] (as,ps,ds) = abundants_perfects_deficients f :: Integral a => [Integer] -> [a] f = map fromInteger abundants,perfects,deficients :: (Integral a) => [a] (abundants,perfects,deficients) = abundants_perfects_deficients'
I have no idea why.