Reputation: 103
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.
Upvotes: 0
Views: 77
Reputation: 103
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])
Upvotes: 1