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:  List -> Map -> List, to add duplicates?
      (Jonas Almstr?m Dureg?rd)
   2.  Monad operators: multiple parameters (Christopher Howard)
   3. Re:  Monad operators: multiple parameters (Daniel Fischer)
   4. Re:  Monad operators: multiple parameters (Mats Rauhala)
   5. Re:  xml light problem or pebkac (Manfred Lotz)
   6. Re:  xml light problem or pebkac (Daniel Fischer)
   7.  How to interpret the definition of MonadState ([email protected])
   8. Re:  How to interpret the definition of   MonadState
      (Daniel Fischer)


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

Message: 1
Date: Tue, 7 Jun 2011 13:47:39 +0200
From: Jonas Almstr?m Dureg?rd <[email protected]>
Subject: Re: [Haskell-beginners] List -> Map -> List, to add
        duplicates?
To: Tom Murphy <[email protected]>
Cc: beginners <[email protected]>
Message-ID: <[email protected]>
Content-Type: text/plain; charset="iso-8859-1"

Is this what you want? (note that it does not preserve ordering, but neither
does Map):

import Data.List
import Data.Function

noDups :: (Ord b, Num a) => [(a,b)] -> [(a,b)]
noDups = map sumGroup . groupBy ((==) `on` snd) . sortBy (compare `on` snd)
where
  sumGroup xs = (sum $ map fst xs, snd $ head xs)

If you use the fact that the list has at most one value per currency,
perhaps you should just keep the values in a map instead.

/J

On 7 June 2011 03:43, Tom Murphy <[email protected]> wrote:

> Hi All,
>     This seems like an inefficient way to do what I'm trying to do.
> I'd really appreciate any suggestions or comments:
>
>
> import qualified Data.Map as Map
>
> data Currency = Dollar
>              | Yen
>              | XP
>              | Health
>              | Street_Cred
>              | Peso
>    deriving (Show, Eq, Ord) -- why ord?
>
> withDups = [(30, Dollar), (-20, Street_Cred), (-2, Dollar), (30, XP),
> (15, Peso), (30, XP)]
>
>
> flipAssoc (a, b) = (b, a)
>
> noDups = Map.fromListWith (+) (map flipAssoc withDups)
>
> final = map flipAssoc $ Map.toList noDups
>
>
>
> Thanks for your time!
> Tom
>
> _______________________________________________
> 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/20110607/ae25b7d1/attachment-0001.htm>

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

Message: 2
Date: Tue, 07 Jun 2011 04:30:29 -0800
From: Christopher Howard <[email protected]>
Subject: [Haskell-beginners] Monad operators: multiple parameters
To: Haskell Beginners <[email protected]>
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1

In a reply to an earlier question, someone told me that "do" expressions
are simply syntactic sugar that expand to expressions with the >> and
>>= operators. I was trying to understand this (and the Monad class) better.

I see in my own experiments that this...

main = do ln1 <- getLine
          putStrLn ln1

translates to this:

main = getLine >>= \ln1 -> putStrLn ln1

However, what does this translate to?:

main = do ln1 <- getLine
          ln2 <- getLine
          putStrLn (ln1 ++ ln2)

-- 
frigidcode.com
theologia.indicium.us



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

Message: 3
Date: Tue, 7 Jun 2011 14:43:09 +0200
From: Daniel Fischer <[email protected]>
Subject: Re: [Haskell-beginners] Monad operators: multiple parameters
To: [email protected]
Message-ID: <[email protected]>
Content-Type: Text/Plain;  charset="us-ascii"

On Tuesday 07 June 2011, 14:30:29, Christopher Howard wrote:
> In a reply to an earlier question, someone told me that "do" expressions
> are simply syntactic sugar that expand to expressions with the >> and
> 
> >>= operators. I was trying to understand this (and the Monad class)
> >>better.
> 
> I see in my own experiments that this...
> 
> main = do ln1 <- getLine
>           putStrLn ln1
> 
> translates to this:
> 
> main = getLine >>= \ln1 -> putStrLn ln1

Shorter: getLine >>= putStrLn

An expression `\x -> foo x' is equivalent to `foo', using a lambda doesn't 
make things clearer in such cases, only if something more complicated is 
done with the argument(s).

