0

I am struggling to figure out how to make a function that takes a list of functions as input to generate some output. For example, let's say that I make a type synonym called Func10 like so:

type Func10 = Int -> Int

I can create a set of functions that add, subtract, divide or multiply a value to 10 like so:

add10 :: Func10
add10 input = 10 + input

subtract10 :: Func10
subtract10 input = 10 - input

times10 :: Func10
times10 input = 10 * input

divide10 :: Func10
divide10 input = 10 `div` input

Now let's say that I want to make a function that will take a list of 4 values and a list of functions that I want to apply to them: add10, subtract10, multiply10 and divide10.

Initially I thought that I could give the function in the list input the integer argument when I need it, like so:

test_function :: [Func10] -> [Int] -> Int
test_function function input = function[0] (input[1])

main = do
    print("print 5 add 10")
    print(test_function [add10, subtract10] [3,5,7,9])

This resulted in the following error:

function_lists2.hs:17:32: error:
    * Couldn't match expected type `[Integer] -> t0 -> Int'
                  with actual type `[Func10]'
    * The function `function' is applied to two arguments,
      but its type `[Func10]' has none
      In the expression: function [0] (input [1])
      In an equation for `test_function':
          test_function function input = function [0] (input [1])
   |
17 | test_function function input = function[0] (input[1])
   |                                ^^^^^^^^^^^^^^^^^^^^^^

function_lists2.hs:17:45: error:
    * Couldn't match expected type `[Integer] -> t0'
                  with actual type `[Int]'
    * The function `input' is applied to one argument,
      but its type `[Int]' has none
      In the second argument of `function', namely `(input [1])'
      In the expression: function [0] (input [1])
   |
17 | test_function function input = function[0] (input[1])
   |                                             ^^^^^^^^

I thought that this could be because test_function needs the integer input as part of calling test_function, like so:

test_function :: [Func10] -> Int
test_function function = function[0]

main = do
    print("print 5 add 10")
    print(test_function ([add10, subtract10] 5))

However, this resulted in a similar error:

function_lists2.hs:26:26: error:
    * Couldn't match expected type `[Integer] -> Int'
                  with actual type `[Func10]'
    * The function `function' is applied to one argument,
      but its type `[Func10]' has none
      In the expression: function [0]
      In an equation for `test_function':
          test_function function = function [0]
   |
26 | test_function function = function[0]
   |                          ^^^^^^^^^^^

function_lists2.hs:30:30: error:
    * Couldn't match expected type `Integer -> [Func10]'
                  with actual type `[Func10]'
    * The function `[add10, subtract10]' is applied to one argument,
      but its type `[Func10]' has none
      In the first argument of `test_function', namely
        `([add10, subtract10] 5)'
      In the first argument of `print', namely
        `(test_function ([add10, subtract10] 5))'
   |
30 |         print(test_function ([add10, subtract10] 5))
   |                              ^^^^^^^^^^^^^^^^^^^^^

What would be the correct way of doing this? I am trying to take a list of functions and apply them to some other values but it's hard to find information on this topic online. Thanks.

4
  • 4
    To take an item from a list you need to use !!. Your first function would be written like testfunction function input = (function !! 0) (input !! 1) but generally manually taking items from lists is a bad idea, you should pattern match or loop through the list Commented Nov 18, 2017 at 22:24
  • 1
    why are you using list in this way... i never had the need to index a list, so what are you trying to do here... Commented Nov 18, 2017 at 22:25
  • Ah yes, @Zpalmtree you're right. I completely forgot that to call an nth index form a list you use !!. That solves the error. HuStmpHrrr I was trying to create some AI "players" and I needed to use the list of players in a functioni that would calculate the next state of the program. Commented Nov 18, 2017 at 22:33
  • What result exactly you want test_function [add10, subtract10] [3,5,7,9] to return? Commented Nov 19, 2017 at 16:37

1 Answer 1

1

Like Zpalmtree said in the comments, the error was not using !! to call indexes. The correct way to write the code was to have it written out like so:

test_function :: [Func10] -> [Int] -> Int
test_function function input = (function !! 0) (input !! 1)

main = do
    print("print 5 add 10")
    print(test_function [add10, subtract10] [3,5,7,9])
Sign up to request clarification or add additional context in comments.

1 Comment

or (function <*> input) !! (0*4 + 1).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.