0

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 (?)

sudobangbang
  • 1,406
  • 10
  • 32
  • 55

2 Answers2

3

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..]]
Mark Seemann
  • 225,310
  • 48
  • 427
  • 736
0

You can do it without making your own function: input>> take 3 [1..] output>> [1,2,3]

MugenTwo
  • 314
  • 6
  • 14