I am writing code in Haskell that acts like take, except it takes elements from the end of a list.
snatch :: (Num a, Ord a) => a -> [b] -> [b]
snatch n _
| n <= 0 = []
snatch _ [] = []
snatch n x = reverse (take n (reverse x))
The problem is with this line,
snatch n x = reverse (take n (reverse x))
It basically states that for take n, n has to be an Int. However, a is a Num type. If I change the definition of the function to this,
snatch :: Int -> [b] -> [b]
Then it works fine. I've tried reading the docs and searching the internet. But I can't find out why. Int is apparently a class of Num. So shouldn't this work? Why doesn't it work?
Numis a class, butIntis a type, and not a class. We say thatIntis an instance ofNum. - duplodeNumseems the wrong constraint here,Integrallooks better. - chi