我想做一個從串列中選擇給定索引元素的函式。
例如:
reconstruct' [1,4] "bear" = [br]
reconstruct' [2,3,4] [1,2,3,4,5] = [2,3,4]
函式定義:
reconstruct' :: [Int] -> [a] -> [a]
reconstruct' _ [] = []
reconstruct' (x:xs) l = [place (x-1) l ] (reconstruct' (xs) l)
place :: Int -> [a] -> a
place _ [] = error "error"
place y (x:xs) | y <= 0 = x
| otherwise = place (y-1) xs
我的函式幾乎可以作業了,但是在回傳正確的元素之后,它沒有關閉串列,而是給出了一個錯誤:[2,3,4*** 例外:函式重構中的非詳盡模式'
我該如何解決這個問題?
uj5u.com熱心網友回復:
當索引串列為空時,您沒有捕捉到這種情況:
reconstruct' [] _ = []
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/471401.html
上一篇:SFINAE在遞回函式中不起作用