> 
> However, what does this translate to?:
> 
> main = do ln1 <- getLine
>           ln2 <- getLine
>           putStrLn (ln1 ++ ln2)

That desugars via

getLine >>= \ln1 -> (do { ln2 <- getLine; putStrLn (ln1 ++ ln2); })

to

getLine >>= \ln1 -> (getLine >>= \ln2 -> putStrLn (ln1 ++ ln2))

Note that the outer parentheses aren't necessary.

do val <- foo
   bar
   baz val

desugars to

foo >>= \val -> do { bar; baz val; }

and the you can desugar the right hand side recursively.



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

Message: 4
Date: Tue, 7 Jun 2011 15:54:05 +0300
From: Mats Rauhala <[email protected]>
Subject: Re: [Haskell-beginners] Monad operators: multiple parameters
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset="us-ascii"

On 04:30 Tue 07 Jun     , Christopher Howard wrote:
> In a reply to an earlier question, someone told me that "do" expressions
> are simply syntactic sugar that expand to expressions with the >> and
> >>= operators. I was trying to understand this (and the Monad class) better.
> 
> I see in my own experiments that this...
> 
> main = do ln1 <- getLine
>           putStrLn ln1
> 
> translates to this:
> 
> main = getLine >>= \ln1 -> putStrLn ln1
> 
> However, what does this translate to?:
> 
> main = do ln1 <- getLine
>           ln2 <- getLine
>           putStrLn (ln1 ++ ln2)

It translates to:

main = getLine >>= \ln1 -> getLine >>= \ln2 -> putStrLn (ln1 ++ ln2)

-- 
Mats Rauhala
MasseR
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 198 bytes
Desc: not available
URL: 
<http://www.haskell.org/pipermail/beginners/attachments/20110607/9933fba8/attachment-0001.pgp>

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

Message: 5
Date: Tue, 7 Jun 2011 15:22:38 +0200
From: Manfred Lotz <[email protected]>
Subject: Re: [Haskell-beginners] xml light problem or pebkac
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=US-ASCII

On Mon, 6 Jun 2011 21:55:23 +0200
Daniel Fischer <[email protected]> wrote:


> 
> Problem, part 2, the real problem.
> 
> The point is that getXmlContent doesn't really parse the file yet, it 
> returns a thunk saying how to get the result from the file contents.
> Therefore, it doesn't need to read the entire file, just enough of it
> to find out whether parseXMLDoc returns a Just contents or a Nothing.
> 
> Then you close the handle, explicitly. That means, it's closed
> immediately, leaving the unread portion of the file unread. If the
> bla bla is long enough, the location tag is in the unread part, and
> when finally the search for that tag is forced by printing, the tag
> is not contained in the input.
> 
> 
> If you leave out the call to hClose, leaving the closing to
> hGetContents when it reaches the end of the file, the file contents
> is not truncated, and the location found.
> But then the file handle might remain half-closed longer than you
> wish (you could run out of file handles if you open a lot of files
> without explicitly closing them before the next [bunch] is opened).
> 

I ran out of handles in the first place that's why I had changed the
code just to got bitten by the IO laziness. Thanks for explaining it to
me. Changing the sample code to use either bracket or withBinaryFile
(implicitly bracket) makes it work indeed.


When I go back to my original program it seems I cannot get rid easily
of the laziness.

I process a bunch of xml files like this:
     
  import qualified Data.Map as M
  ...

  -- xmlfiles is a list of [FilePath]
  ht <- foldM insertXml M.empty xmlfiles 
  mapM_ printEntry (M.toList ht)


and in insertXML I now have


