
Hi there, is there a simple way to carry out the following type of conversion? Suppose I have a list (finite) of (value,image) pairs such as: list = [(0,1),(1,0)]
From this I want to generate a function
f 0 = 1 f 1 = 0 Is there a way to do this? Note, I don't want to define a function that searches through the list each time it is invoked, I want to generate the function once and have it be as fast as the pattern-matcher can make it. So, I'm looking for a function g :: [(a,b)] -> a -> b Is this simple to do? Thanks, Carl

Is there a way to do this? Note, I don't want to define a function that searches through the list each time it is invoked, I want to generate the function once and have it be as fast as the pattern-matcher can make it.
Actually, would the pattern-matcher be doing little more than a linear search through list in this case? Thanks, Carl

Use an array. Don't remember for sure, but I think something like q = array (0, max (map fst list)) list f = (q!!) will probably work. On Fri, May 24, 2002, Carl McTague wrote:
Hi there,
is there a simple way to carry out the following type of conversion? Suppose I have a list (finite) of (value,image) pairs such as:
list = [(0,1),(1,0)]
From this I want to generate a function
f 0 = 1 f 1 = 0
Is there a way to do this? Note, I don't want to define a function that searches through the list each time it is invoked, I want to generate the function once and have it be as fast as the pattern-matcher can make it.
So, I'm looking for a function g :: [(a,b)] -> a -> b
Is this simple to do?
Thanks, Carl _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
-- Night. An owl flies o'er rooftops. The moon sheds its soft light upon the trees. David Feuer

If you can live with f's domain being ordered, I'd probably use something like f = lookupWithDefaultFM (listToFM list) (-1) importing FiniteMap from ghc's libraries. HTH. -- Mark

Maybe using some sort of binary search (using an array or another apropiate
data structure) or dictionary search, would make o(log n) time complexity
function, which could be actually faster than the pattern matcher.
Cheers,
Luis
PD: This is very similar to the asociatives data types in lenguages like
perl or phyton, but don“t know what solution have been used in those
languages.
----- Original Message -----
From: Carl McTague
Is there a way to do this? Note, I don't want to define a function that searches through the list each time it is invoked, I want to generate the function once and have it be as fast as the pattern-matcher can make it.
Actually, would the pattern-matcher be doing little more than a linear search through list in this case?
Thanks, Carl
participants (4)
-
A./C. Luis Pablo Michelena
-
Carl McTague
-
David Feuer
-
Mark Carroll