English 中文(简体)
2. 分类类型
原标题:haskell type signature for integers
  • 时间:2012-01-13 04:13:58
  •  标签:
  • haskell

Say I want to write a function to decide whether a given integer number is prime, which type signature should I use?

  isPrime :: Int -> Bool

  isPrime :: (Integral a) => a -> Bool

What s the difference? Is there a particular reason to choose one over the other?
If so, in which situations should I use the two respectively?

最佳回答

<代码> Int -> Bool系指贵功能在的数值上运作。 Int, 即规模限制的分类账(最高尺寸为机器依赖)。

<代码>(Integral a) => a-> Bool 这意味着你的职能以 任何的数值为依据。 具有<代码>Integral的类型,即behave as integers in a particular way. 在具体类型上选择这种功能的主要原因是要建立更通用的职能。

使用<代码>Integral的通用表格,在您需要在其他情况下使用类似分类的类型时,往往最为有用。 Int -> a -> [a] 。 守则针对某些特定类型进行,目的是使用<条码>复制。 因此,需要改为<代码>。 Int first, or import genericReplicate from Data.List.

你可能想在你的案件中考虑的是<代码>Integer的类型,它代表了任意规模的分类。 由于你的主要目标是计算,因此支持任意综合类型的价值较低。

如果记忆为我服务,标准图书馆唯一的<代码>Integral的例子为。 Int and Integer anyhow. (EDIT): 由于汉玛在评论中提醒我,在<代码>Data.Int和Data.Word中也有固定尺寸类型的例子。 还有外国类型,如<代码>CInt,但我无视这些故意行为。

问题回答

我建议签署

isPrime :: Integer -> Bool

• 签署: Int -> Bool将排除对增量数字的快速测试,因为这些测试通常会过长(在技术方面,Integer,至少在integer-gmp提供的版本中是如此),但你很可能在出现这种情况之前将失去记忆long,因此我们可以维持一个定点编码的虚构。

• 犯罪 整体性a =>a-> Bool将是任何可行的执行。 一种实例是:<代码>Integral, 一种模型(Z[sqrt(2)](尽管这种类型的<代码>toInteger<>/code>是不信仰的),就这种类型而言,2个不是主要内容,如何发现有一般性检验?

Or consider finite types modeling a factor ring Z/(n). An Ord instance for such types would be incompatible with the arithmetic, but we already have that for Int etc. For example, in Z(6) = {0,1,2,3,4,5}, the primes would be 2, 3 and 4 (note that none of them is irreducible, 2 = 4*2, 3 = 3*3, 4 = 2*2).

因此,唯一有意义和可行的检验标准是,“它是一种合理(或自然)的主要产品,其价值在这种类型的范围之内? 在<代码>isPrime>的类型中(尽可能不牺牲速度太快)加以捕获。 Integer -> Bool, to be jointly with a to Integer/code> where appropriate.

许多初步性测试是假装的,需要随机编号,因此至少要达到其签字类型的最低水平:

seemsPrime :: (Integral a) => a -> [a] -> Bool

整体限制似乎是合理的,因为通常你不需要具体类型的限制,而只需要诸如<代码>rem等业务。





相关问题
Euler Problem in Haskell -- Can Someone Spot My Error

I m trying my hand at Euler Problem 4 in Haskell. It asks for that largest palindrome formed by multiplying two three-digit numbers. The problem was simple enough, and I thought my Haskell-fu was up ...

How does foldr work?

Can anybody explain how does foldr work? Take these examples: Prelude> foldr (-) 54 [10, 11] 53 Prelude> foldr (x y -> (x+y)/2) 54 [12, 4, 10, 6] 12.0 I am confused about these executions....

Efficient queue in Haskell

How can I efficiently implement a list data structure where I can have 2 views to the head and end of the list, that always point to a head a tail of a list without expensive calls to reverse. i.e: ...

Problem detecting cyclic numbers in Haskell

I am doing problem 61 at project Euler and came up with the following code (to test the case they give): p3 n = n*(n+1) `div` 2 p4 n = n*n p5 n = n*(3*n -1) `div` 2 p6 n = n*(2*n -1) p7 n = n*(5*n -3)...

Ways to get the middle of a list in Haskell?

I ve just started learning about Functional Programming, using Haskel. I m slowly getting through Erik Meijer s lectures on Channel 9 (I ve watched the first 4 so far) and in the 4th video Erik ...

haskell grouping problem

group :: Ord a => [(a, [b])] -> [(a, [b])] I want to look up all pairs that have the same fst, and merge them, by appending all the list of bs together where they have the same a and discarding ...

Closest equivalent to subprocess.communicate in Haskell

I want to do a popen() / python s subprocess.communicate from Haskell - start a program, give it stdin, and get its stdout/stderr. What s the most direct / Haskellish way to do this?

热门标签