Inclusive-exclusive range

2009-03-04 Thread Mibu

Why does range in Clojure use an inclusive-exclusive range?

I'm aware of the traditional substring range convention, which always
puzzled me as to how an unintuitive and error-prone use became
cemented as the norm.

I'm not calling for a change in range. I'm just genuinely curious.

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
Clojure group.
To post to this group, send email to clojure@googlegroups.com
To unsubscribe from this group, send email to 
clojure+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/clojure?hl=en
-~--~~~~--~~--~--~---



Re: Inclusive-exclusive range

2009-03-04 Thread Joshua Fox
With pointer-based strings or arrays, as in C , it is natural to start at
index 0, so that you can do pointer arithmetic: address+0 is the first
character/item.
Then, if you have a string or array of length n, the last item is at n-1.

Joshua

On Wed, Mar 4, 2009 at 2:07 PM, Mibu mibu.cloj...@gmail.com wrote:


 Why does range in Clojure use an inclusive-exclusive range?

 I'm aware of the traditional substring range convention, which always
 puzzled me as to how an unintuitive and error-prone use became
 cemented as the norm.

 I'm not calling for a change in range. I'm just genuinely curious.

 


--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
Clojure group.
To post to this group, send email to clojure@googlegroups.com
To unsubscribe from this group, send email to 
clojure+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/clojure?hl=en
-~--~~~~--~~--~--~---



Re: Inclusive-exclusive range

2009-03-04 Thread Michael Wood

On Wed, Mar 4, 2009 at 2:07 PM, Mibu mibu.cloj...@gmail.com wrote:

 Why does range in Clojure use an inclusive-exclusive range?

 I'm aware of the traditional substring range convention, which always
 puzzled me as to how an unintuitive and error-prone use became
 cemented as the norm.

 I'm not calling for a change in range. I'm just genuinely curious.

For what it's worth, Python's range function works the same way.

-- 
Michael Wood esiot...@gmail.com

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
Clojure group.
To post to this group, send email to clojure@googlegroups.com
To unsubscribe from this group, send email to 
clojure+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/clojure?hl=en
-~--~~~~--~~--~--~---



Re: Inclusive-exclusive range

2009-03-04 Thread Joshua Fox
This is discussed, with references, here
http://en.wikipedia.org/wiki/Array#Index_of_the_first_element

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
Clojure group.
To post to this group, send email to clojure@googlegroups.com
To unsubscribe from this group, send email to 
clojure+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/clojure?hl=en
-~--~~~~--~~--~--~---



Re: Inclusive-exclusive range

2009-03-04 Thread Mibu

On Mar 4, 2:46 pm, Michael Wood esiot...@gmail.com wrote:
 On Wed, Mar 4, 2009 at 2:07 PM, Mibu mibu.cloj...@gmail.com wrote:
  Why does range in Clojure use an inclusive-exclusive range?
 For what it's worth, Python's range function works the same way.

I think Clojure's design leans towards what's right more than what's
custom even if it breaks old habits, so I am curious why wasn't this
bad habit broken as well. Is it just convention, bad as it is? Or
maybe I'm missing some hidden good reason for using this confusing (to
me) range over an inclusive range.

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
Clojure group.
To post to this group, send email to clojure@googlegroups.com
To unsubscribe from this group, send email to 
clojure+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/clojure?hl=en
-~--~~~~--~~--~--~---



Re: Inclusive-exclusive range

2009-03-04 Thread Mibu

Joshua, my puzzlement is not with the first element but the last.

For example, the (range -1 2) gives (-1 0 1).

On Mar 4, 3:06 pm, Joshua Fox joshuat...@gmail.com wrote:
 This is discussed, with references, 
 herehttp://en.wikipedia.org/wiki/Array#Index_of_the_first_element
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
Clojure group.
To post to this group, send email to clojure@googlegroups.com
To unsubscribe from this group, send email to 
clojure+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/clojure?hl=en
-~--~~~~--~~--~--~---



Re: Inclusive-exclusive range

2009-03-04 Thread Konrad Hinsen

