# I have a question in designing a 'show' function

4 messages
Open this post in threaded view
|

## I have a question in designing a 'show' function

 here is the 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"`my solution is:```showAreaOfCircle :: Show a => a -> String showAreaOfCircle x = "The area of a circle with radius" ++ show x ++ "is about" ++ " " + show(pi * x * x) ++ "cm^2"```but there is an error• Could not deduce (Num a) arising from a use of ‘*’from the context: Show abound by the type signature for:showAreaOfCircle :: forall a. Show a => a -> Stringat FirstStep.hs:20:1-41Possible fix:add (Num a) to the context of the type signature for:showAreaOfCircle :: forall a. Show a => a -> String• In the first argument of ‘show’, namely ‘(pi * x * x)’In the second argument of ‘(+)’, namely ‘show (pi * x * x)’In the second argument of ‘(++)’, namely ‘" " + show (pi * x * x)’I had tried to change the type declaration to `Show a => Num a => a -> String`i guess there must be a problem with pi.Can someone help me with the error message? Great thanks_______________________________________________ Beginners mailing list [hidden email] http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Open this post in threaded view
|

## Re: I have a question in designing a 'show' function

 Add one more constraint that a is member of Floating type class and change your + to ++`" " + show(pi * x * x`showAreaOfCircle :: (Floating a, Show a) => a -> StringshowAreaOfCircle x = "The area of a circle with radius" ++ show x ++ "is about" ++ " " ++ show (pi * x * x) ++ "cm^2"*Tmp> showAreaOfCircle 10"The area of a circle with radius10.0is about 314.1592653589793cm^2"*Tmp> :t showAreaOfCircle 10showAreaOfCircle 10 :: String*Tmp> :t showAreaOfCircle showAreaOfCircle :: (Show a, Floating a) => a -> String*Tmp> :t showAreaOfCircle 10.12showAreaOfCircle 10.12 :: String*Tmp> showAreaOfCircle 10.12"The area of a circle with radius10.12is about 321.74432666180644cm^2"*Tmp> Best, Mukesh On Wed, Apr 4, 2018 at 4:37 PM, 清羽 wrote:here is the 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"`my solution is:```showAreaOfCircle :: Show a => a -> String showAreaOfCircle x = "The area of a circle with radius" ++ show x ++ "is about" ++ " " + show(pi * x * x) ++ "cm^2"```but there is an error• Could not deduce (Num a) arising from a use of ‘*’from the context: Show abound by the type signature for:showAreaOfCircle :: forall a. Show a => a -> Stringat FirstStep.hs:20:1-41Possible fix:add (Num a) to the context of the type signature for:showAreaOfCircle :: forall a. Show a => a -> String• In the first argument of ‘show’, namely ‘(pi * x * x)’In the second argument of ‘(+)’, namely ‘show (pi * x * x)’In the second argument of ‘(++)’, namely ‘" " + show (pi * x * x)’I had tried to change the type declaration to `Show a => Num a => a -> String`i guess there must be a problem with pi.Can someone help me with the error message? Great thanks_______________________________________________ Beginners mailing list [hidden email] http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners _______________________________________________ Beginners mailing list [hidden email] http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
 In reply to this post by 清羽 You only have a single plus sign before the second invocation of show:`++ " " + show(pi * x * x) ++ "cm^2"`Don't you mean concatenation?`++ " " ++ show(pi * x * x) ++ "cm^2"`Good luck,-jn-On Wed, Apr 4, 2018 at 1:37 AM, 清羽 wrote:here is the 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"`my solution is:```showAreaOfCircle :: Show a => a -> String showAreaOfCircle x = "The area of a circle with radius" ++ show x ++ "is about" ++ " " + show(pi * x * x) ++ "cm^2"```but there is an error• Could not deduce (Num a) arising from a use of ‘*’from the context: Show abound by the type signature for:showAreaOfCircle :: forall a. Show a => a -> Stringat FirstStep.hs:20:1-41Possible fix:add (Num a) to the context of the type signature for:showAreaOfCircle :: forall a. Show a => a -> String• In the first argument of ‘show’, namely ‘(pi * x * x)’In the second argument of ‘(+)’, namely ‘show (pi * x * x)’In the second argument of ‘(++)’, namely ‘" " + show (pi * x * x)’I had tried to change the type declaration to `Show a => Num a => a -> String`i guess there must be a problem with pi.Can someone help me with the error message? Great thanks_______________________________________________ Beginners mailing list [hidden email] http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners -- Beauty of style and harmony and grace and good rhythm depend on simplicity. - Plato _______________________________________________ Beginners mailing list [hidden email] http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
 Yes.And I finally make the function error-free.Thanks------------------ 原始邮件 ------------------发件人: "Joel Neely"<[hidden email]>;发送时间: 2018年4月4日(星期三) 晚上7:19收件人: "The Haskell-Beginners Mailing List - Discussion of primarilybeginner-level topics related to Haskell"<[hidden email]>;主题: Re: [Haskell-beginners] I have a question in designing a 'show'functionYou only have a single plus sign before the second invocation of show:`++ " " + show(pi * x * x) ++ "cm^2"`Don't you mean concatenation?`++ " " ++ show(pi * x * x) ++ "cm^2"`Good luck,-jn-On Wed, Apr 4, 2018 at 1:37 AM, 清羽 wrote:here is the 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"`my solution is:```showAreaOfCircle :: Show a => a -> String showAreaOfCircle x = "The area of a circle with radius" ++ show x ++ "is about" ++ " " + show(pi * x * x) ++ "cm^2"```but there is an error• Could not deduce (Num a) arising from a use of ‘*’from the context: Show abound by the type signature for:showAreaOfCircle :: forall a. Show a => a -> Stringat FirstStep.hs:20:1-41Possible fix:add (Num a) to the context of the type signature for:showAreaOfCircle :: forall a. Show a => a -> String• In the first argument of ‘show’, namely ‘(pi * x * x)’In the second argument of ‘(+)’, namely ‘show (pi * x * x)’In the second argument of ‘(++)’, namely ‘" " + show (pi * x * x)’I had tried to change the type declaration to `Show a => Num a => a -> String`i guess there must be a problem with pi.Can someone help me with the error message? Great thanks_______________________________________________ Beginners mailing list [hidden email] http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners -- Beauty of style and harmony and grace and good rhythm depend on simplicity. - Plato _______________________________________________ Beginners mailing list [hidden email] http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners