Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

compare function and parameter in haskell [duplicate]

Possible Duplicate:
Comparing functions in Haskell

I'm learning haskell, wanted to know is something like this possible? All I want is to compare if parameter 'function' is equal to one of the functions a or b. How to do this?

Example code:

a,b :: Integer -> Integer
a x = x+1
b x = x-1

c function parameter = if function == a 
           then ... parameter -- Do a related stuff
           else ... parameter -- Do b related stuff
like image 441
mungurs Avatar asked Nov 04 '22 12:11

mungurs


1 Answers

The only case I know of where you can compare two functions for equality is if their domain has a finite number of values. For example, if you have two functions of type:

f, g :: Bool -> A

Then they are equal if they are equal for all inputs:

f == g = (f False == g False) && (f True == g True)

However, for the case of Int, comparing them on every possible value of Int is impractical and inefficient. For Integer, it can't be done since Integers are unbounded.

As @Miguel correctly pointed out in his comment, functions with non-finite domains cannot be compared for equality in general.

like image 194
Gabriella Gonzalez Avatar answered Nov 09 '22 07:11

Gabriella Gonzalez