Haskell:无法预期类型'Integer'与实际类型'Int' [英] Haskell: Couldn't expected type 'Integer' with actual type 'Int'

查看:390
本文介绍了Haskell:无法预期类型'Integer'与实际类型'Int'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 <$ c我一直盯着这段代码已经有相当长的一段时间了,我无法理解这个错误信息。 $ c> divisors :: Integer  - > [整数] 
除数n = [t | t < - [1..n],mod n t == 0]

length':: [a] - >整数
长度'[] = 0
长度'(x:xs)= 1 +长度'xs

divLengths :: [(Integer,Integer)]
divLengths = [(n,length'(divisors n))| n< - [1 ..]]

divLengths':: [Integer]
divLengths'= [length'(divisors n)| n < - [1 ..]]

hcn :: [Integer]
hcn = [n | n < - [1 ..],max(取n divLengths')==长度'(divisors n)]

divisors接受一个I​​nteger并返回一个包含所有除数的列表。

length'与内置的length ,只有它返回一个Integer。

divLengths是一个Integer元组的无限列表和它的除数的数目。



divLengths'只会返回数字的除数。



hcn应该是一个高度复合数字的无限列表如果除数的数目与所有数字的所有除数的最大值相同(直到被检查的数字))。

然而,我得到这个错误当试图在ghci中加载.hs时:

 无法匹配实际类型为'Int'的预期类型'Integer'
在'divisors'的第一个参数中,即'n'
在长度的第一个参数'中,即'(divisors n)'
在第二个参数(==)',即'length'(divisors n)'

帮助我在这里?



最好的问候,
Lukas

解决方案

问题是 take 需要 Int ,所以GHC推断出 n 必须是 Int 。没问题,你可以用 fromIntegral 在任何整型之间进行转换。



< c $ c> max 这应该有两个参数。你可能打算使用最大值,它取得一个列表。



试试这样:

  hcn :: [Integer] 
hcn = [n | n < - [1 ..],最大(取(从整数n)divLengths')==长度'(divisors n)]


I've been staring at this code for quite some time now and I can't get my head around that error message.

divisors :: Integer -> [Integer]
divisors n = [t | t <- [1..n], mod n t == 0]

length' :: [a] -> Integer
length' []      = 0
length' (x:xs)  = 1 + length' xs

divLengths :: [(Integer, Integer)]
divLengths = [(n, length' (divisors n)) | n <- [1..]]

divLengths' :: [Integer]
divLengths' = [length' (divisors n) | n <- [1..]]

hcn :: [Integer]
hcn = [n | n <- [1..], max (take n divLengths') == length' (divisors n)]

"divisors" takes an Integer and returns a list with all its divisors.

"length'" is the same as the built-in "length", only it returns an Integer.

"divLengths" is an infinite list of tuples of a Integer and the number of its divisors.

"divLengths'" only returns the number of divisors of the numbers.

"hcn" is supposed to be an infinite list of highly composite numbers (if the number of divisors is the same as the maximum of all numbers of divisors of all numbers (up to the number being checked)).

However, i get this error when trying to load the .hs in ghci:

Couldn't match expected type `Integer' with actual type `Int'
In the first argument of `divisors', namely `n'
In the first argument of length', namely `(divisors n)'
In the second argument of `(==)', namely `length' (divisors n)'

Can you please help me out here?

Best regards, Lukas

解决方案

The problem is that take takes an Int, so GHC infers from that that n must be Int. No problem, you can use fromIntegral to convert between any integral types.

There is also another problem with max which is supposed to take two arguments. You probably meant to use maximum, which takes a list instead.

Try something like this:

hcn :: [Integer]
hcn = [n | n <- [1..], maximum (take (fromIntegral n) divLengths') == length' (divisors n)]

这篇关于Haskell:无法预期类型'Integer'与实际类型'Int'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