insertXml m xf = do
  U.withBinaryFile xf ReadMode
     (\handle -> do ct <- getXmlContent xf handle
                    let k = ctName ct
                    let m' = if k /= ""
                                then
                                   if M.lookup k m == Nothing 
                                      then M.insert k ct m
                                      else m
                                else m
                    return m')




I'm not sure if it is obvious where my mistake here lies. If not I have
to try to make this a working minimal example.


-- 
Thanks again,
Manfred






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

Message: 6
Date: Tue, 7 Jun 2011 16:09:41 +0200
From: Daniel Fischer <[email protected]>
Subject: Re: [Haskell-beginners] xml light problem or pebkac
To: [email protected]
Cc: Manfred Lotz <[email protected]>
Message-ID: <[email protected]>
Content-Type: Text/Plain;  charset="us-ascii"

On Tuesday 07 June 2011, 15:22:38, Manfred Lotz wrote:
> On Mon, 6 Jun 2011 21:55:23 +0200
> 
> Daniel Fischer <[email protected]> wrote:
> > Problem, part 2, the real problem.
> > 
> > The point is that getXmlContent doesn't really parse the file yet, it
> > returns a thunk saying how to get the result from the file contents.
> > Therefore, it doesn't need to read the entire file, just enough of it
> > to find out whether parseXMLDoc returns a Just contents or a Nothing.
> > 
> > Then you close the handle, explicitly. That means, it's closed
> > immediately, leaving the unread portion of the file unread. If the
> > bla bla is long enough, the location tag is in the unread part, and
> > when finally the search for that tag is forced by printing, the tag
> > is not contained in the input.
> > 
> > 
> > If you leave out the call to hClose, leaving the closing to
> > hGetContents when it reaches the end of the file, the file contents
> > is not truncated, and the location found.
> > But then the file handle might remain half-closed longer than you
> > wish (you could run out of file handles if you open a lot of files
> > without explicitly closing them before the next [bunch] is opened).
> 
> I ran out of handles in the first place that's why I had changed the
> code just to got bitten by the IO laziness. Thanks for explaining it to
> me. Changing the sample code to use either bracket or withBinaryFile
> (implicitly bracket) makes it work indeed.
> 
> 
> When I go back to my original program it seems I cannot get rid easily
> of the laziness.
> 
> I process a bunch of xml files like this:
> 
>   import qualified Data.Map as M
>   ...
> 
>   -- xmlfiles is a list of [FilePath]
>   ht <- foldM insertXml M.empty xmlfiles
>   mapM_ printEntry (M.toList ht)
> 
> 
> and in insertXML I now have
> 
> 
> insertXml m xf = do
>   U.withBinaryFile xf ReadMode
>      (\handle -> do ct <- getXmlContent xf handle
>                     let k = ctName ct
>                     let m' = if k /= ""
>                                 then
>                                    if M.lookup k m == Nothing
>                                       then M.insert k ct m
>                                       else m
>                                 else m
>                     return m')
> 
> 
> 
> 
> I'm not sure if it is obvious where my mistake here lies. If not I have
> to try to make this a working minimal example.

I wrote:
> If you force the result before closing the handle, enough of the file is
> read to find the desired elements,

You're not forcing the result. You can choose to force in getXmlContent by 
seq'ing on name, location and whatever else fields you have, then using a 
strict return (return $! m') in the withBinaryFile action is sufficient, or 
you can do all the forcing in the withBinaryFile action, for example

do ct@(CTest !nm !loc) <- getXmlContent xf handle
   ...
   return $! m'

with BangPatterns (use seq if you want your code to be portable to other 
implementations than GHC).



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

Message: 7
Date: Tue, 07 Jun 2011 10:59:44 -0400
From: [email protected]
Subject: [Haskell-beginners] How to interpret the definition of
        MonadState
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1; DelSp="Yes";
        format="flowed"

I am confused about the definition of MonadState. The opening line  
looks like this:

class Monad m => MonadState s m | m -> s where

It is the vertical bar | that confuses me. What does that mean? How  
would one read this line in "natural language", say English?

Thanks.

- J -




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

Message: 8
Date: Tue, 7 Jun 2011 17:08:12 +0200
From: Daniel Fischer <[email protected]>
Subject: Re: [Haskell-beginners] How to interpret the definition of
        MonadState
To: [email protected]
Message-ID: <[email protected]>
Content-Type: Text/Plain;  charset="us-ascii"

On Tuesday 07 June 2011, 16:59:44, [email protected] wrote:
> I am confused about the definition of MonadState. The opening line
> looks like this:
> 
> class Monad m => MonadState s m | m -> s where
> 
> It is the vertical bar | that confuses me. What does that mean? How
> would one read this line in "natural language", say English?

The vertical bar signifies a functional dependency (cf. the ghc users' 
guide for details). It says here, that the monad determines the type of the 
state.

Read it

class (demanding a Monad instance for m) MonadState m s, m determining s 
uniquely, where ...



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

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


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

Reply via email to