Send Beginners mailing list submissions to beginners@haskell.org 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 beginners-requ...@haskell.org
You can reach the person managing the list at beginners-ow...@haskell.org When replying, please edit your Subject line so it is more specific than "Re: Contents of Beginners digest..." Today's Topics: 1. Re: Syntactic sugar to insert into a Map? (Markus L?ll) 2. Why does this list comprehension return an empty list? (Costello, Roger L.) 3. Re: Why does this list comprehension return an empty list? (Frerich Raabe) 4. Re: Why does this list comprehension return an empty list? (Emanuel Koczwara) 5. Re: Why does this list comprehension return an empty list? (Magnus Therning) 6. Shorten this code (Nadav Chernin) ---------------------------------------------------------------------- Message: 1 Date: Mon, 22 Jul 2013 23:49:51 -0400 From: Markus L?ll <markus.l...@gmail.com> To: The Haskell-Beginners Mailing List - Discussion of primarily beginner-level topics related to Haskell <beginners@haskell.org> Subject: Re: [Haskell-beginners] Syntactic sugar to insert into a Map? Message-ID: <CALdaiuAm8yVAve-+pK9Ung6Wf9+0K3wNsvTKgb=1gc5hs0b...@mail.gmail.com> Content-Type: text/plain; charset=ISO-8859-1 Hi Martin, I think given that integers are converted to fromInteger x and that strings can be overloaded you could implement something like this with a state or a writer monad: mySet = do 1 2 3 4 5 6 To do this you'll need to implement the appropriate classes. But I guess it's more a hack towards getting what you want than to being true to what a monad is. On Mon, Jul 22, 2013 at 3:06 AM, Ozgur Akgun <ozgurak...@gmail.com> wrote: > Hi. > > On 21 July 2013 17:23, martin <martin.drautzb...@web.de> wrote: >> >> I just want to insert several key/value pairs, something like >> >> Map.empty >> insert key1 val1 >> insert key2 val2 >> return theMap > > > If you really want this syntax, what about using the writer monad: > > import Control.Monad.Writer > import qualified Data.Map as M > > m = execWriter $ do > insert 1 "foo" > insert 2 "bar" > where insert k v = tell (M.singleton k v) > -- m = fromList [(1,"foo"),(2,"bar")] > > > Hope this helps, > Ozgur > > > _______________________________________________ > Beginners mailing list > Beginners@haskell.org > http://www.haskell.org/mailman/listinfo/beginners > -- Markus L?ll ------------------------------ Message: 2 Date: Tue, 23 Jul 2013 08:53:28 +0000 From: "Costello, Roger L." <coste...@mitre.org> To: "beginners@haskell.org" <beginners@haskell.org> Subject: [Haskell-beginners] Why does this list comprehension return an empty list? Message-ID: <b5fee00b53cf054aa8439027e8fe17751efb2...@imcmbx04.mitre.org> Content-Type: text/plain; charset="us-ascii" Hi Folks, I have a list of singletons: xs = [("a")] f is a function that, given an argument x, it returns the argument: f x = x g is a function that, given an argument x, it returns the empty list: g x = [] I have a list comprehension that extracts the singletons from xs using f and g, and creates a pair from their output: [(a,b) | a <- f xs, b <- g xs] I executed this and the result is the empty list: [] That is odd. Why is the empty list the result? Consider this list comprehension that extracts the singletons from xs using f only, and creates a pair by matching up the singleton from xs with the empty list: [(a,[]) | a <- f xs] The result is this: [("a",[])] That is the result that I expected to get with the first list comprehension. Would you explain why this: [(a,b) | a <- f xs, b <- g xs] yields [], whereas this: [(a,[]) | a <- f xs] yields [("a",[])]? Also, how would I modify this: [(a,b) | a <- f xs, b <- g xs] so that it produces this: [("a",[])] /Roger ------------------------------ Message: 3 Date: Tue, 23 Jul 2013 11:08:10 +0200 From: Frerich Raabe <ra...@froglogic.com> To: The Haskell-Beginners Mailing List - Discussion of primarily beginner-level topics related to Haskell <beginners@haskell.org> Subject: Re: [Haskell-beginners] Why does this list comprehension return an empty list? Message-ID: <355f8941a5723081406da1acb5d4b...@roundcube.froglogic.com> Content-Type: text/plain; charset=UTF-8; format=flowed On 2013-07-23 10:53, Costello, Roger L. wrote: > I have a list of singletons: > > xs = [("a")] > > f is a function that, given an argument x, it returns the argument: > > f x = x > > g is a function that, given an argument x, it returns the empty list: > > g x = [] > > I have a list comprehension that extracts the singletons from xs > using f and g, and creates a pair from their output: > > [(a,b) | a <- f xs, b <- g xs] > > I executed this and the result is the empty list: > > [] > > That is odd. Why is the empty list the result? Consider how the list comprehension looks like when written as monadic code: f xs >>= \a -> g xs >>= \b -> return (a,b) I.e. "for each element 'a' drawn from the result of "f sx", draw an element 'b' from the result of "g xs" and yield a new element "(a,b)". So the resulting list will always be 'length (f xs) * length (g xs)' items in length. Consequently, if either of the two lists is empty, the result will be empty. -- Frerich Raabe - ra...@froglogic.com www.froglogic.com - Multi-Platform GUI Testing ------------------------------ Message: 4 Date: Tue, 23 Jul 2013 11:12:23 +0200 From: Emanuel Koczwara <poc...@emanuelkoczwara.pl> To: The Haskell-Beginners Mailing List - Discussion of primarily beginner-level topics related to Haskell <beginners@haskell.org> Subject: Re: [Haskell-beginners] Why does this list comprehension return an empty list? Message-ID: <1374570743.10100.5.camel@emanuel-laptop> Content-Type: text/plain; charset="utf-8" Hi, Dnia 2013-07-23, wto o godzinie 08:53 +0000, Costello, Roger L. pisze: > Hi Folks, > > I have a list of singletons: > > xs = [("a")] > > f is a function that, given an argument x, it returns the argument: > > f x = x > > g is a function that, given an argument x, it returns the empty list: > > g x = [] > > I have a list comprehension that extracts the singletons from xs using f and > g, and creates a pair from their output: > > [(a,b) | a <- f xs, b <- g xs] > > I executed this and the result is the empty list: > > [] > > That is odd. Why is the empty list the result? > Becouse g xs has zero elements. Look at this example: [(x,x) | x <- []] => [] Empty list has zero elements. > Consider this list comprehension that extracts the singletons from xs using f > only, and creates a pair by matching up the singleton from xs with the empty > list: > > [(a,[]) | a <- f xs] > > The result is this: > > [("a",[])] > > That is the result that I expected to get with the first list comprehension. > > Would you explain why this: > > [(a,b) | a <- f xs, b <- g xs] > > yields [], whereas this: > > [(a,[]) | a <- f xs] > > yields [("a",[])]? > > Also, how would I modify this: > > [(a,b) | a <- f xs, b <- g xs] > > so that it produces this: > > [("a",[])] Define g as: g xs = [[]] or maybe better: g _ = [[]] Now it has 1 element - empty list. Best regards, Emanuel -------------- next part -------------- A non-text attachment was scrubbed... Name: smime.p7s Type: application/x-pkcs7-signature Size: 5185 bytes Desc: not available URL: <http://www.haskell.org/pipermail/beginners/attachments/20130723/beb6595b/attachment-0001.bin> ------------------------------ Message: 5 Date: Tue, 23 Jul 2013 11:17:59 +0200 From: Magnus Therning <mag...@therning.org> To: The Haskell-Beginners Mailing List - Discussion of primarily beginner-level topics related to Haskell <beginners@haskell.org> Subject: Re: [Haskell-beginners] Why does this list comprehension return an empty list? Message-ID: <CAAExw5sUktB7BcNA_=an9ak8ci4ioajm34h12fbc1sl-_tp...@mail.gmail.com> Content-Type: text/plain; charset=UTF-8 On Tue, Jul 23, 2013 at 10:53 AM, Costello, Roger L. <coste...@mitre.org> wrote: > Hi Folks, > > I have a list of singletons: > > xs = [("a")] The type of that, as written, is [String]. > f is a function that, given an argument x, it returns the argument: > > f x = x > > g is a function that, given an argument x, it returns the empty list: > > g x = [] > > I have a list comprehension that extracts the singletons from xs using f and > g, and creates a pair from their output: > > [(a,b) | a <- f xs, b <- g xs] > > I executed this and the result is the empty list: > > [] > > That is odd. Why is the empty list the result? If you say it out loud what you are looking for it makes sense: construct a list tuples containing all combinations of 'a' and 'b' where 'a' is taken from the list 'f xs' and 'b' from the list 'g xs'. Now 'g xs' is empty, which means there is no possible value for 'b' to take, hence there is no way to create even a single such tuple. /M -- Magnus Therning OpenPGP: 0xAB4DFBA4 email: mag...@therning.org jabber: mag...@therning.org twitter: magthe http://therning.org/magnus ------------------------------ Message: 6 Date: Tue, 23 Jul 2013 13:41:32 +0300 From: Nadav Chernin <nadavcher...@gmail.com> To: beginners@haskell.org Subject: [Haskell-beginners] Shorten this code Message-ID: <cae80g0kqqw7rrw+fmq6jdetsbqg_6jfz6y_ayyowv4kyvfj...@mail.gmail.com> Content-Type: text/plain; charset="iso-8859-1" Hi, all Below is my solution to SPOJ->Polybius square<http://www.spoj.com/problems/POLYBIUS> Please try to shorten it: *import Data.Maybe* *d=[1..5]* *f s=unwords$map(\c->fromJust$lookup c((' ',""):('J',"24"):zip(['A'..'I']++['K'..'Z'])[show(x+10*y)|y<-d,x<-d]))s* *main=getLine>>(interact$unlines.map f.lines)* Thanks, Nadav -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://www.haskell.org/pipermail/beginners/attachments/20130723/ddffe711/attachment-0001.html> ------------------------------ Subject: Digest Footer _______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners ------------------------------ End of Beginners Digest, Vol 61, Issue 25 *****************************************