Arc Forumnew | comments | leaders | submit | scn's commentslogin

That gives

  (subseq "hello" 0 -1)
  "hello"
I was going for

  (subseq "hello" 0 -1)
  "hell"
to match python's

  "hello"[:-1]
  "hell"

Are reverse strings 0 or -1 indexed? :)

-----

3 points by mdemare 6119 days ago | link

Both. They use ranges, with have the inclusive (..) and the exclusive notation:

    "ruby"[0 .. -1] #=> "ruby"
    "ruby"[0 ... -1] #=> "rub"

-----

2 points by nex3 6119 days ago | link

Ruby does it the other way:

  > "hello"[-3..-1]
  "llo"
However, given that we can do something like

  > (subseq "hello" -4)
  "ello"
if we want to take the last n characters, doing zero-based reverse indexing might make more sense.

-----

2 points by oddbod 6118 days ago | link

Tcl lrange: http://tcl.tk/man/tcl8.5/TclCmd/lrange.htm

    (subseq "hello" 0 'end)
    "hello"
    (subseq "hello" 0 -1)
    "hell"

-----

2 points by rapp 6119 days ago | link

Your 100% right. I was blindly matching the test case:

    > (subseq "foobar" 2 -2)
    "oba"

-----


This is the subseq from arc.arc with an extra (commented) line that would allow -ve end parameters.

  (def subseq (seq start (o end (len seq)))
  ; (if (< end 0) (= end (+ (len seq) end)))
    (if (isa seq 'string)
      (let s2 (newstring (- end start))
        (for i 0 (- end start 1)
          (= (s2 i) (seq (+ start i))))
        s2)
      (firstn (- end start) (nthcdr start seq))))

-----