0

I saw on a Haskell tutorial this function:

factorial :: (Integral a) => a -> a 

My question is why should I use that instead of this:

factorial :: Int->Int 

It's because with the first one we can use every type of numbers and in the second one we can only use an Int?

3
  • 8
    Yes. That's it. Commented Sep 22, 2015 at 21:26
  • You may want to use Integer at least Commented Sep 22, 2015 at 21:38
  • 2
    A detail: that type means you can use any type of integer numbers (that is, any type which is an instance of Integer). For instance, factorial 0.5 won't work. Commented Sep 22, 2015 at 21:39

1 Answer 1

2

This answer is a mashup of the comments to the question.

Your interpretation is correct, except for one detail: that type means you can use any type of integer numbers (that is, any type which is an instance of Integer). For instance, factorial 0.5 won't work, as expected for a factorial function.

Sign up to request clarification or add additional context in comments.

2 Comments

So if i put (Num a) instead of (Integral a) factorial 0.5 will work?
@paulo_ferreira, when you do that, and test it, what happens? Can you figure out why?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.