Struggling to understand a "Cannot construct the infinite type" error

Nicholas Hubbard

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)
Aplet123

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.

edited at
0

Comments

0 comments
Login to comment

Related

Why do I get this "cannot construct the infinite type: a ~ [a]" error in Haskell?

Compile error: Occurs check: cannot construct the infinite type: a1 = [a1]

Error on defining Applicative's <*> with record accessors: cannot construct the infinite type: t ~ t -> t1

Recursion in conditional definition in Haskell gives error ( Occurs check: cannot construct the infinite type:)

Cannot construct infinite type with functor

Haskell - cannot construct the infinite type

Haskell: cannot construct the infinite type

Split Function - cannot construct the infinite type

Haskell foldl cannot construct the infinite type

cannot construct the infinite type: e ~ [e]

cannot construct the infinite type when using foldl

Haskell Newbie - Occurs check: cannot construct the infinite type: a ~ [a]

haskell fibonacci - cannot construct the infinite type: a0 = [a0]

How can I deal with "Occurs check: cannot construct the infinite type: a0 = [a0]"?

Struggling to understand generics

Struggling to understand this recursive function

Struggling to understand groupby pandas

Python struggling to understand for loops with a list

Struggling to understand workflow for Java files

Haskell | Struggling to understand type declaration and abbreviations

Struggling to understand a specific behaviour of the is-operator

Struggling to understand what each input will evaluate to in python

Struggling to understand the Guardian API via Python

Struggling to understand the behavior of the window<-.ts function in R

Struggling to understand MVC in context on my Website code

Struggling to understand C++ template instantiation

Struggling to understand why my 'this' implementation is not working?

Struggling to understand the logic of interacting with my MySQL database

Scitkit PolynomialFeatures - struggling to understand higher degree results