Haskell 如何将分部函数应用于元组列表

Haskell 如何将分部函数应用于元组列表,haskell,Haskell,这个问题的标题听起来可能有误导性,但我不知道如何才能解释我想做的事情 我正在试验非确定性数据结构。我试图生成一组部分应用运算符和一组元组的所有可能组合: -- test.hs makeTupleList :: (Int, Int) -> [(Int, Int)] makeTupleList (a, b) = ZipList [(+2), (-2)] <*> ZipList [a, b] 但我显然做错了什么,因为我一直在犯这样的错误: Couldn't match type

这个问题的标题听起来可能有误导性,但我不知道如何才能解释我想做的事情

我正在试验非确定性数据结构。我试图生成一组部分应用运算符和一组元组的所有可能组合:

-- test.hs
makeTupleList :: (Int, Int) -> [(Int, Int)] 
makeTupleList (a, b) = ZipList [(+2), (-2)] <*> ZipList [a, b]
但我显然做错了什么,因为我一直在犯这样的错误:

Couldn't match type `ZipList' with `[]'

如何获得所需的结果?

您声明的类型与函数实际拥有的类型不匹配

让我们检查一下函数的主体

*> :t \(a, b) -> ZipList [(+2), (-2)] <*> ZipList [a, b]
\(a, b) -> ZipList [(+2), (-2)] <*> ZipList [a, b]
  :: (Num (b -> b), Num b) => (b, b) -> ZipList b
好的,我们将其更改为显式lambda,并进一步:

*> :t \(a, b) -> ZipList [(+2), (\x -> x - 2)] <*> ZipList [a, b]
\(a, b) -> ZipList [(+2), (\x -> x - 2)] <*> ZipList [a, b]
  :: Num b => (b, b) -> ZipList b
这似乎就做到了

*> makeTupleList (10,20)
[(12,22),(12,18),(8,22),(8,18)]

您声明的类型与函数实际拥有的类型不匹配

让我们检查一下函数的主体

*> :t \(a, b) -> ZipList [(+2), (-2)] <*> ZipList [a, b]
\(a, b) -> ZipList [(+2), (-2)] <*> ZipList [a, b]
  :: (Num (b -> b), Num b) => (b, b) -> ZipList b
好的,我们将其更改为显式lambda,并进一步:

*> :t \(a, b) -> ZipList [(+2), (\x -> x - 2)] <*> ZipList [a, b]
\(a, b) -> ZipList [(+2), (\x -> x - 2)] <*> ZipList [a, b]
  :: Num b => (b, b) -> ZipList b
这似乎就做到了

*> makeTupleList (10,20)
[(12,22),(12,18),(8,22),(8,18)]

我会放下拉链:

makeTupleList :: (Int, Int) -> [(Int, Int )]
makeTupleList (a,b) = (,) <$> aList <*> bList
 where
  functionList :: [Int -> Int]
  functionList = [flip (-) 2  ,(+ 2) ]
  aList :: [Int]
  aList = functionList <*> [a]
  bList = functionList <*> [b]

ZipList用于不从应用程序操作生成置换

我会放下拉链:

makeTupleList :: (Int, Int) -> [(Int, Int )]
makeTupleList (a,b) = (,) <$> aList <*> bList
 where
  functionList :: [Int -> Int]
  functionList = [flip (-) 2  ,(+ 2) ]
  aList :: [Int]
  aList = functionList <*> [a]
  bList = functionList <*> [b]

ZipList用于不从应用程序操作生成置换

是什么促使你使用
ZipList
呢?顺便提一下,一个术语:通常这些被称为“部分应用函数”,而不是“部分函数”,“部分函数”是为可能永远抛出错误或循环的函数保留的。是什么促使你使用
ZipList
,术语:通常这些被称为“部分应用函数”,而不是“部分函数”,“部分函数”是为可能永远抛出错误或循环的函数保留的。
flip(-)2
subtract 2
。有没有办法将
()
函数不使用
flip
或类似
(+(-2))
lambda包装器工作正常,可能是最清晰的。
flip(-)2
减去2
。有没有一种方法可以在不使用
flip
或类似
(+(-2))的情况下实现
函数
lambda包装器工作正常,可能是最清晰的。