2

I want to create a function apply that takes a function with an arbitrary amount of arguments as well as a list of integers, and returns the result of the function (Where each integer in the list is an argument in order.

I was thinking something like:

apply :: ([Int] -> Int) -> [Int] -> Int
apply f x:xs = apply (f x) xs
apply f [] = f

But I know this won't work because the type signature is wrong - the function doesn't take a list of ints, it just takes some amount of int arguments.

Additionally, when I get to the base case the f argument to apply should actually be an integer, violating the type signature anyway.

Does anyone know how to deal with this sort of problem?

Don Stewart
  • 137,316
  • 36
  • 365
  • 468
oadams
  • 3,019
  • 6
  • 30
  • 53

2 Answers2

11

I want to create a function apply that takes a function with an arbitrary amount of arguments as well as a list of integers,

Why do you want to do this? Perhaps your argument structure should be passed as a data structure, but so far you've over constrained the problem to ensure it won't produce an idiomatic Haskell solution.

Don Stewart
  • 137,316
  • 36
  • 365
  • 468
7

You can do it with some fancy type classes

{-# LANGUAGE FlexibleInstances #-}
-- for ApplyType (Int -> r)

class ApplyType t where
    apply :: t -> [Int] -> Int

instance ApplyType Int where
    apply f _ = f

instance (ApplyType r) => ApplyType (Int -> r) where
    apply f (x:xs) = apply (f x) xs

main :: IO ()
main = do print $ apply ((+) :: Int->Int->Int) [1, 2]
          print $ apply ((\x y z w -> x*y - z`div`w) :: Int->Int->Int->Int->Int) [3,5,8,2]
newacct
  • 119,665
  • 29
  • 163
  • 224
  • 1
    You just need FlexibleInstances and you can bypass the whole IsInt class and write ApplyType (Int -> r) directly. – Edward Kmett May 10 '10 at 17:11