繁体   English   中英

努力理解“无法构造无限类型”错误

[英]Struggling to understand a “Cannot construct the infinite type” error

我正在尝试解决 HackerRank 问题,但遇到了一个我无法弄清楚的错误。 问题在solve1中。 确切的错误是: 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)' 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)' 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)'

我一直在查看这些类型,它们在我眼中仍然看起来是正确的。 tripList接受一个数字列表并返回一个三元组的数字列表。 tripleMap将三组数字列表作为其第二个参数。

在我的 REPL 中测试tripList时,我得到了想要的结果:

> tripList [1,0,-1,0,1] 
([1,1],[0,0],[-1])

这是我的代码:

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)

您的foldSolution (length' xs)需要ta并返回a 因此,当您在tripleMap tripList xs (类型为([a], [a], [a]) )上对其进行三重映射时,您会得到一个类型为(a, a, a)的元组,而不是([a], [a], [a]) ,这就是你得到错误的原因。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM