Haskell Convert Integer to Int?

Use fromIntegral.

takeN :: Integer -> [a] -> [a]
takeN n l = take (fromIntegral n) l

Note that fromIntegral :: (Integral a, Num b) => a -> b, so sometimes you will need an extra type annotation (e.g. (fromIntegral n :: Int)), but usually the compiler can infer which type you want.

In the special case of your example, in Data.List there is genericTake :: (Integral i) => i -> [a] -> [a], which does the same thing as take but with a more general type.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)