Other instances of Integral
Hello, I would like to ask wheter there are other instances of the class Integral? And I would like to ask what the difference is between the following functions: SomeFunctionA :: (Integral a) => a -> a, SomeFunctionB (Integer a) => a -> a, SomeFunctionC (Int a) => a -> a. What I do know is, that the Int can have underflow and overflows, however I don't actually see the difference (and I can't really find a difference on Google as it gives me results that aren't really relevant to my question) between prefering to use an Integral a when I've already got an Integer. (Assuming I didn't missed the definiton of a Integral, which has by definition (I looked it up on Google: http://www.zvon.org/other/haskell/Outputprelude/Integral_c.html) has two instances) What I also know is that operatons on a Int is faster than operations on a Integral or Integer. Can someone explain to me what kind of advantages and disadvantages I would get when substituting SomeFunctionB for someFunctionA? Thank you for your help!
Tsunkiet Man wrote:
Hello,
I would like to ask wheter there are other instances of the class Integral?
Lots of them. You can define a few of them yourself, you know.
And I would like to ask what the difference is between the following functions: SomeFunctionA :: (Integral a) => a -> a,
Perfectly correct.
SomeFunctionB (Integer a) => a -> a, SomeFunctionC (Int a) => a -> a.
Both incorrect. Integral is a class, but Integer and Int are types.
What I do know is, that the Int can have underflow and overflows, however I don't actually see the difference (and I can't really find a difference on Google as it gives me results that aren't really relevant to my question) between prefering to use an Integral a when I've already got an Integer.
Sorry, didn't understand your question.
(Assuming I didn't missed the definiton of a Integral, which has by definition (I looked it up on Google: http://www.zvon.org/other/haskell/Outputprelude/Integral_c.html) has two instances)
No. It makes no sense to say "by definition ... has that number of instances". Instances of class are not included in it's definition and could be defined separately.
Can someone explain to me what kind of advantages and disadvantages I would get when substituting SomeFunctionB for someFunctionA?
Working program would certainly be an advantage.
Thank you for your help!
------------------------------------------------------------------------
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
2009/11/27 Miguel Mitrofanov <miguelimo38@yandex.ru>
Tsunkiet Man wrote:
Hello, I would like to ask wheter there are other instances of the class Integral?
Lots of them. You can define a few of them yourself, you know.
Yes, I knew that =).
And I would like to ask what the difference is between the following
functions: SomeFunctionA :: (Integral a) => a -> a,
Perfectly correct.
SomeFunctionB (Integer a) => a -> a, SomeFunctionC (Int a) => a -> a.
Both incorrect.
Integral is a class, but Integer and Int are types.
What I do know is, that the Int can have underflow and overflows, however I
don't actually see the difference (and I can't really find a difference on Google as it gives me results that aren't really relevant to my question) between prefering to use an Integral a when I've already got an Integer.
Sorry, didn't understand your question.
Was not really a question ^_^
(Assuming I didn't missed the definiton of a Integral, which has by definition (I looked it up on Google: http://www.zvon.org/other/haskell/Outputprelude/Integral_c.html) has two instances)
No. It makes no sense to say "by definition ... has that number of instances". Instances of class are not included in it's definition and could be defined separately.
Ah I see!
Can someone explain to me what kind of advantages and disadvantages I would
get when substituting SomeFunctionB for someFunctionA?
Working program would certainly be an advantage.
What if it both works? Should I take the "highest level"?
Thank you for your help!
------------------------------------------------------------------------
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
This is actually all related to my homework problem which I can't seem to get a usefull answer out. Cause it tells me that a definition with Integrals would be much more general. It can support other instances of the class Integral. And then it asks me what the difference the definition has, actually it has none. Because I only use functions in de Prelude that are defined for Integrals.
Correct me if I'm wrong with this =). I don't really understand how I should answer this homework question as it makes no sense in my opinion give me the answer and then asking me the question xD. Thanks for your quick response!
I have not understood what the question is. Are you asking "Why would one need functions of the form (Integral a) => ...a... if one can just use ...Integer... or ...Int... explicitly"? 2009/11/27 Tsunkiet Man <temp.tsun@gmail.com>:
2009/11/27 Miguel Mitrofanov <miguelimo38@yandex.ru>
Tsunkiet Man wrote:
Hello, I would like to ask wheter there are other instances of the class Integral?
Lots of them. You can define a few of them yourself, you know.
Yes, I knew that =).
And I would like to ask what the difference is between the following functions: SomeFunctionA :: (Integral a) => a -> a,
Perfectly correct.
SomeFunctionB (Integer a) => a -> a, SomeFunctionC (Int a) => a -> a.
Both incorrect.
Integral is a class, but Integer and Int are types.
What I do know is, that the Int can have underflow and overflows, however I don't actually see the difference (and I can't really find a difference on Google as it gives me results that aren't really relevant to my question) between prefering to use an Integral a when I've already got an Integer.
Sorry, didn't understand your question.
Was not really a question ^_^
(Assuming I didn't missed the definiton of a Integral, which has by definition (I looked it up on Google: http://www.zvon.org/other/haskell/Outputprelude/Integral_c.html) has two instances)
No. It makes no sense to say "by definition ... has that number of instances". Instances of class are not included in it's definition and could be defined separately.
Ah I see!
Can someone explain to me what kind of advantages and disadvantages I would get when substituting SomeFunctionB for someFunctionA?
Working program would certainly be an advantage.
What if it both works? Should I take the "highest level"?
Thank you for your help!
------------------------------------------------------------------------
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
This is actually all related to my homework problem which I can't seem to get a usefull answer out. Cause it tells me that a definition with Integrals would be much more general. It can support other instances of the class Integral. And then it asks me what the difference the definition has, actually it has none. Because I only use functions in de Prelude that are defined for Integrals.
Correct me if I'm wrong with this =).
I don't really understand how I should answer this homework question as it makes no sense in my opinion give me the answer and then asking me the question xD.
Thanks for your quick response!
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
-- Eugene Kirpichov Web IR developer, market.yandex.ru
I have not understood what the question is. Are you asking "Why would one need functions of the form (Integral a) => ...a... if one can just use ...Integer... or ...Int... explicitly"?
To clarify, the problem Tsun is asking about is Ex. 9 (bonus) in http://www.cs.uu.nl/wiki/FP/Practicum#PracticumOne . Define the following function toDigitsRevG :: (Integral a) => a -> a -> [a] that takes a base and a value and outputs the reverse list of digits. The function must be total (modulo bottom) and thus work for any instance of Integral. This is a first-year course, so I expect this to be a difficult problem for those seeing Haskell for the first time. That's why it's a bonus exercise. ;) Regards,
Integer is a type. It has members like 5, -3, and 1095823705763209723486723782. Int is a type. 5 and -3 can also both be Ints, but that last number can't be; it's too big. Word32 is a type. 5 can also be a Word32, but neither of the other numbers can be (it's an unsigned type so there are no negative numbers). All of these are instances of Integral, The main thing that Integral requires you to provide is "toInteger"; that is, your type must be a subset of Integer, because you need to be able to convert it to an Integer on demand. If you have a function f :: Integer -> Integer, it's possible to make it into a function of type (Integral a) => a -> a. Exercise: write this function: liftIntegral :: Integral a => (Integer -> Integer) -> a -> a liftIntegral f x = ? "fromInteger" and "toInteger" might be useful to you here. Of course, this is likely not as efficient as operating on the argument directly (using the operations provided by the type classes Eq, Show, Num, Ord, Real, Enum, and Integral), but it works :) -- ryan On 11/2d7/09, Tsunkiet Man <temp.tsun@gmail.com> wrote:
2009/11/27 Miguel Mitrofanov <miguelimo38@yandex.ru>
Tsunkiet Man wrote:
Hello, I would like to ask wheter there are other instances of the class Integral?
Lots of them. You can define a few of them yourself, you know.
Yes, I knew that =).
And I would like to ask what the difference is between the following
functions: SomeFunctionA :: (Integral a) => a -> a,
Perfectly correct.
SomeFunctionB (Integer a) => a -> a, SomeFunctionC (Int a) => a -> a.
Both incorrect.
Integral is a class, but Integer and Int are types.
What I do know is, that the Int can have underflow and overflows, however I
don't actually see the difference (and I can't really find a difference on Google as it gives me results that aren't really relevant to my question) between prefering to use an Integral a when I've already got an Integer.
Sorry, didn't understand your question.
Was not really a question ^_^
(Assuming I didn't missed the definiton of a Integral, which has by definition (I looked it up on Google: http://www.zvon.org/other/haskell/Outputprelude/Integral_c.html) has two instances)
No. It makes no sense to say "by definition ... has that number of instances". Instances of class are not included in it's definition and could be defined separately.
Ah I see!
Can someone explain to me what kind of advantages and disadvantages I would
get when substituting SomeFunctionB for someFunctionA?
Working program would certainly be an advantage.
What if it both works? Should I take the "highest level"?
Thank you for your help!
------------------------------------------------------------------------
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
This is actually all related to my homework problem which I can't seem to get a usefull answer out. Cause it tells me that a definition with Integrals would be much more general. It can support other instances of the class Integral. And then it asks me what the difference the definition has, actually it has none. Because I only use functions in de Prelude that are defined for Integrals.
Correct me if I'm wrong with this =).
I don't really understand how I should answer this homework question as it makes no sense in my opinion give me the answer and then asking me the question xD.
Thanks for your quick response!
participants (5)
-
Eugene Kirpichov -
Miguel Mitrofanov -
Ryan Ingram -
Sean Leather -
Tsunkiet Man