Haskell 将多个参数缩写为单个变量
Haskell abbreviating multiple parameters to a single variable
所以我有一个 必须 具有特定类型的函数。我的实现类似于以下内容:
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
现在有什么方法可以将 t 值缩写为 更改验证然后重新排列 f 或类似以下内容:
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
filterFirst :: (a -> Bool) -> [a] -> [a]
-- Find all possible permutations
findAll :: [a] -> [[a]]
-- Basically Data.List (permutations)
问题是 f 必须将值作为参数传递。我一直在寻找,甚至一些接受任意数量参数并生成列表的函数也会有所帮助,但我似乎找不到任何具有此类功能的模块。
首先,让我们以一种看起来更接近于使用实际使用 t
值的函数组合 filterFirst
的部分应用程序的形式重写它:
f t1 t2 t3 t4 t5 t6 t7 t8 t9 = let cf = validate t1 t2 t3 t4 t5 t6 t7 t8 t9
in (flip filterFirst) (findAll [1..9]) cf
http://pointfree.io 然后告诉我们上面等价于
f = ((((((((flip filterFirst (findAll [1..9]) .) .) .) .) .) .) .) .) . validate
多层组合让我们避免在定义中重复t
个名字。
但是,我不会说这是对您的显式版本的改进。
所以我有一个 必须 具有特定类型的函数。我的实现类似于以下内容:
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
现在有什么方法可以将 t 值缩写为 更改验证然后重新排列 f 或类似以下内容:
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
filterFirst :: (a -> Bool) -> [a] -> [a]
-- Find all possible permutations
findAll :: [a] -> [[a]]
-- Basically Data.List (permutations)
问题是 f 必须将值作为参数传递。我一直在寻找,甚至一些接受任意数量参数并生成列表的函数也会有所帮助,但我似乎找不到任何具有此类功能的模块。
首先,让我们以一种看起来更接近于使用实际使用 t
值的函数组合 filterFirst
的部分应用程序的形式重写它:
f t1 t2 t3 t4 t5 t6 t7 t8 t9 = let cf = validate t1 t2 t3 t4 t5 t6 t7 t8 t9
in (flip filterFirst) (findAll [1..9]) cf
http://pointfree.io 然后告诉我们上面等价于
f = ((((((((flip filterFirst (findAll [1..9]) .) .) .) .) .) .) .) .) . validate
多层组合让我们避免在定义中重复t
个名字。
但是,我不会说这是对您的显式版本的改进。