Like a (@) template in Haskell

I wrote my own function takein Haskell as follows:

take' :: (Integral n, Eq a) => n -> [a] -> [a]
take' n lst
    | n <= 0 = []
    | lst == [] = []
    | otherwise = (head lst) : (take' (n-1) $ tail lst)

and it works well.

But when I try to write the same function using the as (@) pattern in the function arguments, it seems that the function does not recognize the second security parameter:

take' :: (Integral n, Eq a) => n -> [a] -> [a]
take' n lst@(hd:tl)
    | n <= 0 = []
    | lst == [] = []
    | otherwise = hd : (take' (n-1) $ tl)

When I try take' 20 []in ghci, I get an error Non-exhaustive patterns in function take'.

What am I doing wrong?

Thanks, Spel.

+4
source share
1 answer

Look at here:

lst == [] = []

lst [], , lst = hd:tl. , . ( Eq, .)

take' 20 [], , , : hd:tl []. , , .

+9

Source: https://habr.com/ru/post/1606808/


All Articles