goe*_*ash 0 haskell functional-programming list-comprehension
我想加倍列表中的每一个元素.这是代码 -
doubleSec n [] = []
doubleSec n (x:xs)
| n==1 = x*2 : doubleSec 0 xs
| otherwise = x : doubleSec 1 xs
doubleSecond xs =
doubleSec 0 xs
Run Code Online (Sandbox Code Playgroud)
如何在单个函数中压缩此逻辑?
您可以像这样匹配列表中的模式
doubleSec :: [Int] -> [Int]
doubleSec [] = []
doubleSec [x] = [x]
doubleSec (x : y : xs) = x : 2* y : doubleSec xs
Run Code Online (Sandbox Code Playgroud)
让你对第二个元素做特定的事情