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. Re: More De/Serialisation Woes (Stephen Tetley)
2. Re: Mixing IO and other monads (Brian Victor)
3. Re: Re: Mixing IO and other monads (Brandon S Allbery KF8NH)
----------------------------------------------------------------------
Message: 1
Date: Tue, 7 Sep 2010 12:12:44 +0100
From: Stephen Tetley <[email protected]>
Subject: Re: [Haskell-beginners] More De/Serialisation Woes
Cc: [email protected]
Message-ID:
<[email protected]>
Content-Type: text/plain; charset=ISO-8859-1
Hi Tom
For the serializePreamble question...
serialisePreamble would need to map over the list of strings.
As you want to apply a monadic function to each value you would need
monadic-map rather than regular map (:: (a->b) -> [a] -> [b]).
Monad map comes in two flavours
a) mapM :: (a -> m b) -> [a] -> m [b]
b) mapM_ :: (a -> m b) -> [a] -> m ()
mapM_ is in some respects a "forgetful" version of mapM - it performs
the actions but "forgets" the results.
mapM_ is the one you want here as writing to handle in the IO monad is
approximately :: a -> IO ()
Because Strings are length prefixed you will a helper function along
the lines of
serializeString :: Handle -> String -> IO ()
serializeString h s = do { L.hPut h (encode (length s))
; mapM_ (\ch -> L.hPutChar h ch) s }
Unfortunately using L.hPutChar isn't appropriate - it illustrates what
you would have to do, but isn't the right way of doing it. If you are
using String data from the Java world - its likely you will actually
have to deal with some encoding - probably converting each char to a
Word16 and writing a Word16 with the appropriate endian-ness.
The Data.Binary.Put module has a function putByteString for writing
(byte) strings. Unfortunately this is won't be appropriate either, as
the data layout from putByteString is unlikely to match the layout
that the Java tuple space expects. For your use case, you will have to
make some "primitive" output functions yourself.
------------------------------
Message: 2
Date: Tue, 7 Sep 2010 12:58:40 +0000 (UTC)
From: Brian Victor <[email protected]>
Subject: [Haskell-beginners] Re: Mixing IO and other monads
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=us-ascii
Sorry it took so long, but I have to thank everyone who responded. You
all gave me some good ideas.
Brandon's approach most closely resembled what I'm going with for the
moment. I did have to make a few changes, which are shown below for
archival purposes. The changes are:
1) Made the first parameter to ioE a String since fail requires it.
2) Switched left/right in io since that's how PortMidi does it.
3) Added (Show e) constraint to io
4) Added return to the Just and Left branches of the case.
ioE :: String -> IO (Maybe a) -> IO a
ioE e a = a >>= \r -> case r of
Just r' -> return r'
Nothing -> fail e
io :: (Show e) => IO (Either a e) -> IO a
io a = a >>= \r -> case r of
Left v -> return v
Right e -> fail $ show e
main :: IO ()
main = do
initialize
inputDeviceId <- ioE "No default input device" getDefaultInputDeviceID
outputDeviceId <- ioE "No default output device" getDefaultOutputDeviceID
inputStream <- io $ openInput inputDeviceId
outputStream <- io $ openOutput outputDeviceId 0
putStrLn "Starting translation"
runTranslationLoop inputStream outputStream
Thanks again!
--
Brian
------------------------------
Message: 3
Date: Tue, 07 Sep 2010 12:13:33 -0400
From: Brandon S Allbery KF8NH <[email protected]>
Subject: Re: [Haskell-beginners] Re: Mixing IO and other monads
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=UTF-8
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
On 9/7/10 08:58 , Brian Victor wrote:
> 4) Added return to the Just and Left branches of the case.
That's in the running for my most common error when writing Haskell code :/
- --
brandon s. allbery [linux,solaris,freebsd,perl] [email protected]
system administrator [openafs,heimdal,too many hats] [email protected]
electrical and computer engineering, carnegie mellon university KF8NH
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.10 (Darwin)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/
iEYEARECAAYFAkyGZK0ACgkQIn7hlCsL25W30gCePwo25uCqGeRtFrahIBkOBaxl
BowAn0vbrQLl+pinGgix/TK1NgigoPl7
=RGFE
-----END PGP SIGNATURE-----
------------------------------
_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners
End of Beginners Digest, Vol 27, Issue 17
*****************************************