我正在尝试创建一个列出另一个列表的差异的函数.因此,对于[1,3,7,11],它将返回[2,4,4].我正在尝试使用列表理解,但我遇到了我想要使用的函数类型的问题.是否可以通过将[t]转换为[int]并再次转换为[t]来保持此格式?
{ difflist x y = [ p - q | p<- [x..y],
q<- [ ( [1..(length [x..y]) ] !! [x..y] ): []]] }
<interactive>:200:70: error:
• Couldn't match expected type ‘Int’ with actual type ‘[[Int]]’
• In the second argument of ‘(!!)’, namely ‘[x .. y]’
In the first argument of ‘(:)’, namely
‘([1 .. (length [x .. y])] !! [x .. y])’
In the expression: ([1 .. (length [x .. y])] !! [x .. y]) : []
Run Code Online (Sandbox Code Playgroud) 使用Haskell:
假设我有列表:[1,3,4,2,3]
我想修改列表中的所有3个.我知道在这种情况下我可以应用它来选择3:
map (\x -> if p x then f x else x) xs
Run Code Online (Sandbox Code Playgroud)
但是,应用于三元组的函数取决于它们在列表中的索引.
因此,例如,如果索引被添加到所需的数字,我要去的函数的输出将是:[1,4,4,2,7].