indexing

For the Compleat Fan
Locked
eddier
Posts: 289
Joined: Mon Oct 07, 2002 2:48 pm
Location: Blue Mountain College, MS US

indexing

Post by eddier »

I absolutely LOVE the implicit indexing Lutz. Just one question, can we do something with rest in a similar manar?

Eddie

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Post by Lutz »

I love it just as much :), you could see implicit indexing as a natural extension of traditional LISP/Scheme evaluation:

traditional LISP
-----------------
built-ins -> apply to args
lambda -> apply to args

newLISP adds
---------------
contexts -> apply default function on args
lists -> indexing with args

Theoretically you could extend this to any data type, i.e. numbers:

number -> slice list in args

(1 '(a b c d e f g)) => (b c d e f g) ; like (rest ...)
(2 '(a b c d e f g)) => (c d e f g) ; like (rest (rest ...))
(2 '(a b c d e f g) 3) => (c d e) ; like (slice ...)

have to think about this a little bit more ...

Lutz

eddier
Posts: 289
Joined: Mon Oct 07, 2002 2:48 pm
Location: Blue Mountain College, MS US

Post by eddier »

Would something like

Code: Select all

(0 3 '(a b c d e)) => (a b c d)
(1 2 '(a b c d e)) => (b c)
?

Eddie

eddier
Posts: 289
Joined: Mon Oct 07, 2002 2:48 pm
Location: Blue Mountain College, MS US

Post by eddier »

Opps, I didn't see the slice up there. Wouldn't the semantics indicate

Code: Select all

(2 '(a b c d e f g) 3) =>
('(c d e f g) 3) =>
f
?

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Post by Lutz »

I think the implicit nrest is fine:

Code: Select all

(1 '(a b c d)) => (b c d)
(2 '(a b c d)) => (c d)
(10 '(a b c d)) => ()
But I am not happy with the slice (idx theList len) or (idx len theList), just doesn't feel right, but the second syntax may be the better one.

I implemented (undocumented, experimental, list only, no strings) the implicit nrest in the new development version 8.4.7 here: http://newlisp.org/downloads/development

Lutz

eddier
Posts: 289
Joined: Mon Oct 07, 2002 2:48 pm
Location: Blue Mountain College, MS US

Post by eddier »

I was thinking more

Code: Select all

(idx1 idx2 list) 

(2 4 '(a b c d e f g)) => '(c d e)
But I'm not sure. The only thing that might be confusing is the position of the number. I still get confused on set-nth and nth-set. There might be a similar problem with implicit indexing and slicing.

Eddie

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Post by Lutz »

Already did similar over the weekend, but length instead of idx2 this:

Code: Select all

(offset len lst)

(2 4 '(a b c d e f g)) => (c d e f)
so its's basically a slice with implicit indexing, and it also works with negative offsets:

Code: Select all

(-4 2 '(a b c d e f g)) => (d e)
and I also have the implicit index/rest now for negative index:

Code: Select all

(-3 '(a b c d e f g)) => (e f g)
and everything works on strings too.

Lutz

Locked