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.  hGetContents exception (Luca Ciciriello)
   2. Re:  hGetContents exception (Daniel Fischer)
   3. Re:  hGetContents exception (Felipe Almeida Lessa)
   4. Re:  Editing the layout hook (Anthony Campbell)
   5. Re:  Editing the layout hook (Amy de Buitl?ir)
   6. Re:  Editing the layout hook (Anthony Campbell)
   7.  How Best to Deal with Nested Monads? (Michael Craig)
   8. Re:  How Best to Deal with Nested Monads? (David McBride)
   9. Re:  How Best to Deal with Nested Monads? (Brent Yorgey)
  10. Re:  How Best to Deal with Nested Monads? (Michael Craig)


----------------------------------------------------------------------

Message: 1
Date: Wed, 14 Sep 2011 14:50:12 +0200
From: Luca Ciciriello <[email protected]>
Subject: [Haskell-beginners] hGetContents exception
To: Beginners <[email protected]>
Message-ID: <[email protected]>
Content-Type: text/plain; charset="us-ascii"

Hi All.
I'm using the function hGetContents in order to read some text file. If one or 
more of these text file have a wrong UTF encoding, I get the error:

hGetContents: invalid argument (Illegal byte sequence)

My workaround is to open the wrong encoded file in emacs and create a copy of 
this file (cut and paste in a new buffer). After this operation the new file 
has a correct UTF encoding and hGetContents doesn't complain any more.

Is there a better way to read (without complaining) such wrong file without an 
external action (emacs)?

Thanks in advance for any answer.

Luca.

 


------------------------------

Message: 2
Date: Wed, 14 Sep 2011 15:15:11 +0200
From: Daniel Fischer <[email protected]>
Subject: Re: [Haskell-beginners] hGetContents exception
To: [email protected]
Message-ID: <[email protected]>
Content-Type: Text/Plain;  charset="iso-8859-1"

On Wednesday 14 September 2011, 14:50:12, Luca Ciciriello wrote:
> Hi All.
> I'm using the function hGetContents in order to read some text file. If
> one or more of these text file have a wrong UTF encoding, I get the
> error:
> 
> hGetContents: invalid argument (Illegal byte sequence)
> 
> My workaround is to open the wrong encoded file in emacs and create a
> copy of this file (cut and paste in a new buffer). After this operation
> the new file has a correct UTF encoding and hGetContents doesn't
> complain any more.

Wouldn't using iconv be more convenient?

> 
> Is there a better way to read (without complaining) such wrong file
> without an external action (emacs)?

If you know the encoding of the file, you can

hSetEncoding handle encoding

after you opened the file (if it's one of the known encodings).




------------------------------

Message: 3
Date: Wed, 14 Sep 2011 10:17:17 -0300
From: Felipe Almeida Lessa <[email protected]>
Subject: Re: [Haskell-beginners] hGetContents exception
To: Luca Ciciriello <[email protected]>
Cc: Beginners <[email protected]>
Message-ID:
        <CANd=ogee8e5zkg84zajj0romgfz4nzzvz4tcz7sp27dfa4g...@mail.gmail.com>
Content-Type: text/plain; charset=UTF-8

On Wed, Sep 14, 2011 at 9:50 AM, Luca Ciciriello
<[email protected]> wrote:
> Hi All.
> I'm using the function hGetContents in order to read some text file. If one 
> or more of these text file have a wrong UTF encoding, I get the error:
>
> hGetContents: invalid argument (Illegal byte sequence)
>
> My workaround is to open the wrong encoded file in emacs and create a copy of 
> this file (cut and paste in a new buffer). After this operation the new file 
> has a correct UTF encoding and hGetContents doesn't complain any more.
>
> Is there a better way to read (without complaining) such wrong file without 
> an external action (emacs)?

Yes, use the text package [1].  More specifically, you want to read
your file to a ByteString bs and do "decodeUtf8With lenientDecode bs"
[2,3].  I strongly advise against using "ignore", it may pose a
security threat to your application.

Cheers!

[1] http://hackage.haskell.org/package/text
[2] 
http://hackage.haskell.org/packages/archive/text/0.11.1.5/doc/html/Data-Text-Encoding.html#v:decodeUtf8With
[3] 
http://hackage.haskell.org/packages/archive/text/0.11.1.5/doc/html/Data-Text-Encoding-Error.html#v:lenientDecode

-- 
Felipe.



------------------------------

Message: 4
Date: Wed, 14 Sep 2011 14:19:23 +0100
From: Anthony Campbell <[email protected]>
Subject: Re: [Haskell-beginners] Editing the layout hook
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=us-ascii

On 14 Sep 2011, Brandon Allbery wrote:
>    On Wed, Sep 14, 2011 at 04:30, Anthony Campbell <[email protected]>
>    wrote:
> 
>      A  A mylayoutHook = Full ||| noBorders (tabbed shrinkText defaultTConf)
>      ||| Accordion
>      xmonad.hs:7:58: Not in scope: `defaultTConf'
> 
>    There isn't a defaultTConf defined by XMonad.Layout.Tabbed. A You probably
>    want defaultTheme.
>    --
>    brandon s allbery A  A  A  A  A  A  A  A  A  A  A  A  A  A  A  A  A  A 
>    A [email protected]
>    wandering unix systems administrator (available) A  A  (412) 475-9364
>    vm/sms


I tried that, but no luck. But I think I may have a more fundamental
problem: whichever module I select to import, I get an error message
that it can't be found. But I do have the relevant package installed:

libghc-xmonad-contrib-dev - Extensions to xmonad

and they are present in

/usr/lib/haskell-packages/ghc/lib/xmonad-contrib-0.9.2/ghc-7.0.4/XMonad/Layout/


Is something misconfigured? The ghc executables are in my path in
/usr/bin.

Anthony


-- 
Anthony Campbell - [email protected] 
Microsoft-free zone - Using Debian GNU/Linux 
http://www.acampbell.org.uk - sample my ebooks at
http://www.smashwords.com/profile/view/acampbell




------------------------------

Message: 5
Date: Wed, 14 Sep 2011 15:34:01 +0000 (UTC)
From: Amy de Buitl?ir <[email protected]>
Subject: Re: [Haskell-beginners] Editing the layout hook
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=us-ascii

Try the command "ghc-pkg check". If that doesn't report any errors, then your
configuration is probably OK.




------------------------------

Message: 6
Date: Wed, 14 Sep 2011 17:35:44 +0100
From: Anthony Campbell <[email protected]>
Subject: Re: [Haskell-beginners] Editing the layout hook
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=iso-8859-1

On 14 Sep 2011, Amy de Buitl?ir wrote:
> Try the command "ghc-pkg check". If that doesn't report any errors, then your
> configuration is probably OK.
> 
> 
Thanks - no errors shown. 

Anthony



-- 
Anthony Campbell - [email protected] 
Microsoft-free zone - Using Debian GNU/Linux 
http://www.acampbell.org.uk - sample my ebooks at
http://www.smashwords.com/profile/view/acampbell




------------------------------

Message: 7
Date: Wed, 14 Sep 2011 18:48:29 -0400
From: Michael Craig <[email protected]>
Subject: [Haskell-beginners] How Best to Deal with Nested Monads?
To: [email protected]
Message-ID:
        <caha9zaeny3dpdmk+6ymfgsbmb4neqn9jfczwowphlhs_7kg...@mail.gmail.com>
Content-Type: text/plain; charset="iso-8859-1"

Say we've got these types

lst :: m [a]
getMB :: a -> m (Maybe b)
getC :: b -> m c

and we want to map getMB and getC over the elements of lst, all the while
discarding elements x where getMB x == Nothing.

(This could be generalized more by replacing Maybe with some monad m', but
let's run with Maybe because it's easy to talk about.)

The best I've got (after some help on IRC) is this not-so-easy-to-read
oneliner:

lst >>= (\x -> mapM (liftM (liftM getC) (getMB x)) >>= sequence . catMaybes

This is hard to read, but it's also bad because we run sequence twice (once
inside of mapM). If we want to do multiple things to each element of lst, it
would be nice to process each element completely before moving on to the
next.

Thoughts?

Best,
Mike S Craig
(908) 328 8030
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://www.haskell.org/pipermail/beginners/attachments/20110914/97eef322/attachment-0001.htm>

------------------------------

Message: 8
Date: Wed, 14 Sep 2011 19:46:02 -0400
From: David McBride <[email protected]>
Subject: Re: [Haskell-beginners] How Best to Deal with Nested Monads?
To: Michael Craig <[email protected]>
Cc: [email protected]
Message-ID:
        <can+tr43hhmaykqt4gne2yvl-kj-kn-na3tjpq-zo74ryqne...@mail.gmail.com>
Content-Type: text/plain; charset=ISO-8859-1

Try this?

import Data.Maybe (isNothing)
import Control.Monad (filterM)

lst >>= (fmap getC $ filterM $ fmap isNothing . getMB)

On Wed, Sep 14, 2011 at 6:48 PM, Michael Craig <[email protected]> wrote:
> Say we've got these types
> lst :: m [a]
> getMB :: a -> m (Maybe b)
> getC :: b -> m c
> and we want to map getMB and getC over the elements of lst, all the while
> discarding elements x where getMB x == Nothing.
> (This could be generalized more by replacing Maybe with some monad m', but
> let's run with Maybe because it's easy to talk about.)
> The best I've got (after some help on IRC) is this not-so-easy-to-read
> oneliner:
> lst >>= (\x -> mapM (liftM (liftM getC) (getMB x)) >>= sequence . catMaybes
> This is hard to read, but it's also bad because we run sequence twice (once
> inside of mapM). If we want to do multiple things to each element of lst, it
> would be nice to process each element completely before moving on to the
> next.
> Thoughts?
> Best,
> Mike S Craig
> (908) 328 8030
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>
>



------------------------------

Message: 9
Date: Wed, 14 Sep 2011 20:18:50 -0400
From: Brent Yorgey <[email protected]>
Subject: Re: [Haskell-beginners] How Best to Deal with Nested Monads?
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=us-ascii

On Wed, Sep 14, 2011 at 06:48:29PM -0400, Michael Craig wrote:
> Say we've got these types
> 
> lst :: m [a]
> getMB :: a -> m (Maybe b)
> getC :: b -> m c
> 
> and we want to map getMB and getC over the elements of lst, all the while
> discarding elements x where getMB x == Nothing.
> 
> (This could be generalized more by replacing Maybe with some monad m', but
> let's run with Maybe because it's easy to talk about.)
> 
> The best I've got (after some help on IRC) is this not-so-easy-to-read
> oneliner:
> 
> lst >>= (\x -> mapM (liftM (liftM getC) (getMB x)) >>= sequence
> . catMaybes

How about this:

  lst >>= (mapM getMB >=> (return . catMaybes) >=> mapM getC)

Everyone always forgets about (>=>).

> This is hard to read, but it's also bad because we run sequence twice (once
> inside of mapM). If we want to do multiple things to each element of lst, it
> would be nice to process each element completely before moving on to the
> next.

I wouldn't worry about running sequence twice.  Processing things by
chaining whole-structure transformations is the Haskell Way (tm).  All
that business about "doing only one traversal" is for people
programming in strict languages to worry about. The compiler can often
turn a chain of wholesale transformations into a single traversal
anyway.  In short, I see no particular reason why it is "nice" to
process each element completely before moving on.  Isn't it nicer to
be able to think in a more modular style?

-Brent



------------------------------

Message: 10
Date: Wed, 14 Sep 2011 21:21:04 -0400
From: Michael Craig <[email protected]>
Subject: Re: [Haskell-beginners] How Best to Deal with Nested Monads?
To: Brent Yorgey <[email protected]>
Cc: [email protected]
Message-ID:
        <caha9zahp2tpxhjrs_5p5jn8ppgca4yqsz4eehsydaf_emku...@mail.gmail.com>
Content-Type: text/plain; charset="iso-8859-1"

Brent: Thanks for reminding me about (>=>). Far more readable! But regarding
the sequence thing: I can think of all sorts of reasons why we'd want to do
a single traversal. How about when lst is long or infinite? In general, it's
more useful to produce output incrementally than all at once at the end.

Mike S Craig
(908) 328 8030


On Wed, Sep 14, 2011 at 8:18 PM, Brent Yorgey <[email protected]>wrote:

> On Wed, Sep 14, 2011 at 06:48:29PM -0400, Michael Craig wrote:
> > Say we've got these types
> >
> > lst :: m [a]
> > getMB :: a -> m (Maybe b)
> > getC :: b -> m c
> >
> > and we want to map getMB and getC over the elements of lst, all the while
> > discarding elements x where getMB x == Nothing.
> >
> > (This could be generalized more by replacing Maybe with some monad m',
> but
> > let's run with Maybe because it's easy to talk about.)
> >
> > The best I've got (after some help on IRC) is this not-so-easy-to-read
> > oneliner:
> >
> > lst >>= (\x -> mapM (liftM (liftM getC) (getMB x)) >>= sequence
> > . catMaybes
>
> How about this:
>
>  lst >>= (mapM getMB >=> (return . catMaybes) >=> mapM getC)
>
> Everyone always forgets about (>=>).
>
> > This is hard to read, but it's also bad because we run sequence twice
> (once
> > inside of mapM). If we want to do multiple things to each element of lst,
> it
> > would be nice to process each element completely before moving on to the
> > next.
>
> I wouldn't worry about running sequence twice.  Processing things by
> chaining whole-structure transformations is the Haskell Way (tm).  All
> that business about "doing only one traversal" is for people
> programming in strict languages to worry about. The compiler can often
> turn a chain of wholesale transformations into a single traversal
> anyway.  In short, I see no particular reason why it is "nice" to
> process each element completely before moving on.  Isn't it nicer to
> be able to think in a more modular style?
>
> -Brent
>
> _______________________________________________
> 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/20110914/dfe14aa3/attachment.htm>

------------------------------

_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners


End of Beginners Digest, Vol 39, Issue 19
*****************************************

Reply via email to