Haskell函数异常中的非穷尽模式

时间:2019-12-09 19:06:18

标签: haskell

countElem :: [Int] -> Int
countElem [] = 0
countElem (x:xs) = 1 + countElem xs

listOfDiffs :: [Int] -> [Int]
listOfDiffs [a]
  |countElem [a] == 0 || countElem [a] == 1 = []
  |otherwise = [(nthElem k [a]) - (nthElem (k+1) [a]) | k <- [0..((countElem [a])-2)]] 
    where
    nthElem :: Int -> [Int] -> Int
    nthElem n [] = error "leere Liste"
    nthElem 0 (x:xs) = x
    nthElem n (x:xs) = nthElem (n-1) xs

我需要从原始列表中退回2个元素之间的差异列表,例如listOfDiffs [2,3,3,1,4]-> [-1,0,2,-3],而无需使用任何预定义列表功能,但我想我没有足够的案例

0 个答案:

没有答案