On Mar 4, 2009, at 14:06, Mibu wrote:

 On Mar 4, 2:46 pm, Michael Wood esiot...@gmail.com wrote:
 On Wed, Mar 4, 2009 at 2:07 PM, Mibu mibu.cloj...@gmail.com wrote:
 Why does range in Clojure use an inclusive-exclusive range?
 For what it's worth, Python's range function works the same way.

 I think Clojure's design leans towards what's right more than what's
 custom even if it breaks old habits, so I am curious why wasn't this
 bad habit broken as well. Is it just convention, bad as it is? Or
 maybe I'm missing some hidden good reason for using this confusing (to
 me) range over an inclusive range.

I wouldn't call it a bad habit just because it's not what you expected.

The definition of range used in Clojure (and elsewhere) has some nice  
properties:

(=  n  (count (range n)))

(=  (- b a)  (count (range a b)))

(=  (concat (range a b) (range b c))  (range a c))

Their utility may not be obvious immediately, but if you write code  
that works a lot on indices, you will learn to appreciate them.

Konrad.


--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
Clojure group.
To post to this group, send email to clojure@googlegroups.com
To unsubscribe from this group, send email to 
clojure+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/clojure?hl=en
-~--~~~~--~~--~--~---



Re: Inclusive-exclusive range

2009-03-04 Thread Itay Maman

(+1 for Konrad's point regarding concat).

Two points, in favor of 0-based indexing (as opposed to 1-based)

When you look at a piece of code and see zero used as an index into
some custom-made collection, you immediately know that this is a
reference to the first item. Except for the rare cases of collections
with negative indexes, it is clear that 0 is the first element. An
index of 1 is ambiguous: It means either the first or the second
element, depending on the convention of the language as well as
whether the programmer who wrote the collection decided to follow the
convention.

The second point is somewhat less significant but it does have a
point: With 1-based you need to write (= i n) instead of ( i n). It
seems that programmers prefer to write less, hence  making 0-based
indexing more popular.

Just my 2c

--
Itay Maman
http://javadots.blogspot.com/


On Mar 4, 3:40 pm, Konrad Hinsen konrad.hin...@laposte.net wrote:
 On Mar 4, 2009, at 14:06, Mibu wrote:

  On Mar 4, 2:46 pm, Michael Wood esiot...@gmail.com wrote:
  On Wed, Mar 4, 2009 at 2:07 PM, Mibu mibu.cloj...@gmail.com wrote:
  Why does range in Clojure use an inclusive-exclusive range?
  For what it's worth, Python's range function works the same way.

  I think Clojure's design leans towards what's right more than what's
  custom even if it breaks old habits, so I am curious why wasn't this
  bad habit broken as well. Is it just convention, bad as it is? Or
  maybe I'm missing some hidden good reason for using this confusing (to
  me) range over an inclusive range.

 I wouldn't call it a bad habit just because it's not what you expected.

 The definition of range used in Clojure (and elsewhere) has some nice  
 properties:

         (=  n  (count (range n)))

         (=  (- b a)  (count (range a b)))

         (=  (concat (range a b) (range b c))  (range a c))

 Their utility may not be obvious immediately, but if you write code  
 that works a lot on indices, you will learn to appreciate them.

 Konrad.
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
Clojure group.
To post to this group, send email to clojure@googlegroups.com
To unsubscribe from this group, send email to 
clojure+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/clojure?hl=en
-~--~~~~--~~--~--~---



Re: Inclusive-exclusive range

2009-03-04 Thread Meikel Brandmeyer

Hi,

Am 04.03.2009 um 14:06 schrieb Mibu:


On Mar 4, 2:46 pm, Michael Wood esiot...@gmail.com wrote:

On Wed, Mar 4, 2009 at 2:07 PM, Mibu mibu.cloj...@gmail.com wrote:

Why does range in Clojure use an inclusive-exclusive range?

For what it's worth, Python's range function works the same way.


I think Clojure's design leans towards what's right more than what's
custom even if it breaks old habits, so I am curious why wasn't this
bad habit broken as well. Is it just convention, bad as it is? Or
maybe I'm missing some hidden good reason for using this confusing (to
me) range over an inclusive range.


Edsger Dijkstra wrote up his opinion, why it should be like that:

http://www.cs.utexas.edu/users/EWD/transcriptions/EWD08xx/EWD831.html

Sincerely
Meikel



smime.p7s
Description: S/MIME cryptographic signature