
Hi, Prelude> :t (/) (/) :: Fractional a => a -> a -> a Prelude> :t div div :: Integral a => a -> a -> a Prelude> 6 / length [23,34,45] error Prelude> 6 / 3 2.0 Could somebody explain to me why this is? thanks, Alexander Chen

From the error message you can see that the problem is as I said before
Hi,
as you said, the operator (/) takes arguments that belong to the class
*Fractional* (instances of this class are the types *Double* and *Float*).
The function length has type:
Prelude> :t length
length :: Foldable t => t a -> Int
this means that it takes a list and returns something of type Int, In fact
Prelude> :t (length [23,34,45])
(length [23,34,45]) :: Int
Since Int is *not* an instance of the class Fractional, you can't use (/).
Instead Int is an instance of the class Integral, so you can use div with
arguments of type Int.
The example 6/3 works because you didn't assign any type to those numbers,
so they are seen as belonging to the class Num.
Prelude> x=6
Prelude> :t x
x :: Num p => p
This means that they can be seen both as Integral and Fractional and you
can use them with both functions that take Integral arguments and functions
that take Fractional arguments.
If you specify that for example 6 is an Int you can't use (/) any more:
Prelude> x::Int; x=6;
Prelude> x/3
<interactive>:20:1: error:
• No instance for (Fractional Int) arising from a use of ‘/’
that Int is not an instace of the class Fractional.
Hope is clear
Best,
Ut
Il ven 17 apr 2020, 09:20 Alexander Chen
Hi,
Prelude> :t (/) (/) :: *Fractional* a => a -> a -> a
Prelude> :t div div :: *Integral* a => a -> a -> a
Prelude> 6 / length [23,34,45] error
Prelude> 6 / 3 2.0
Could somebody explain to me why this is?
thanks,
Alexander Chen
_______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners

It might help to imagine if you flip your arguments: length [23, 34, 45] / 6 :: Int How do you represent 3/6 as an Int?
El 17 abr 2020, a las 03:19, Alexander Chen
escribió: Hi,
Prelude> :t (/) (/) :: Fractional a => a -> a -> a
Prelude> :t div div :: Integral a => a -> a -> a
Prelude> 6 / length [23,34,45] error
Prelude> 6 / 3 2.0
Could somebody explain to me why this is?
thanks,
Alexander Chen
_______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
participants (3)
-
Alexander Chen
-
amindfv@gmail.com
-
Ut Primum