I'm new in Haskell programming. I'm in search for an implementation of !!
operator which I need to use not as [a] -> Int -> a
or [a] -> Int -> [a]
but as [a] -> Integer -> [a]
, or [a] -> Integer -> a
.
Some code where I want to use this:
next x = 1: zipWith ( ) x (tail x) [1]
bins :: Integer -> [Integer]
bins n
| n < 0 = undefined
| otherwise = (( (foldr (:) [] $ unfoldr (\st->Just(st,next st)) [1]) !! n ))
And i can't just change type of bins :: Integer -> [Integer]
to bins :: Int -> [Integer]
because it's a strict condition in tests.
CodePudding user response:
You can just call it through fromIntegral :: (Num b, Integral a) => a -> b
like so:
..... !! (fromIntegral n)
and keep your type signature as is, because Integer
is an Integral
type and Int
is a Num
.
In GHCi,
> fromIntegral (1::Integer)::Int
1
it :: Int
> :i Integral
class (Real a, Enum a) => Integral a where
....
instance Integral Integer -- Defined in `GHC.Real'
instance Integral Int -- Defined in `GHC.Real'
> :i Num
class Num a where
....
instance Num Integer -- Defined in `GHC.Num'
instance Num Int -- Defined in `GHC.Num'
....
Of course this can introduce the usual integer wrap-around problems,
> maxBound :: Int
9223372036854775807
it :: Int
> (maxBound :: Int) 1
-9223372036854775808
it :: Int
> (fromIntegral (maxBound :: Int) :: Integer) 1
9223372036854775808
it :: Integer
> (fromIntegral (fromIntegral (maxBound :: Int) :: Integer) 1) :: Int
-9223372036854775808
But it is probably safe to assume you won't actually use it with such high index values, ever.