Lookup ?

Q&A's, tips, howto's
Locked
newdep
Posts: 2038
Joined: Mon Feb 23, 2004 7:40 pm
Location: Netherlands

Lookup ?

Post by newdep »

Hi Lutz,

Why are these examples below not working? the lookup explanation says:

"Finds in assoc-list an association the key element of which has the same value as exp ........."


The (sym "10") returns also an Atom called 10, or is it an evaluation issue
in 'lookup..?

The confusing part is that they are all atoms.. but not symbols ofcourse,
so does 'lookup like symbols? or only atoms? If only atoms then it should
work with (sym "10")




(setq order '( 10 (0 1 2 11 20 19 18 9)))
(setq R (sym "10"))



> (symbol? (order 0 0))
nil
> (atom? (order 0 0))
true
> (symbol? (sym "10"))
true
> (atom? (sym "10"))
true



> (lookup (sym "10") order)
nil

> (lookup R order)
nil

> (lookup (first (list (sym "10"))) order)
nil


This is working, but officialy '10 is an invalid atom! ircc...

> (lookup '10 order)
(0 1 2 11 20 19 18 9)

These are the only one working..but for me just the same as the examples above..

> (lookup 10 order)

> (lookup (first (flat '((10)))) order)
(0 1 2 11 20 19 18 9)


Norman.
-- (define? (Cornflakes))

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

Post by Lutz »

'order' in your example is not an association list. An association list is a list of lists. 'lookup' will check the first member to find the matching association, then takes by default the last element. If you define 'order' correctly it will work:

Code: Select all

> (setq order '( (10) (0 1 2 11 20 19 18 9)))
((10) (0 1 2 11 20 19 18 9))
> (lookup 10 order)
10
> (lookup 0 order)
9
> (lookup '0 order)
9
> (lookup '10 order) ; lookup evaluates args and takes the quote off 
10
> (lookup (sym 0) order) ; expected because 'order' has numbers
nil
> (lookup (sym 10) order) ; expected becuase 'order' has numbers
nil
> 
Lutz

Locked