Stuck on a cadr on page 78 || PLEASE HELP!! URGENT!! EXCLUSIVE!!! LEAKED!!!

68 views
Skip to first unread message

Tyrone Johnson

unread,
Aug 26, 2016, 5:24:26 PM8/26/16
to Land of Lisp
I understand all of this except for the cadr on line 3. I know how car, cdr, cadr, caddr, and cadadddadddadadddr work but I have no idea what its doing here. I was just wondering if anyone could provide my dumb ass with a super comprehensive breakdown of whats happening.

(defun objects-at (loc objs obj-locs)
    (labels ((at-loc-p (obj)
        (eq (cadr (assoc obj obj-locs)) loc)))
    (remove-if-not #'at-loc-p objs)))

Win Treese

unread,
Aug 26, 2016, 8:53:35 PM8/26/16
to land-o...@googlegroups.com
Hi, Tyrone.

Just from looking at what’s here, I think this is what’s going on:

obj-locs is a specially constructed list called an “association list”. It’s usually used like what is often now called a dictionary or map in other programming languages, so you can look up things associated with other things. Each member of obj-locs is probably something like the list (object location), which could have been created with (list object location).

assoc is the function that looks up obj in the obj-locs association list (also called an a-list). Calling assoc gives back the whole :

(assoc obj obj-locs) => (obj location)

What the code cares about is checking the location. The cdr of the result is a list (probably with one element, but it doesn’t matter much here). We actually want that single element, so we take the car to get it. In code,

(car (cdr location))

And cadr is just a shorter way to say this: (cadr location)

I hope this helps. It can take a while to build up the intuition about nested list structures and where things are, and it’s pretty common to end up with a list of one element when want you want is just the element.

Good luck with Lisping!

Win Treese

--
You received this message because you are subscribed to the Google Groups "Land of Lisp" group.
To unsubscribe from this group and stop receiving emails from it, send an email to land-of-lisp...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages