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. splitAt and negative values (Stayvoid)
2. Re: splitAt and negative values (Brent Yorgey)
3. Re: splitAt and negative values (Stephen Tetley)
4. Re: splitAt and negative values (Tom Murphy)
5. Re: splitAt and negative values (Brandon Allbery)
6. Who discovered the fold operation? (Costello, Roger L.)
7. Re: Who discovered the fold operation? (Tony Morris)
----------------------------------------------------------------------
Message: 1
Date: Thu, 6 Sep 2012 21:24:13 +0400
From: Stayvoid <[email protected]>
Subject: [Haskell-beginners] splitAt and negative values
To: [email protected]
Message-ID:
<CAK5fS_GfyY7yog82droYJxY7fdi=ehcxvu+z8xg17y7dma3...@mail.gmail.com>
Content-Type: text/plain; charset=ISO-8859-1
Hello,
Could you explain this behaviour?
> splitAt (-1) "Foobar"
("","Foobar")
> splitAt (-100) "Foobar"
("","Foobar")
I don't understand why splitAt (-1) "Foobar" doesn't output this:
("r", "Fooba")
or this:
("", "Fooba")
How does it work?
Python's list slicing looks similar, but results for negative values differ:
>>> a = "Foobar"
>>> a[:-1]
>>> 'Fooba'
Thanks
------------------------------
Message: 2
Date: Thu, 6 Sep 2012 17:12:47 -0400
From: Brent Yorgey <[email protected]>
Subject: Re: [Haskell-beginners] splitAt and negative values
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=us-ascii
On Thu, Sep 06, 2012 at 09:24:13PM +0400, Stayvoid wrote:
> Hello,
>
> Could you explain this behaviour?
>
> > splitAt (-1) "Foobar"
> ("","Foobar")
> > splitAt (-100) "Foobar"
> ("","Foobar")
That is simply how splitAt is defined. It treats negative values as
if they were 0.
If you are looking for an explanation of *why* this behavior was
chosen, it is probably because Haskell lists are really *singly-linked
lists*, not arrays. Taking something off the end of a list takes O(n)
time and requires making a copy of the entire list. So it wouldn't be
a good idea to encourage it.
If you really need this sort of functionality often then perhaps you
should be using a different type, such as Data.Text, which has
functions for doing things efficiently at the end of some text:
http://hackage.haskell.org/package/text
-Brent
------------------------------
Message: 3
Date: Thu, 6 Sep 2012 22:14:09 +0100
From: Stephen Tetley <[email protected]>
Subject: Re: [Haskell-beginners] splitAt and negative values
To: Stayvoid <[email protected]>
Cc: [email protected]
Message-ID:
<cab2tprdm3xd9o0a2d0s3wxctte+fwhhccbjzra0bti2tvrn...@mail.gmail.com>
Content-Type: text/plain; charset=ISO-8859-1
In Haskell "positional" list functions treat negative values as zero:
> take (-1) "abc"
""
> drop (-2) "xyz"
"xyz"
So, its idiomatic of splitAt to follow Haskell's precedent rather than Python's.
------------------------------
Message: 4
Date: Thu, 6 Sep 2012 19:48:46 -0400
From: Tom Murphy <[email protected]>
Subject: Re: [Haskell-beginners] splitAt and negative values
To: Stayvoid <[email protected]>
Cc: [email protected]
Message-ID:
<cao9q0tvrm9ur3_m2ngsjzd4tctwas31nigdwxa8ejyht2s_...@mail.gmail.com>
Content-Type: text/plain; charset="iso-8859-1"
One problem you might run into is:
a = 4
b = 5
splitAt (a - b) [1..]
If you don't know what a or b are beforehand, you might be opening up a can
of uncomputable worms.
Tom
On Sep 6, 2012 1:25 PM, "Stayvoid" <[email protected]> wrote:
> Hello,
>
> Could you explain this behaviour?
>
> > splitAt (-1) "Foobar"
> ("","Foobar")
> > splitAt (-100) "Foobar"
> ("","Foobar")
>
> I don't understand why splitAt (-1) "Foobar" doesn't output this:
> ("r", "Fooba")
> or this:
> ("", "Fooba")
>
> How does it work?
>
> Python's list slicing looks similar, but results for negative values
> differ:
> >>> a = "Foobar"
> >>> a[:-1]
> >>> 'Fooba'
>
> Thanks
>
> _______________________________________________
> 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/20120906/6770f82a/attachment-0001.htm>
------------------------------
Message: 5
Date: Thu, 6 Sep 2012 22:06:57 -0400
From: Brandon Allbery <[email protected]>
Subject: Re: [Haskell-beginners] splitAt and negative values
To: Stayvoid <[email protected]>
Cc: [email protected]
Message-ID:
<CAKFCL4Vad0MRj73gkB98Vrp46N7JUFg8akNk8Z-CP+cGKV1=g...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"
On Thu, Sep 6, 2012 at 1:24 PM, Stayvoid <[email protected]> wrote:
> > splitAt (-1) "Foobar"
> ("","Foobar")
>
As others have mentioned, this falls out of the way Haskell implements
lists; Perl and Python "lists" are more like Vector, and the end of the
list is easier to locate and operate on.
It's also worth noting that experience with Perl is that the negative index
thing is prone to error (a fencepost error reverses the function's
behavior) and Perl 6 has ditched it in favor of a more explicit syntax for
the negative index mechanism.
--
brandon s allbery [email protected]
wandering unix systems administrator (available) (412) 475-9364 vm/sms
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://www.haskell.org/pipermail/beginners/attachments/20120906/46056b1e/attachment-0001.htm>
------------------------------
Message: 6
Date: Fri, 7 Sep 2012 08:58:09 +0000
From: "Costello, Roger L." <[email protected]>
Subject: [Haskell-beginners] Who discovered the fold operation?
To: "[email protected]" <[email protected]>
Message-ID:
<[email protected]>
Content-Type: text/plain; charset="us-ascii"
Hi Folks,
Scientists look for recurring patterns in nature. Once a pattern is discovered,
its essential ingredients are identified and formalized into a law or
mathematical equation. Discovery of a pattern is important and those
individuals who make such discoveries are rightfully acknowledged in our annals
of science.
Scientists are not the only ones that look for recurring patterns, so do
functional programmers. Once a pattern is discovered, its essential ingredients
are identified and formalized into a function.
Long ago someone in the functional programming community discovered a pattern
that has become known as the fold operation. It was an important discovery,
on-par with some scientific discoveries.
Who discovered the fold operation? When?
/Roger
------------------------------
Message: 7
Date: Fri, 07 Sep 2012 19:09:38 +1000
From: Tony Morris <[email protected]>
Subject: Re: [Haskell-beginners] Who discovered the fold operation?
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1
On 07/09/12 18:58, Costello, Roger L. wrote:
> Hi Folks,
>
> Scientists look for recurring patterns in nature. Once a pattern is
> discovered, its essential ingredients are identified and formalized into a
> law or mathematical equation. Discovery of a pattern is important and those
> individuals who make such discoveries are rightfully acknowledged in our
> annals of science.
>
> Scientists are not the only ones that look for recurring patterns, so do
> functional programmers. Once a pattern is discovered, its essential
> ingredients are identified and formalized into a function.
>
> Long ago someone in the functional programming community discovered a pattern
> that has become known as the fold operation. It was an important discovery,
> on-par with some scientific discoveries.
>
> Who discovered the fold operation? When?
>
> /Roger
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
See Church-Turing Thesis.
--
Tony Morris
http://tmorris.net/
------------------------------
_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners
End of Beginners Digest, Vol 51, Issue 10
*****************************************