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: nub... almost (Christopher Howard)
2. Re: nub... almost (Alex Rozenshteyn)
3. Re: nub... almost (Benjamin Edwards)
4. Re: nub... almost (Alexander Dunlap)
5. Re: Confused by type constraints (Brent Yorgey)
----------------------------------------------------------------------
Message: 1
Date: Sat, 04 Jun 2011 08:52:56 -0800
From: Christopher Howard <[email protected]>
Subject: Re: [Haskell-beginners] nub... almost
To: Haskell Beginners <[email protected]>
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1
On 06/04/2011 12:20 AM, Iustin Pop wrote:
> On Fri, Jun 03, 2011 at 11:49:01PM -0800, Christopher Howard wrote:
>> On 06/03/2011 11:37 PM, Christopher Howard wrote:
>>> Weird request: For a certain application, the "nub" function from
>>> Data.List is exactly what I need... almost. Nub removes duplicates,
>>> keeping the /first/ occurrence of each element. However, I need a
>>> function that removes duplicates, keeping the /second/ occurrence of
>>> each element. There wouldn't happen to be a function already that does
>>> this, would there?
>>>
>>> I'm working with a custom type which is a member of Eq, but some of the
>>> data in the type is not used in the comparison. So it is important which
>>> of the two "duplicates" are actually saved.
>>>
>>
>> I should qualify this: By "second" I mean "last". Of course, it is
>> possible for there to be more than one duplicate of any given element in
>> a list. E.g., if there are three identical elements in a list, I want
>> the third one, not the second one.
>
> In that case, can't you simply revert the list, nub it, then revert
> again (i.e. reverse . nub . reverse)?
>
> regards,
> iustin
>
@iustin: Thanks, that should work well enough.
@Edwards: I'm not familiar with Data.Set, so I do not understand your
solution.
--
frigidcode.com
theologia.indicium.us
------------------------------
Message: 2
Date: Sat, 4 Jun 2011 12:59:35 -0400
From: Alex Rozenshteyn <[email protected]>
Subject: Re: [Haskell-beginners] nub... almost
To: Christopher Howard <[email protected]>
Cc: Haskell Beginners <[email protected]>
Message-ID: <[email protected]>
Content-Type: text/plain; charset="utf-8"
Also, (since I believe that sort is stable), (map last . group . sort)
should work, and may have different performance characteristics (but
requires an Ord constraint).
On Sat, Jun 4, 2011 at 12:52 PM, Christopher Howard <
[email protected]> wrote:
> On 06/04/2011 12:20 AM, Iustin Pop wrote:
> > On Fri, Jun 03, 2011 at 11:49:01PM -0800, Christopher Howard wrote:
> >> On 06/03/2011 11:37 PM, Christopher Howard wrote:
> >>> Weird request: For a certain application, the "nub" function from
> >>> Data.List is exactly what I need... almost. Nub removes duplicates,
> >>> keeping the /first/ occurrence of each element. However, I need a
> >>> function that removes duplicates, keeping the /second/ occurrence of
> >>> each element. There wouldn't happen to be a function already that does
> >>> this, would there?
> >>>
> >>> I'm working with a custom type which is a member of Eq, but some of the
> >>> data in the type is not used in the comparison. So it is important
> which
> >>> of the two "duplicates" are actually saved.
> >>>
> >>
> >> I should qualify this: By "second" I mean "last". Of course, it is
> >> possible for there to be more than one duplicate of any given element in
> >> a list. E.g., if there are three identical elements in a list, I want
> >> the third one, not the second one.
> >
> > In that case, can't you simply revert the list, nub it, then revert
> > again (i.e. reverse . nub . reverse)?
> >
> > regards,
> > iustin
> >
>
> @iustin: Thanks, that should work well enough.
>
> @Edwards: I'm not familiar with Data.Set, so I do not understand your
> solution.
>
> --
> frigidcode.com
> theologia.indicium.us
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>
--
Alex R
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://www.haskell.org/pipermail/beginners/attachments/20110604/4fc0d258/attachment-0001.htm>
------------------------------
Message: 3
Date: Sat, 4 Jun 2011 18:03:29 +0100
From: Benjamin Edwards <[email protected]>
Subject: Re: [Haskell-beginners] nub... almost
To: Christopher Howard <[email protected]>
Cc: Haskell Beginners <[email protected]>
Message-ID: <[email protected]>
Content-Type: text/plain; charset="iso-8859-1"
nub = toList . foldl (flip insert) empty
But it's crap because it doesn't respect the ordering on the list.
On 4 Jun 2011 17:53, "Christopher Howard" <[email protected]>
wrote:
On 06/04/2011 12:20 AM, Iustin Pop wrote:
> On Fri, Jun 03, 2011 at 11:49:01PM -0800, Christopher Ho...
@iustin: Thanks, that should work well enough.
@Edwards: I'm not familiar with Data.Set, so I do not understand your
solution.
--
frigidcode.com
theologia.indicium.us
_______________________________________________
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/20110604/a1568454/attachment-0001.htm>
------------------------------
Message: 4
Date: Sat, 4 Jun 2011 11:57:17 -0700
From: Alexander Dunlap <[email protected]>
Subject: Re: [Haskell-beginners] nub... almost
To: Benjamin Edwards <[email protected]>
Cc: Haskell Beginners <[email protected]>
Message-ID: <[email protected]>
Content-Type: text/plain; charset=UTF-8
Why? According to Data.Set documentation, "If the set already contains
an element equal to the given value, it is replaced with the new
value." So as long as you insert into the set starting from the front
of the list and proceeding towards the back, you will end up with the
last element prevailing.
Alex
On 4 June 2011 10:03, Benjamin Edwards <[email protected]> wrote:
> nub = toList . foldl (flip insert) empty
>
> But it's crap because it doesn't respect the ordering on the list.
>
> On 4 Jun 2011 17:53, "Christopher Howard"
> <[email protected]> wrote:
>
> On 06/04/2011 12:20 AM, Iustin Pop wrote:
>> On Fri, Jun 03, 2011 at 11:49:01PM -0800, Christopher Ho...
>
> @iustin: Thanks, that should work well enough.
>
> @Edwards: I'm not familiar with Data.Set, so I do not understand your
> solution.
>
> --
> frigidcode.com
> theologia.indicium.us
>
> _______________________________________________
>
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>
>
------------------------------
Message: 5
Date: Sat, 4 Jun 2011 15:49:30 -0400
From: Brent Yorgey <[email protected]>
Subject: Re: [Haskell-beginners] Confused by type constraints
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=us-ascii
On Fri, Jun 03, 2011 at 05:13:43PM +0300, Guy wrote:
> On 03/06/2011 16:55, Daniel Schoepe wrote:
> >On Fri, 03 Jun 2011 16:04:05 +0300, Guy<[email protected]> wrote:
> >>I have some code resembling
> >>
> >>class Foo f where
> >> foo :: a -> f a
> >>
> >>data Bar f a = Foo f => Bar {bar :: f a}
> >>
> >>instance Foo (Bar f) where
> >> foo a = Bar $ foo a
> >>
> >>GHC insists on Foo f => in the instance declaration. However, the
> >>definition of Bar guarantees that this will always be
> >>the case. Why do I have to state this explicitly?
> >
> >The constraints in the data type declaration only affect the
> >constructors. It's a known issue, unfortunately there doesn't seem to be
> >a way around it.
>
> It seems to work for simpler examples. For example, this is OK
>
> data X x = Show x => X x
>
> s :: X x -> String
> s (X x) = show x
>
> I don't have to add a Show x => type constraint to s.
In this case, you are *destructing* an X value, and when you do, the
Show constraint on x becomes available, since the X constructor is
always paired with a Show constraint.
In your original example, you are *constructing* a Bar value. You
have stated that the Bar constructor must be accompanied by a Foo
constraint, but there is no such constraint in sight!
So the difference is between *destructing* a value (when the
associated constraint becomes available) and *constructing* one (when
the required constraint must be provided).
I don't know if I've explained that well. As others have mentioned,
the ability to specify constraints on constructors in this way is
probably going to be removed from the language anyway.
-Brent
------------------------------
_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners
End of Beginners Digest, Vol 36, Issue 10
*****************************************