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: question on layout (Maur??cio) 2. decorate-sort-undecorate in haskell (Ivan Uemlianin) 3. FoldL/R Reducing List (Waaaggh) 4. Re: decorate-sort-undecorate in haskell (Daniel Fischer) 5. Re: FoldL/R Reducing List (Daniel Fischer) 6. Re: FoldL/R Reducing List (Waaaggh) 7. Re: FoldL/R Reducing List (Chadda? Fouch?) 8. Updating lists inside another list (Aaron MacDonald) 9. Re: Updating lists inside another list (Lee Duhem) ---------------------------------------------------------------------- Message: 1 Date: Sun, 21 Jun 2009 15:25:13 -0300 From: Maur??cio <briqueabra...@yahoo.com> Subject: [Haskell-beginners] Re: question on layout To: beginners@haskell.org Message-ID: <h1ltu9$8v...@ger.gmane.org> Content-Type: text/plain; charset=ISO-8859-1; format=flowed >>> do >>> a <- x >>> let b = a >>> y b >>> z >>> >>> expands to >>> >>> do {a <- x ; let {b = a} in do {y b >> z}} >> I'm curious as to where the second `do' came from? > Well, the above translation isn't quite correct, the second 'do' > wouldn't come until later. The point is that 'do { let x = y; foo }' > translates to 'let x = y in do { foo }'. Exatly, I should have checked better that example. I just thought it worth to show how 'let' translates in a 'do' expression, because it caused me a lot of trouble when I learned Haskell. Since I had read that 'do' expressions are supposed to chain (Monad m) => (m a) elements, I assumed from this use of 'let' that: (WARNING: WRONG ASSUMPTIONS!) * 'let a = b' has type (Monad m) => (m x), x the type of a and b. * Since 'let' is used in let expressions and also in do expressions, either Haskell allows redefinition of reserved keywords or 'let' is not a reserved keyword and there's some way in Haskell to define constructs like 'let ... in ...' using more basic primitives. * Haskell has some kind of overloading allowing one word to be used in unrelated contexts. That's of course completely nonsense, but I would be happy if I could avoid others from running into this kind of misunderstanding. Best, Maurício ------------------------------ Message: 2 Date: Mon, 22 Jun 2009 11:03:02 +0100 From: Ivan Uemlianin <i...@llaisdy.com> Subject: [Haskell-beginners] decorate-sort-undecorate in haskell To: beginners@haskell.org Message-ID: <4a3f56d6.2090...@llaisdy.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Dear All I'm learning Haskell from a background in Python, and I'm just looking at the sort and sortBy functions in Data.List. In Python, the decorate-sort-undecorate pattern is a popular alternative to using an explicit compare function. For example, to sort a list of lists by length: def sortByLength(xs): dec_xs = [(len(x), x) for x in xs] dec_xs.sort() undec_xs = [x[1] for x in dec_xs] return undec_xs This is often preferred to something like: xs.sort(lambda x,y: len(x) > len(y)) # just used lambda so it fits on one line I think the reasoning is that plain sort() is done at C speed, whereas sort(func) is done in Python, so, depending on the list I suppose, dsu can be worth the trouble. (Sorry for vagueness). Anyway, I was wondering if dsu is popular in Haskell, and whether or when it would make sense to use dsu rather than sortBy. Here's a verbose dsu I wrote myself: sortDSU decFunc a = undecorate (sort (decorate decFunc a)) decorate decFunc [] = [] decorate decFunc (x:xs) = ( ((decFunc x), x) : decorate decFunc xs ) undecorate [] = [] undecorate ( (_, y) :xs) = ( y : undecorate xs ) Here's a terser and perhaps more idiomatic (but I think equivalent) dsu which I then found in a comment at the Real World Haskell website: dsuSort decFunc a = map snd (sort (zip (map decFunc a) a)) I have tested both functions with length and sum in ghci. So, do Haskell programmers use the decorate-sort-undecorate pattern? If not, why not? If so, when? Thanks and best wishes Ivan -- ============================================================ Ivan A. Uemlianin Speech Technology Research and Development i...@llaisdy.com www.llaisdy.com llaisdy.wordpress.com www.linkedin.com/in/ivanuemlianin "Froh, froh! Wie seine Sonnen, seine Sonnen fliegen" (Schiller, Beethoven) ============================================================ ------------------------------ Message: 3 Date: Mon, 22 Jun 2009 13:10:08 +0200 From: Waaaggh <waaa...@gmail.com> Subject: [Haskell-beginners] FoldL/R Reducing List To: beginners@haskell.org Message-ID: <ddb85f4f0906220410v48aa42bcnf14df2fb41330...@mail.gmail.com> Content-Type: text/plain; charset="iso-8859-1" Hi.how can i reduce list with foldl.r ? np (1,1,1,2,3,3,4,5,5) -> (1,2,3,4,5) ??? Azz. -------------- next part -------------- An HTML attachment was scrubbed... URL: http://www.haskell.org/pipermail/beginners/attachments/20090622/4c62f380/attachment-0001.html ------------------------------ Message: 4 Date: Mon, 22 Jun 2009 13:38:16 +0200 From: Daniel Fischer <daniel.is.fisc...@web.de> Subject: Re: [Haskell-beginners] decorate-sort-undecorate in haskell To: beginners@haskell.org Message-ID: <200906221338.16853.daniel.is.fisc...@web.de> Content-Type: text/plain; charset="iso-8859-1" Am Montag 22 Juni 2009 12:03:02 schrieb Ivan Uemlianin: > Dear All > > I'm learning Haskell from a background in Python, and I'm just looking > at the sort and sortBy functions in Data.List. In Python, the > decorate-sort-undecorate pattern is a popular alternative to using an > explicit compare function. For example, to sort a list of lists by length: > > def sortByLength(xs): > dec_xs = [(len(x), x) for x in xs] > dec_xs.sort() > undec_xs = [x[1] for x in dec_xs] > return undec_xs > > This is often preferred to something like: > > xs.sort(lambda x,y: len(x) > len(y)) # just used lambda so it fits > on one line > > I think the reasoning is that plain sort() is done at C speed, whereas > sort(func) is done in Python, so, depending on the list I suppose, dsu > can be worth the trouble. (Sorry for vagueness). > > Anyway, I was wondering if dsu is popular in Haskell, and whether or > when it would make sense to use dsu rather than sortBy. It's moderately popular. It makes much sense to use it, if the decoration function is expensive. >From http://www.haskell.org/haskellwiki/Blow_your_mind : map snd . sortBy (comparing fst) . map (length &&& id) -- the so called "Schwartzian Transform" for computationally more expensive -- functions. it even has a name :) If you used sortBy (comparing f) f x would be recalculated each time you compare x, it's only calculated once with the Schwartzian transform > > Here's a verbose dsu I wrote myself: > > sortDSU decFunc a = undecorate (sort (decorate decFunc a)) > > decorate decFunc [] = [] > decorate decFunc (x:xs) = ( ((decFunc x), x) : decorate decFunc xs ) > > undecorate [] = [] > undecorate ( (_, y) :xs) = ( y : undecorate xs ) > > Here's a terser and perhaps more idiomatic (but I think equivalent) dsu > which I then found in a comment at the Real World Haskell website: > > dsuSort decFunc a = map snd (sort (zip (map decFunc a) a)) more general: dsuSort decFun a = map snd . sortBy (comparing fst) $ zip (mp decFun a) a > > I have tested both functions with length and sum in ghci. > > So, do Haskell programmers use the decorate-sort-undecorate pattern? If > not, why not? If so, when? Sometimes. It's used if the decorating function is expensive, not if constructing and deconstructing tuples costs more than recalculating it (consider sortBy (comparing snd) as an extreme case). > > Thanks and best wishes > > Ivan ------------------------------ Message: 5 Date: Mon, 22 Jun 2009 13:41:18 +0200 From: Daniel Fischer <daniel.is.fisc...@web.de> Subject: Re: [Haskell-beginners] FoldL/R Reducing List To: beginners@haskell.org Message-ID: <200906221341.18263.daniel.is.fisc...@web.de> Content-Type: text/plain; charset="iso-8859-15" Am Montag 22 Juni 2009 13:10:08 schrieb Waaaggh: > Hi.how can i reduce list with foldl.r ? > > np (1,1,1,2,3,3,4,5,5) -> (1,2,3,4,5) ??? > Azz. First, these are tuples, not lists. Second, what are you trying to do? Implement nub via fold(l)r? Is http://www.haskell.org/haskellwiki/Homework_help relevant to your request? ------------------------------ Message: 6 Date: Mon, 22 Jun 2009 14:12:05 +0200 From: Waaaggh <waaa...@gmail.com> Subject: Re: [Haskell-beginners] FoldL/R Reducing List To: Beginners@haskell.org Message-ID: <ddb85f4f0906220512q1cc19ccajb3154cbc31100...@mail.gmail.com> Content-Type: text/plain; charset="iso-8859-1" i am trying to implement remowal of repetting elements from lists with foldeg. [1,1,1,2] -> [1,2] [1,1,2,2,3,1,1,1] -> [1,2,3,1] 2009/6/22 Daniel Fischer <daniel.is.fisc...@web.de> > Am Montag 22 Juni 2009 13:10:08 schrieb Waaaggh: > > Hi.how can i reduce list with foldl.r ? > > > > np (1,1,1,2,3,3,4,5,5) -> (1,2,3,4,5) ??? > > Azz. > > First, these are tuples, not lists. > Second, what are you trying to do? > Implement nub via fold(l)r? > Is http://www.haskell.org/haskellwiki/Homework_help relevant to your > request? > _______________________________________________ > Beginners mailing list > Beginners@haskell.org > http://www.haskell.org/mailman/listinfo/beginners > -------------- next part -------------- An HTML attachment was scrubbed... URL: http://www.haskell.org/pipermail/beginners/attachments/20090622/15c8a123/attachment-0001.html ------------------------------ Message: 7 Date: Mon, 22 Jun 2009 18:27:45 +0200 From: Chadda? Fouch? <chaddai.fou...@gmail.com> Subject: Re: [Haskell-beginners] FoldL/R Reducing List To: Waaaggh <waaa...@gmail.com> Cc: Beginners@haskell.org Message-ID: <e9350eaf0906220927o1748c98eg2b34712845aef...@mail.gmail.com> Content-Type: text/plain; charset=UTF-8 On Mon, Jun 22, 2009 at 2:12 PM, Waaaggh<waaa...@gmail.com> wrote: > i am trying to implement remowal of repetting elements from lists with fold > eg. [1,1,1,2] -> [1,2] > [1,1,2,2,3,1,1,1] -> [1,2,3,1] > It's pretty easy to do with foldr and a "variant" of cons (:), it's a little bit more tricky to do it with proper lazyness but nothing serious. -- Jedaï ------------------------------ Message: 8 Date: Mon, 22 Jun 2009 21:05:36 -0300 From: Aaron MacDonald <aaro...@eastlink.ca> Subject: [Haskell-beginners] Updating lists inside another list To: beginners@haskell.org Message-ID: <99f5ab4e-10f6-4202-8ad1-38e845bd2...@eastlink.ca> Content-Type: text/plain; charset=US-ASCII; format=flowed; delsp=yes I have a list of pairs. Each pair has a value and a list. So, the structure looks like this: [ (a, [b, c]), (b, [a, c]) ] What I want to do is be able to add an element to the inner list of the pair with the head value of my choosing. So, for the above list: > addToInnerList [ (a, [b, c]), (b, [a, c]) ] a d [ (a, [b, c, d]), (b, [a, c]) ] ------------------------------ Message: 9 Date: Tue, 23 Jun 2009 09:41:30 +0800 From: Lee Duhem <lee.du...@gmail.com> Subject: Re: [Haskell-beginners] Updating lists inside another list To: Aaron MacDonald <aaro...@eastlink.ca> Cc: beginners@haskell.org Message-ID: <da43c2e0906221841u737ebae0od799554ddbbd2...@mail.gmail.com> Content-Type: text/plain; charset=UTF-8 On Tue, Jun 23, 2009 at 8:05 AM, Aaron MacDonald<aaro...@eastlink.ca> wrote: > I have a list of pairs. Each pair has a value and a list. So, the structure > looks like this: > > [ (a, [b, c]), (b, [a, c]) ] > > What I want to do is be able to add an element to the inner list of the pair > with the head value of my choosing.  So, for the above list: > >> addToInnerList [ (a, [b, c]), (b, [a, c]) ] a d > [ (a, [b, c, d]), (b, [a, c]) ] > _______________________________________________ A simple recursive definition will do this: addToInnerList [] _ _ = [] addToInnerList (x@(h,l):xs) a b | h == a = (h,l++[b]):xs | otherwise = x : addToInnerList xs a b BTW, is it your homework? lee ------------------------------ _______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners End of Beginners Digest, Vol 12, Issue 10 *****************************************