(Integral a) => a vs Integer

Hi all- I'm going through Learn You a Haskell for Great Good and I don't understand the advantage of using (Integral a)=> a vs just Integer as I show below. Could someone explain this to me? Thanks, Dan lucky :: (Integral a) => a -> String lucky 7 = "LUCKY NUMBER SEVEN!" lucky x = "Sorry, you're out of luck, pal!" lucky :: Integer -> String lucky 7 = "LUCKY NUMBER SEVEN!" lucky x = "Sorry, you're out of luck, pal!"

On Tue, Jul 12, 2011 at 7:20 PM, Dan Ross
Hi all-
I'm going through Learn You a Haskell for Great Good and I don't understand the advantage of using (Integral a)=> a vs just Integer as I show below.
Could someone explain this to me?
Thanks,
Dan
lucky :: (Integral a) => a -> String lucky 7 = "LUCKY NUMBER SEVEN!" lucky x = "Sorry, you're out of luck, pal!"
lucky :: Integer -> String lucky 7 = "LUCKY NUMBER SEVEN!" lucky x = "Sorry, you're out of luck, pal!"
Hi Dan, The first version is polymorphic, and will work on *any* instance of Integral. This would allow people to use Int, Int32, Int64, etc. The second version requires the user to pass in specifically an Integer. Michael

Ah. Because Integer is an instance of Integral right? So using Integer would be more restrictive. On Tue, 12 Jul 2011 19:22:36 +0300, Michael Snoyman wrote:
On Tue, Jul 12, 2011 at 7:20 PM, Dan Ross
wrote: Hi all-
I'm going through Learn You a Haskell for Great Good and I don't understand the advantage of using (Integral a)=> a vs just Integer as I show below.
Could someone explain this to me?
Thanks,
Dan
lucky :: (Integral a) => a -> String lucky 7 = "LUCKY NUMBER SEVEN!" lucky x = "Sorry, you're out of luck, pal!"
lucky :: Integer -> String lucky 7 = "LUCKY NUMBER SEVEN!" lucky x = "Sorry, you're out of luck, pal!"
Hi Dan,
The first version is polymorphic, and will work on *any* instance of Integral. This would allow people to use Int, Int32, Int64, etc. The second version requires the user to pass in specifically an Integer.
Michael

Precisely.
On Tue, Jul 12, 2011 at 7:44 PM, Dan Ross
Ah. Because Integer is an instance of Integral right?
So using Integer would be more restrictive.
On Tue, 12 Jul 2011 19:22:36 +0300, Michael Snoyman wrote:
On Tue, Jul 12, 2011 at 7:20 PM, Dan Ross
wrote: Hi all-
I'm going through Learn You a Haskell for Great Good and I don't understand the advantage of using (Integral a)=> a vs just Integer as I show below.
Could someone explain this to me?
Thanks,
Dan
lucky :: (Integral a) => a -> String lucky 7 = "LUCKY NUMBER SEVEN!" lucky x = "Sorry, you're out of luck, pal!"
lucky :: Integer -> String lucky 7 = "LUCKY NUMBER SEVEN!" lucky x = "Sorry, you're out of luck, pal!"
Hi Dan,
The first version is polymorphic, and will work on *any* instance of Integral. This would allow people to use Int, Int32, Int64, etc. The second version requires the user to pass in specifically an Integer.
Michael

Cool. Thanks. On Tue, 12 Jul 2011 19:57:38 +0300, Michael Snoyman wrote:
Precisely.
On Tue, Jul 12, 2011 at 7:44 PM, Dan Ross
wrote: Ah. Because Integer is an instance of Integral right?
So using Integer would be more restrictive.
On Tue, 12 Jul 2011 19:22:36 +0300, Michael Snoyman wrote:
On Tue, Jul 12, 2011 at 7:20 PM, Dan Ross
wrote: Hi all-
I'm going through Learn You a Haskell for Great Good and I don't understand the advantage of using (Integral a)=> a vs just Integer as I show below.
Could someone explain this to me?
Thanks,
Dan
lucky :: (Integral a) => a -> String lucky 7 = "LUCKY NUMBER SEVEN!" lucky x = "Sorry, you're out of luck, pal!"
lucky :: Integer -> String lucky 7 = "LUCKY NUMBER SEVEN!" lucky x = "Sorry, you're out of luck, pal!"
Hi Dan,
The first version is polymorphic, and will work on *any* instance of Integral. This would allow people to use Int, Int32, Int64, etc. The second version requires the user to pass in specifically an Integer.
Michael
participants (2)
-
Dan Ross
-
Michael Snoyman