您的where 的范围在列表理解之外。在列表理解之外 c 和 d 确实没有定义。
但我认为你把这个问题弄得太复杂了。您可以简单地将添加内容放在列表理解的开头:
convertingList :: (Int,Int) -> [(Int,Int)] -> [(Int,Int)]
convertingList (a,b) list = [(a + c, b + d) | (c,d) <- list]
或者你可以使用let 表达式:
convertingList :: (Int,Int) -> [(Int,Int)] -> [(Int,Int)]
convertingList (a,b) list = [let x = a+c; y = b+d in (x, y) | (c,d) <- list]
或者像@M.Aroosi 所说,我们可以将lets(不带in!)移动到列表理解的正文部分:
convertingList :: (Int,Int) -> [(Int,Int)] -> [(Int,Int)]
convertingList (a,b) list = [ (x, y) | (c,d) <- list, let x = a+c, let y = b+d]
另一种选择是定义一个映射函数,将a 和b 添加到元素中,例如:
convertingList :: (Int,Int) -> [(Int,Int)] -> [(Int,Int)]
convertingList (a,b) = map f
where f (c, d) = (a+c, b+d)
或(***) :: Arrow a => a b c -> a b' c' -> a (b, b') (c, c'):
import Control.Arrow((***))
convertingList :: (Int,Int) -> [(Int,Int)] -> [(Int,Int)]
convertingList (a,b) = map ((***) (a+) (b+))