For the Compleat Fan
cormullion
Posts: 2038 Joined: Tue Nov 29, 2005 8:28 pm
Location: latiitude 50N longitude 3W
Contact:
Post
by cormullion » Mon Mar 06, 2006 10:27 am
Please correct my faulty understanding of ref:
Code: Select all
(set 'data '("a" "b" "c" "d" "e" "f" "g" "a" "b" "c" "d" "e"))
(ref "b" data)
I'm expecting this to return (1 8), yet it returns (1). Why is this?
newdep
Posts: 2038 Joined: Mon Feb 23, 2004 7:40 pm
Location: Netherlands
Post
by newdep » Mon Mar 06, 2006 12:01 pm
'ref is not repeating and find first occeurens found also nested lists..
so if you like to search multple strings in your list you could use 'map or
>(index (lambda (x) (= "b" x)) '("a" "b" "c" "d" "e" "b"))
(1 5)
-- is not seeking in nested lists --
there are even more options..
Regards,
Norman.
-- (define? (Cornflakes))
cormullion
Posts: 2038 Joined: Tue Nov 29, 2005 8:28 pm
Location: latiitude 50N longitude 3W
Contact:
Post
by cormullion » Mon Mar 06, 2006 12:25 pm
Thanks - the great pleasure of newLISP is that there's always a more elegant solution that I hadn't thought of! ;-)
I was reading the manual and got the wrong idea:
ref searches for expression exp in list and returns a list of integer indexes or an empty list if the exp cannot be found.
but in fact it only finds the 'hierachy level' that it finds the exp in?
newdep
Posts: 2038 Joined: Mon Feb 23, 2004 7:40 pm
Location: Netherlands
Post
by newdep » Mon Mar 06, 2006 12:42 pm
yes its like that...
-- (define? (Cornflakes))
Lutz
Posts: 5289 Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:
Post
by Lutz » Mon Mar 06, 2006 3:32 pm
It says "list of indexes" because 'ref' is mostly used for multidimensional searches:
Code: Select all
(set 'L '(a b (c d e) f g))
(ref 'd L) => (2 1)
Lutz
cormullion
Posts: 2038 Joined: Tue Nov 29, 2005 8:28 pm
Location: latiitude 50N longitude 3W
Contact:
Post
by cormullion » Mon Mar 06, 2006 4:15 pm
How about:
ref searches for expression exp in list and returns a list of integer indexes sufficient to identify the locaion of the first occurrence in a multi-dimensional list. or an empty list if the exp cannot be found....
or something ;-)
nigelbrown
Posts: 429 Joined: Tue Nov 11, 2003 2:11 am
Location: Brisbane, Australia
Post
by nigelbrown » Mon Mar 06, 2006 8:05 pm
Yes, it is clearer to specifiy it finds only the first.
It may also be worth specifying the search is depth-first
viz
> (ref 'b '(a b (b c) b))
(1)
> (ref 'b '(a (b c) b))
(1 0)
>
Nigel