Search code examples
listhaskellintegraltake

Take - Haskell problems within lists


I am in the process of learning Haskell. I have a function that looks as follows:

takeN :: Integral a => a -> [a]
takeN n = take n [x | x<-[0..]]

All I want this to do, is return n amount of elements in an infinite list, and I am unaware of why this is not working. Any explanations of how to fix it without abandoning my binding (?)


Solution

  • The reason this doesn't work is that take has the type Int -> [a] -> [a]. The number must be an Int, and can't be any Integral.

    You can address the issue with fromIntegral:

    takeN :: Integral a => a -> [a]
    takeN n = take (fromIntegral n) [x | x<-[0..]]