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: exception, not in IO (Lyndon Maydwell)
2. Re: exception, not in IO (Ertugrul S?ylemez)
3. Re: exception, not in IO (Kees Bleijenberg)
4. Lens (Emmanuel Surleau)
----------------------------------------------------------------------
Message: 1
Date: Sun, 14 Jul 2013 20:30:32 +1000
From: Lyndon Maydwell <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] exception, not in IO
Message-ID:
<cam5qztz7z_1zpgjiyvennqqdtamxbcj+1eamfkmtz+6nu0a...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"
You can turn anything into an IO action with return, or you could catch the
exception at a level where you are performing IO. Would this be what you're
after?
On Sun, Jul 14, 2013 at 7:47 PM, Kees Bleijenberg <
[email protected]> wrote:
> The app I?am working on, converts a jsonString to another string encoding.
> ****
>
> The function I want to write is jsonString -> (encoding, errorMsg) so
> String-> (String, String) ****
>
> For this purpose I have a typeable datastructure Glass. Because it is
> typeable I can do (decodeJSON jsonString) :: Glass****
>
> But sometimes the jsonString is not valid (misformed or wrong fields).
> decodeJSON then throws a exception. I want to****
>
> catch that exection and transform the result to something like (?? ,
> theErrorMsg). Unfortunately all catch functions want IO parameters. ****
>
> What can I do?****
>
> ** **
>
> Kees****
>
> ** **
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://www.haskell.org/pipermail/beginners/attachments/20130714/8a48744b/attachment-0001.html>
------------------------------
Message: 2
Date: Sun, 14 Jul 2013 15:06:32 +0200
From: Ertugrul S?ylemez <[email protected]>
To: [email protected]
Subject: Re: [Haskell-beginners] exception, not in IO
Message-ID: <[email protected]>
Content-Type: text/plain; charset="us-ascii"
"Kees Bleijenberg" <[email protected]> wrote:
> But sometimes the jsonString is not valid (misformed or wrong fields).
> decodeJSON then throws a exception. I want to
>
> catch that exection and transform the result to something like ("" ,
> theErrorMsg). Unfortunately all catch functions want IO parameters.
>
> What can I do?
IO is just one of the many monads with exception support. For your
case, since JSON parsing is a pure process, you would want to use a pure
exception monad like `Maybe` or `Either MyError`:
data MyError
= InvalidDateField
| {- ... -}
| UnknownError
There is nothing wrong with using regular exception types, if you wish,
in which case you might use `Either SomeException`. Then separate
concerns:
decode :: String -> Either MyError Glass
encode :: Glass -> String
Finally the conversion function is as simple as:
convert :: String -> Either MyError String
convert = fmap encode . decode
If `encode` can fail as well and exceptions are regular Haskell
exceptions:
import Control.Exception
import Control.Monad
decode :: String -> Either SomeException Glass
encode :: Glass -> Either SomeException String
convert :: String -> Either SomeException String
convert = encode <=< decode
I hope this helps.
Greets,
Ertugrul
--
Not to be or to be and (not to be or to be and (not to be or to be and
(not to be or to be and ... that is the list monad.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 836 bytes
Desc: not available
URL:
<http://www.haskell.org/pipermail/beginners/attachments/20130714/a405caa4/attachment-0001.sig>
------------------------------
Message: 3
Date: Sun, 14 Jul 2013 20:07:08 +0200
From: "Kees Bleijenberg" <[email protected]>
To: "'The Haskell-Beginners Mailing List - Discussion of primarily
beginner-level topics related to Haskell'" <[email protected]>
Subject: Re: [Haskell-beginners] exception, not in IO
Message-ID: <000001ce80bc$f45967b0$dd0c3710$@[email protected]>
Content-Type: text/plain; charset="utf-8"
Lyndon,
You wrote:
You can turn anything into an IO action with return, or you could catch the
exception at a level where you are performing IO. Would this be what you're
after?
Wait until you perform IO, seems quit uneasy to me and why? Converting a string
to a typeable is not a IO action. Problem is that this function lives in a dll.
The IO is done by the calling program (not a Haskell program). The function is
not in a monad, it is a pure function. So I think I can?t do a return.
Kees
On Sun, Jul 14, 2013 at 7:47 PM, Kees Bleijenberg <[email protected]>
wrote:
The app I?am working on, converts a jsonString to another string encoding.
The function I want to write is jsonString -> (encoding, errorMsg) so
String-> (String, String)
For this purpose I have a typeable datastructure Glass. Because it is typeable
I can do (decodeJSON jsonString) :: Glass
But sometimes the jsonString is not valid (misformed or wrong fields).
decodeJSON then throws a exception. I want to
catch that exection and transform the result to something like (?? ,
theErrorMsg). Unfortunately all catch functions want IO parameters.
What can I do?
Kees
_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://www.haskell.org/pipermail/beginners/attachments/20130714/1b4f9561/attachment-0001.html>
------------------------------
Message: 4
Date: Sun, 14 Jul 2013 22:47:16 +0200
From: Emmanuel Surleau <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
beginner-level topics related to Haskell <[email protected]>
Subject: [Haskell-beginners] Lens
Message-ID:
<CADd2AG4AXELc2Df=uwp8l0pjf9uohskfngzh3x8smvz_jjr...@mail.gmail.com>
Content-Type: text/plain; charset="iso8859-7"
Hello there,
I'm starting to use a bit more extensively the lens package. It is clearly
very powerful, but it feels like a maze of related components
(Setting/Setter/Lens'/LensLike/Lens/Traversal/Traversal') which can be
swapped for one another except when they can't.
The first (concrete) problem I ran into is how to update the members of a
set with the result of an IO action. I have managed to do this with a pure
function (prefixName) but I'm not sure of how to do this with promptName.
Full program below:
{-# LANGUAGE TemplateHaskell #-}
import Control.Lens
import Data.Set
import Data.Set.Lens
data Dog = Dog { _name :: String, _legs :: Int }
deriving (...)
makeLenses ''Dog
data Dogs = Dogs { _dogs :: Set Dog }
deriving Show
makeLenses ''Dogs
fourLegs :: Traversal' Dog Dog
fourLegs = filtered (?d -> d^.legs == 4)
promptName :: String -> IO String
promptName dogName = do
putStr $ "New name for " ++ dogName
getLine
prefixName :: Dog -> Dog
prefixName dog = set name ("PREFIXED: " ++ dog^.name) dog
main :: IO ()
main = do
let fido = Dog "fido" 4
let milou = Dog "milou" 4
let cripple = Dog "cripple" 3
let doggies = Dogs $ fromList [fido, milou, cripple]
-- prefix dog names via a pure function
let doggies' = over (dogs.setmapped) prefixName doggies
print doggies'
-- change dog names by prompting the user ?
return ()
Help would be appreciated (in particular, 'cripple' would love to be
renamed).
Since I was struggling with the library, I had the idea to look at the
internals, but got stuck at the definition of Lens:
type Lens s t a b = forall f. Functor f => (a -> f b) -> s -> f t
a) I'm not sure why the explicit forall is needed here (isn't this
equivalent to just Functor f => ...)?
b) My understanding is that a lens packs both getter and setters, but I
don't know which is supposed to be which here...
c) Is there any kind of in-depth guide to Control.Lens somewhere? I have
found some examples and tutorials but nothing that seemed to do more than
scratch the surface.
Thanks,
Emm
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://www.haskell.org/pipermail/beginners/attachments/20130714/0331337e/attachment.html>
------------------------------
Subject: Digest Footer
_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners
------------------------------
End of Beginners Digest, Vol 61, Issue 18
*****************************************