
I would like to have a function that can accept more than one input type and gives the same back as output. For the different eligible input types there would be different tests. An example of the idea would be something like: test :: a -> Bool -> a test 0 True = 0 test False True = False . . . is it obligatory that I create a typeclass for test and then instance each type that might go in the "a" slot? Because that seems like a lot of work. Let me also add a general thank to those of you who support the beginners' list. I learn something everyday.

On Wed, Mar 16, 2011 at 7:57 PM, Britt Anderson
I would like to have a function that can accept more than one input type and gives the same back as output. For the different eligible input types there would be different tests. An example of the idea would be something like:
test :: a -> Bool -> a
test 0 True = 0 test False True = False What is expected from: test 0 False or test "hello" False ?
is it obligatory that I create a typeclass for test and then instance each type that might go in the "a" slot? Because that seems like a lot of work.

On Wed, Mar 16, 2011 at 10:27:52AM -0400, Britt Anderson wrote:
I would like to have a function that can accept more than one input type and gives the same back as output. For the different eligible input types there would be different tests. An example of the idea would be something like:
test :: a -> Bool -> a
test 0 True = 0 test False True = False . . .
is it obligatory that I create a typeclass for test and then instance each type that might go in the "a" slot? Because that seems like a lot of work.
In a polymorphic function, you are not allowed to do different things depending on the type of a polymorphic input. Partly this is just theoretically elegant (see "parametricity" and "free theorems"), and practically speaking it means that types can be *erased* at compile time. At run time there is no way to see what type something is because all you have is a bunch of bits, with no type information! The only way to have a polymorphic function which does something different for different types is to use a type class. -Brent
participants (3)
-
Brent Yorgey
-
Britt Anderson
-
Karthick Gururaj