Home > database >  Haskell - Non-exhaustive patterns in my function
Haskell - Non-exhaustive patterns in my function

Time:05-10

I want to make a function that selects the given indexed elements from a list.

For example:

reconstruct' [1,4] "bear" = [br]
reconstruct' [2,3,4] [1,2,3,4,5] = [2,3,4] 

Function definition:

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

My function is almost working, however after giving back the right elements, instead of closing the list it gives an error: [2,3,4*** Exception: Non-exhaustive patterns in function reconstruct'

How could i fix this?

CodePudding user response:

You are not capturing the situation when the list of indices is empty:

reconstruct' [] _ = []
  • Related