Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Lisp newbie-ass question (CL)

Name: Anonymous 2011-06-28 4:29

I want to return the value from a list that looks like(key value). I've got an alist like ((a 1) (b 2) (c 3)) (as alists go), and I get a value given a key with assoc, but the returned value for (assoc 'a myalist) is (A 1).

I can get just the key from that with cdr, but that will return (1), still a list, and I might want to use that value eg. for addition.  So I do: (car (cdr (assoc 'a myalist))) to get 1

Is this as good as any other way?  Or is there some other preferred way of doing it, maybe with 1 function instead of (car (cdr (key val)) or something?

I'm really new to Lisp, so any useful information at all is appreciated.

Name: VIPPER 2011-06-28 4:44

Most implementations provide cxxr macros, like caar (car of car), cdar(cdr of car), caadr (car of car of cdr).
Meaning in this case you could use (cadr) instead of (car (cdr)), you have to check how many levels deep your implementaion provides, but im sure it has atlest 3 levels.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List