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

Pages: 1-

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: Anonymous 2011-06-28 4:43

>>1
Use (cons key value)
CL is a little outdated (50 years old, etc..).

Name: Anonymous 2011-06-28 4:44

newbie ass-question

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.

Name: Anonymous 2011-06-28 4:49

OP, you should consider pure-functional data structures, because modification of assoc-lists or hash-tables is dangerous.

Name: Anonymous 2011-06-28 5:22

>>4
Both McCarthy LISP and Sussman Scheme require at least four levels.

Name: Anonymous 2011-06-28 12:17

for key-value pairs use pairs, not sublists.

((a . 1) (b . 2) (c . 3))

Name: Anonymous 2011-06-28 13:42

>>2

NIGGERLOUT

Name: Anonymous 2011-06-28 15:23

>>1
(cadr (assoc 'a myalist))

Name: Anonymous 2011-06-28 15:54

>>7
see >>2

>>9
see >>4

Name: CHECK 'EM DUBS 2011-06-28 19:42

#CHECK EM DUBZ#

#CHECK EM DUBZ#

#CHECK EM DUBZ#

#CHECK EM DUBZ#

#CHECK EM DUBZ#

#CHECK EM DUBZ#

#CHECK EM DUBZ#

#CHECK EM DUBZ#

#CHECK EM DUBZ#

#CHECK EM DUBZ#

Name: Anonymous 2011-06-28 20:54

>>4
Thanks

>>2,7
Ok.  What is the advantage of doing ((k . v)) over ((k v)) ?

>>5
I'm too noob to understand that yet, but I will look into it.

Name: Anonymous 2011-06-28 21:03

>>12
Ok.  What is the advantage of doing ((k . v)) over ((k v)) ?
(cdr (assoc ...)) => v

>>5 is trolling.

Name: Anonymous 2011-06-28 21:19

Use CADR or SECOND, or just format your alist as (cons a b) == '(a . b)
>>12
What is the advantage of doing ((k . v)) over ((k v)) ?
A proper list is just a CONS whose CAR represents the start of the list and whose CDR represents the continuation of the list. If the CDR is NIL, it is the end of the list (NIL is the same as '()).
Dot notation just lets you represent a list's CDR element without making a proper list, so saying (k . v) is similar to saying (cons k v), while [/m](k v)[/m] is similar to saying (cons k (list 'v)) or (cons k (cons v nil)). It should also be noted that dot notation is a bit more extensive and you can just use it whenever you want to specify the last CDR, like this: '(a b . c). You may also want to learn about backquote and other fancy syntaxes, but pretty much all notation can be expanded into plain S-Expressions and atoms (you can play with the printer/reader to see underneath various objects if you so wish, for example:

CL-USER> '`(a ,b)
`(A ,B)
CL-USER> (setf *print-pretty* nil)
NIL
CL-USER> **
(SB-IMPL::BACKQ-LIST (QUOTE A) B)

SB-IMPL::BACKQ-LIST is functionally equivalent to LIST, but a different symbol is used so the implementation's pretty printer hook can properly print backquoted expressions (you could easily implement your own, this is supported in standard CL, and you can even see one such implementation in CLTL2 (book))

In general this should all be explained in the right books. What are you using? I would recommend having/reading Practical Common Lisp, Hyperspec, Common Lisp The Language 2, Art of the Metaobject Protocol. There are also various other introductory books such as Graham's ANSI CL, Common Lisp: A Gentle Introduction to Symbolic Computation, Land of Lisp, and quite a few others.

You should also read SICP if you haven't already and maybe also "LISP in Small Pieces".

Name: Anonymous 2011-06-29 2:26

the advantage of dotting key-value pairs to me is beauty. Lists are for when you might conceivably want more. key-value will always be two. So pair. It saves a cons cell anyway. (not that this is significant except aesthetically)

Name: Anonymous 2011-06-29 4:14

>>14
What are you using?

I've read through like the first half of Successful Lisp, and skimmed through various other parts.  I was looking at Practical Common Lisp and I think it explained things that SL didn't.  I also want to read CLTL 2 since I hear it's really good.

And yeah, I'm gonna do SICP sometime.  I did the first chapter or so about 3 years ago.

Name: Anonymous 2011-06-29 5:02

>>13
>>5 is trolling
He isn't. With side-effects you are trading safety for perfomance. And some problems (like backtracking) require pure-functional approach implicitly.

Name: Anonymous 2013-10-03 3:04

>>11
Nice dubs, bro

Name: Anonymous 2013-10-03 4:07

>>18
Its like you scavenge the old, rotted threads just to get a piece of dubs.
>Such is life of pathetic dubs hunter.

Name: Anonymous 2013-10-03 4:24

>>19
I found this thread mildly entertaining

http://dis.4chan.org/read/prog/1327869524

Name: Anonymous 2013-10-03 4:44

>>11
Stale dubs are dubs.

Come get some fresh dubs.

Name: Anonymous 2013-10-03 4:51

Why would you think such a thing is possible?
This is Lisp not some baby language that allows you to do things

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