简体   繁体   English

我可以在Haskell中使用一系列函数作为输入吗?

[英]Can I use a list of functions, as input, for a different function in Haskell?

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: 例如,假设我像这样创建了一个名为Func10的类型同义词:

type Func10 = Int -> Int

I can create a set of functions that add, subtract, divide or multiply a value to 10 like so: 我可以创建一组将值加,减,除或乘以10的函数,如下所示:

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. 现在,假设我要创建一个函数,该函数将包含4个值的列表以及要应用于它们的函数的列表:add10,subtract10,multipli10和divid10。

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需要整数输入作为调用test_function的一部分,如下所示:

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. 谢谢。

Like Zpalmtree said in the comments, the error was not using !! 就像Zpalmtree在评论中说的那样,该错误未使用!! 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])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM