How do I say Haskell to interpret something as a special type? For example, I have a list and want to divide its length by 2. So I write
(length mylist) / 2
and get this error
No instance for (Fractional Int) arising from a use of `/'
As I want a whole-number division, I'd like to make length mylist
, 2
and the result Int
.
genericLength
function inData.List
. In your case it will not return whole-number answer, but it's generally useful as it lets you avoid some awkwardfromIntegral
calls. – Matvey AksenovfromIntegral
. Seriously, though,fromIntegral
is the magical way to "cast" an Int to anything that instantiates theNum
typeclass. – Dan Burton