I'm trying to solve a HackerRank problem and I am getting an error that I cannot figure out. The issue is in the solve1
function. The exact error is: cannot construct the infinite type a ~ t0 a. Expected type ([t0 a], [t0 a], [t0 a]) Actual type ([a], [a], [a]). In the second argument of `tripleMap`, namely '(tripList xs)'
.
I keep looking at the types and they continue to appear correct in my eyes. tripList
takes a list of numbers and returns a triple of lists of numbers. tripleMap
takes a triple of lists of numbers as its second argument.
In testing tripList
in my REPL I get the desired results:
> tripList [1,0,-1,0,1]
([1,1],[0,0],[-1])
Here is my code:
length' :: (Foldable t, Num b, Fractional b, Ord b) => t a -> b
length' = foldr (\_ acc -> 1 + acc) 0
tripList :: (Num a, Ord a) => [a] -> ([a], [a], [a])
tripList xs =
( filter (>0) xs
, filter (==0) xs
, filter (<0) xs )
foldSolution :: (Foldable t, Num a, Ord a, Fractional a)
=> a -> t a -> a
foldSolution n = foldr (\x y -> x/n + y/n) 0
tripleMap :: (a -> b) -> ([a], [a], [a]) -> ([b], [b], [b])
tripleMap f (a, b, c) = (map f a, map f b, map f c)
solve1 :: (Num a, Ord a) => [a] -> ([a], [a], [a])
solve1 xs = tripleMap (foldSolution (length' xs)) (tripList xs)
Your foldSolution (length' xs)
takes t a
and returns a
. So, when you tripleMap
it on tripList xs
(which has type ([a], [a], [a])
), you get a tuple of type (a, a, a)
, not ([a], [a], [a])
, which is why you got the error.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments