Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/haskell/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Haskell将多个参数简化为单个变量_Haskell_Functional Programming_Currying - Fatal编程技术网

Haskell将多个参数简化为单个变量

Haskell将多个参数简化为单个变量,haskell,functional-programming,currying,Haskell,Functional Programming,Currying,所以我有一个函数,它必须有一个特定的类型。我的实现类似于以下内容: f :: Int -> Int -> Int -> Int -> Int -> Int -> Int -> Int -> Int -> Int f t1 t2 t3 t4 t5 t6 t7 t8 t9 = filterFirst checkFunc p where p = findAll [1..9] checkFunc = v

所以我有一个函数,它必须有一个特定的类型。我的实现类似于以下内容:

f :: Int -> Int -> Int -> Int -> Int -> Int -> Int -> Int -> Int -> Int
f t1 t2 t3 t4 t5 t6 t7 t8 t9
    = filterFirst checkFunc p
    where
        p = findAll [1..9]
        checkFunc = validate t1 t2 t3 t4 t5 t6 t7 t8 t9
f :: Int -> Int -> Int -> Int -> Int -> Int -> Int -> Int -> Int -> Int
f ts
    = filterFirst checkFunc p
    where
        p = findAll [1..9]
        checkFunc = validate ts
现在是否有任何方法可以将t值缩写为change validate,然后重新排列f或类似的内容,如下所示:

f :: Int -> Int -> Int -> Int -> Int -> Int -> Int -> Int -> Int -> Int
f t1 t2 t3 t4 t5 t6 t7 t8 t9
    = filterFirst checkFunc p
    where
        p = findAll [1..9]
        checkFunc = validate t1 t2 t3 t4 t5 t6 t7 t8 t9
f :: Int -> Int -> Int -> Int -> Int -> Int -> Int -> Int -> Int -> Int
f ts
    = filterFirst checkFunc p
    where
        p = findAll [1..9]
        checkFunc = validate ts
一种让它看起来更干净的方法将是惊人的

编辑:更多细节

validate :: Int -> Int -> Int -> Int -> Int -> Int -> Int -> Int -> Int -> [Int] -> Bool
validate t1 t2 t3 t4 t5 t6 t7 t8 t9 is =
    [t1, t2, t3, t4, t5, t6, t7, t8, t9] == sums is

-- Calculates sums from specific indexes in list
sums :: [Int] -> [Int]

-- from https://stackoverflow.com/a/28904773/1218369
filterFirst :: (a -> Bool) -> [a] -> [a]

-- Find all possible permutations
findAll :: [a] -> [[a]]
-- Basically Data.List (permutations)

问题是f必须将值作为参数传递。我在寻找一些函数,甚至一些函数可以接受任意数量的参数并生成一个列表,这可能会有所帮助,但我似乎找不到任何具有此类函数的模块。

首先,让我们将其改写为一种更接近于组成filterFirst的部分应用程序的形式,其中包含一个实际使用t值的函数:

然后告诉我们,以上是等价的

f = ((((((((flip filterFirst (findAll [1..9]) .) .) .) .) .) .) .) .) . validate
多层组合允许我们避免在定义中重复t名称


但是,我不认为这是对显式版本的改进。

请提供一个您希望简化的代码的示例。Int->之间的同构是。。。Int->Int和Int,…,Int->Int不会比你现在做的更漂亮。在这个过程中的某个地方,一个糟糕的设计决策导致你需要f来拥有那种类型。这就是我所想的。我没有为f做设计决定,但必须与之合作。我只是希望有一种方法可以像在其他语言中一样选择所有参数,或者使用validate公式化一个函数,我可以用它来定义f,而不需要指定参数。