A question regarding the syntax behind the parenthesis and type class.

this is the original question: Write a function showAreaOfCircle which, given the radius of a circle, calculates the area of the circle, showAreaOfCircle 12.3 ⇒ "The area of a circle with radius 12.3cm is about 475.2915525615999 cm^2" Use the show function, as well as the predefined value pi :: Floating a => a to write showAreaOfCircle. the type signature works for this function is: showAreaOfCircle :: (Show a, Floating a) => a -> a -> String my question is : I don't quite understand the syntax behind the parenthesis and the type class Floating a. Since pi is not a function, and hence the entire function only takes one argument which belongs to the type class Show, why should i put a Floating a in the type signature in the first place?(i come up with this type signature because of the error message and the suggestion stated above) Thanks

Because you are not showing x or pi, you are showing (pi * x * x). And the type of the (*) operator is (*) :: Num a => a -> a -> a. So if pi is Floating => a, then x must also be the same type as pi, and thus must be have the same Floating constraint. Keep in mind that Floating is a Fractional, and Fractional is a Num, so you know that if you have a Floating it is already an instance of Num, so you don't have to worry about that constraint. On Wed, Apr 4, 2018 at 10:08 AM, 清羽 <1625143974@qq.com> wrote:
this is the original question:
1.
Write a function showAreaOfCircle which, given the radius of a circle, calculates the area of the circle,
showAreaOfCircle 12.3 ⇒ "The area of a circle with radius 12.3cm is about 475.2915525615999 cm^2"
Use the show function, as well as the predefined value pi :: Floating a => a to write showAreaOfCircle.
the type signature works for this function is: showAreaOfCircle :: (Show a, Floating a) => a -> a -> String
my question is : I don't quite understand the syntax behind the parenthesis and the type class Floating a. Since pi is not a function, and hence the entire function only takes one argument which belongs to the type class Show, why should i put a Floating a in the type signature in the first place?(i come up with this type signature because of the error message and the suggestion stated above) Thanks
_______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
participants (2)
-
David McBride
-
清羽