Send Beginners mailing list submissions to
[email protected]
To subscribe or unsubscribe via the World Wide Web, visit
http://www.haskell.org/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. Type error in sub-function (???)
2. Manage the type of a variable in the show implementation
(Christian Sperandio)
3. Re: Type error in sub-function (Barbu Paul - Gheorghe)
4. Re: Type error in sub-function (Gesh)
5. Re: Type error in sub-function (Kim-Ee Yeoh)
6. Re: Manage the type of a variable in the show implementation
(Chadda? Fouch?)
----------------------------------------------------------------------
Message: 1
Date: Wed, 23 Jul 2014 21:08:49 +0900
From: ??? <[email protected]>
To: [email protected]
Subject: [Haskell-beginners] Type error in sub-function
Message-ID:
<calmycjpa9amfqluxjsdh8zyrup_qnlzktl8ozm2azzwackr...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"
Hi, all!
When I compile my program below, ghc emits Type error.
Since the return type of foo Func is IO String and
first case statement has "return timedVal",
I think that ghc expects the type of timedVal as String.
However, the error message shows that
ghc expects timedVal should have type IO b0.
What am I missing?
Any comments are appreciated.
Thanks in advance!
Chul-Woong
--
import Data.Time.LocalTime (getZonedTime, zonedTimeToUTC)
import System.Locale (defaultTimeLocale)
import Data.Time.Format (formatTime)
import Data.Time.Clock (UTCTime)
currentUTCTime :: IO UTCTime
currentUTCTime = do
zTime <- getZonedTime
return (zonedTimeToUTC zTime)
fooFunc :: IO String
fooFunc = do
putStrLn "Hello, world"
barFunc "xutc"
where barFunc s =
case s of
"apple" -> return s
('x': val) -> return timedVal
where timedVal = case val of
"utc" -> do utcTime <-
currentUTCTime
formatTime
defaultTimeLocale "%a" utcTime
---
Prelude> :l test
[1 of 1] Compiling Main ( test.hs, interpreted )
test.hs:21:57:
Couldn't match expected type `IO b0' with actual type `String'
In the return type of a call of `formatTime'
In a stmt of a 'do' block:
formatTime defaultTimeLocale "%a" utcTime
In the expression:
do { utcTime <- currentUTCTime;
formatTime defaultTimeLocale "%a" utcTime }
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://www.haskell.org/pipermail/beginners/attachments/20140723/a5ff12d3/attachment-0001.html>
------------------------------
Message: 2
Date: Wed, 23 Jul 2014 14:20:23 +0200
From: Christian Sperandio <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
beginner-level topics related to Haskell <[email protected]>
Subject: [Haskell-beginners] Manage the type of a variable in the show
implementation
Message-ID:
<CAKc7jjierahdA64V+9wHa=boek67wgu1w1vxngaqykfwvhl...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"
Hi,
I wrote this kind of code:
data DataLog a = SingleData a
| MultipleData [a]
instance (Show a) => Show (DataLog a) where
show (SingleData v) = show v
show (MultipleData vs) = join "|" vs
where join _ [] = []
join _ (x:[]) = show x
join s (x:xs) = show x ++ s ++ join s xs
It works but when the DataLog is String typed I got this result:
"My Test"|"Another Value"
I'd prefer have:
My Test|Another Value
I know the quote problem comes from the show function on String value.
How could I do a show for no-string values and return directly the value
for strings?
Thanks.
Chris
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://www.haskell.org/pipermail/beginners/attachments/20140723/1f148f48/attachment-0001.html>
------------------------------
Message: 3
Date: Wed, 23 Jul 2014 15:29:53 +0300
From: Barbu Paul - Gheorghe <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] Type error in sub-function
Message-ID: <[email protected]>
Content-Type: text/plain; charset="utf-8"
On 07/23/2014 03:08 PM, ??? wrote:
> Hi, all!
Hi,
Code attached and online: http://lpaste.net/107954
If you look at the formatTime docs [1] you'll see that it returns a "String" not
an "IO String" as you expected in the original code, so we need to wrap that in
the IO monad using "return", because on the "utc" case branch you're in a monad
(keyword: "do").
After doing this modification we bound an IO String to timedVal in the where
clause, thus it's no longer necessary to "return timedVal" on the ('x': val)
branch of the first case since timedVal is already an IO monad containing a
string.
Hope this helps.
PS: Also if I were you, I'd extract the barFunc function form the fooFunc
function and write it as a standalone with explicit types, too. For me, seeing
the types helps a lot.
http://hackage.haskell.org/package/time-1.4.2/docs/Data-Time-Format.html#v:formatTime
--
Barbu Paul - Gheorghe
Common sense is not so common - Voltaire
Visit My GitHub profile to see my open-source projects -
https://github.com/paullik
-------------- next part --------------
A non-text attachment was scrubbed...
Name: test.hs
Type: text/x-haskell
Size: 760 bytes
Desc: not available
URL:
<http://www.haskell.org/pipermail/beginners/attachments/20140723/e8c0ce4d/attachment-0001.hs>
------------------------------
Message: 4
Date: Wed, 23 Jul 2014 15:30:50 +0300
From: Gesh <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] Type error in sub-function
Message-ID: <[email protected]>
Content-Type: text/plain; charset=UTF-8; format=flowed
On 7/23/2014 3:08 PM, ??? wrote:
Look at the error:
> test.hs:21:57:
> Couldn't match expected type `IO b0' with actual type `String'
> In the return type of a call of `formatTime'
> In a stmt of a 'do' block:
> formatTime defaultTimeLocale "%a" utcTime
> In the expression:
> do { utcTime <- currentUTCTime;
> formatTime defaultTimeLocale "%a" utcTime }
Rewriting that do-block a little, we get:
> do utcTime <- currentUTCTime
> problem
> where problem = formatTime defaultTimeLocale "%a" utcTime
In plain English, GHC's error message means:
> On line 21 of test.hs, column 57:
> I (GHC) inferred from the context surrounding it that
> 'problem' should have the type 'IO a' for some a.
> However, you defined 'problem' in a way that makes its
> type be 'String'. Therefore, I can't accept your program.
Do you see what caused the error?
How would you fix it?
HTH,
Gesh
------------------------------
Message: 5
Date: Wed, 23 Jul 2014 19:33:56 +0700
From: Kim-Ee Yeoh <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] Type error in sub-function
Message-ID:
<CAPY+ZdTnbnsF+ikh7QRDN=san4A_pGFSGxEXPNJPPJqp5M92=g...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"
On Wed, Jul 23, 2014 at 7:08 PM, ??? <[email protected]> wrote:
> Since the return type of foo Func is IO String and
> first case statement has "return timedVal",
> I think that ghc expects the type of timedVal as String.
> However, the error message shows that
> ghc expects timedVal should have type IO b0.
>
It's not really about timedVal nor the case 'statement'. (The scare quotes
are because there are only expressions, not statements, in haskell.)
Consider the difference between
do { putStrLn "hello"; True; }
and
do { putStrLn "hello"; return True; }
Which one throws an error and why?
-- Kim-Ee
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://www.haskell.org/pipermail/beginners/attachments/20140723/41ab3c89/attachment-0001.html>
------------------------------
Message: 6
Date: Wed, 23 Jul 2014 14:40:02 +0200
From: Chadda? Fouch? <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] Manage the type of a variable in the
show implementation
Message-ID:
<canfjzryknv9czjh27gsiid5dtjc1fgrht7+wlsebtgclrhl...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"
On Wed, Jul 23, 2014 at 2:20 PM, Christian Sperandio <
[email protected]> wrote:
> I know the quote problem comes from the show function on String value.
> How could I do a show for no-string values and return directly the value
> for strings?
>
You can't ! (ok you can but you'll need to use OverlappingInstances)
Anyway, using Show for this is wrong, Show is supposed to translate
datatypes in a form you could copy in your code to get the value back, it
isn't supposed to be for presentation. You have libraries that do pretty
formatting better.
If you absolutely want this, I would suggest writing another class for it
and use OverlappingInstances to allow for one "DataLog String" instance and
one "(Show a) => YourClass (DataLog a)" instance.
Another possibility is to do a newtype for strings that have a Show
instance that doesn't put quotes around. That might even be a better idea,
maybe your Strings really represent variables or something in this case
anyway ?
--
Jeda?
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://www.haskell.org/pipermail/beginners/attachments/20140723/a9425507/attachment.html>
------------------------------
Subject: Digest Footer
_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners
------------------------------
End of Beginners Digest, Vol 73, Issue 16
*****************************************