Re: [Haskell-beginners] apply a function to all params of another function

On Mon, May 26, 2014 at 04:46:29PM +0200, Kees Bleijenberg wrote:
In have a lot of functions in a program with params that are alle Int's. f :: Int -> Int -> Int g :: Int -> Int -> Int h :: Int -> Int -> Int -> Int ... and convertParam :: Int -> Int -- say (+1) I want to call functions like f, g and h, but apply convertParam to all params first. f2 a b c = f (convertParam a) (convertParam b) (convertParam c)
I tried: run1p f conv = \a -> f (conv a) -- for functions with one param run2p f conv = \a b -> f (conv a) (conv b) -- for functions with two params . Can you do this in a more generalized way (using currying?) Any ideas?
class Convertible t where convert :: t -> t instance Convertible Int where ... instance Convertible t => Convertible (Int -> t) where ... I'll let you fill in the ... ! =) Note this only works well because the base case is Int. You could also add some extra base cases for other concrete return types. However, it is quite difficult to give a Convertible instance which applies to "all types which are not functions", so this will only work if you are willing to make one instance for each concrete result type your functions use. -Brent

Just a thought, you might want to use functions like (a,a,a)->a instead.
J
On Monday, May 26, 2014, Brent Yorgey
On Mon, May 26, 2014 at 04:46:29PM +0200, Kees Bleijenberg wrote:
In have a lot of functions in a program with params that are alle Int's. f :: Int -> Int -> Int g :: Int -> Int -> Int h :: Int -> Int -> Int -> Int ... and convertParam :: Int -> Int -- say (+1) I want to call functions like f, g and h, but apply convertParam to all params first. f2 a b c = f (convertParam a) (convertParam b) (convertParam c)
I tried: run1p f conv = \a -> f (conv a) -- for functions with one param run2p f conv = \a b -> f (conv a) (conv b) -- for functions with two params . Can you do this in a more generalized way (using currying?) Any ideas?
class Convertible t where convert :: t -> t
instance Convertible Int where ...
instance Convertible t => Convertible (Int -> t) where ...
I'll let you fill in the ... ! =) Note this only works well because the base case is Int. You could also add some extra base cases for other concrete return types. However, it is quite difficult to give a Convertible instance which applies to "all types which are not functions", so this will only work if you are willing to make one instance for each concrete result type your functions use.
-Brent _______________________________________________ Beginners mailing list Beginners@haskell.org javascript:; http://www.haskell.org/mailman/listinfo/beginners
-- Sent from an iPhone, please excuse brevity and typos.
participants (2)
-
Brent Yorgey
-
Julian Birch