Send Beginners mailing list submissions to
[email protected]
To subscribe or unsubscribe via the World Wide Web, visit
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
or, via email, send a message with subject or body 'help' to
[email protected]
You can reach the person managing the list at
[email protected]
When replying, please edit your Subject line so it is more specific
than "Re: Contents of Beginners digest..."
Today's Topics:
1. I have a question in designing a 'show' function
(=?utf-8?B?5riF5769?=)
2. Re: I have a question in designing a 'show' function
(mukesh tiwari)
3. Re: I have a question in designing a 'show' function (Joel Neely)
----------------------------------------------------------------------
Message: 1
Date: Wed, 4 Apr 2018 14:37:13 +0800
From: "=?utf-8?B?5riF5769?=" <[email protected]>
To: "=?utf-8?B?YmVnaW5uZXJz?=" <[email protected]>
Subject: [Haskell-beginners] I have a question in designing a 'show'
function
Message-ID: <[email protected]>
Content-Type: text/plain; charset="utf-8"
here is the question:
Write a function showAreaOfCircle which, given the radius of a circle,
calculates the area of the circle,
<code>showAreaOfCircle 12.3</code> ⇒ <code>"The area of a circle with radius
12.3cm is about 475.2915525615999 cm^2"</code>
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 a
bound by the type signature for:
showAreaOfCircle :: forall a. Show a => a -> String
at FirstStep.hs:20:1-41
Possible 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
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://mail.haskell.org/pipermail/beginners/attachments/20180404/d6bdacfa/attachment-0001.html>
------------------------------
Message: 2
Date: Wed, 4 Apr 2018 16:51:43 +1000
From: mukesh tiwari <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] I have a question in designing a
'show' function
Message-ID:
<CAFHZvE8avVDCNAbiSAPCK4e2=jh78xcj-20ufgrefrigwsu...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"
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 -> String
showAreaOfCircle 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 10
showAreaOfCircle 10 :: String
*Tmp> :t showAreaOfCircle
showAreaOfCircle :: (Show a, Floating a) => a -> String
*Tmp> :t showAreaOfCircle 10.12
showAreaOfCircle 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, 清羽 <[email protected]> wrote:
>
> here is the question:
>
> Write a function showAreaOfCircle which, given the radius of a circle,
> calculates the area of the circle,
>
> <code>showAreaOfCircle 12.3</code> ⇒ <code>"The area of a circle with radius
> 12.3cm is about 475.2915525615999 cm^2"</code>
>
> 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 a
> bound by the type signature for:
> showAreaOfCircle :: forall a. Show a => a -> String
> at FirstStep.hs:20:1-41
> Possible 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
> [email protected]
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://mail.haskell.org/pipermail/beginners/attachments/20180404/ae18ab08/attachment-0001.html>
------------------------------
Message: 3
Date: Wed, 4 Apr 2018 06:19:11 -0500
From: Joel Neely <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] I have a question in designing a
'show' function
Message-ID:
<caeezxajv7itb8cnejp6ypembyszn68h5d3tf_2bv6+nfb7b...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"
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, 清羽 <[email protected]> wrote:
>
> here is the question:
>
> Write a function showAreaOfCircle which, given the radius of a circle,
> calculates the area of the circle,
>
> <code>showAreaOfCircle 12.3</code> ⇒ <code>"The area of a circle with radius
> 12.3cm is about 475.2915525615999 cm^2"</code>
>
> 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 a
> bound by the type signature for:
> showAreaOfCircle :: forall a. Show a => a -> String
> at FirstStep.hs:20:1-41
> Possible 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
> [email protected]
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>
>
--
Beauty of style and harmony and grace and good rhythm depend on simplicity.
- Plato
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://mail.haskell.org/pipermail/beginners/attachments/20180404/17b9e8b3/attachment.html>
------------------------------
Subject: Digest Footer
_______________________________________________
Beginners mailing list
[email protected]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
------------------------------
End of Beginners Digest, Vol 118, Issue 2
*****************************************