Page 78 objects-at function

24 views
Skip to first unread message

cgfaas

unread,
Jun 1, 2018, 12:19:55 AM6/1/18
to Land of Lisp

Dear LISP'er

I am having trouble understanding how the at-loc-p function is being passed each object of the objs list individually. I can see how the assoc function compares each obj of the objs list with the full obj-locs list, as that is assoc purpose. What I don't get is how LISP is scrolling through the objs list and passing each element of objs to at-loc-p. 

at-loc-p is being passed a list, but is declared to take a single object (atom). This seems like it should crash, but instead the list is scrolled though. Does the the #' grant this power to cycle over a list?  

In C++ this would need some type of for-loop. I have commented in Step 1 and Step 2 below. 

Can anyone clue me in on how the single elements of objs are being passed through? I am confused by this property of LISP. 


;; object - objects
(defparameter *objects* '(whisky-bottle bucket frog chain))

;; object - object locations
(defparameter *object-locations* '((whisky-bottle living-room)
   (bucket living-room)
   (chain garden)
   (frog garden)))

;; function returns list of objects at location
(defun objects-at (loc objs obj-locs)
  (labels ((at-loc-p (obj)  ;; Step 2: at-loc-p somehow runs once for each obj in objs, but only takes a single obj
     (eq (cadr (assoc obj obj-locs)) loc))) 
  (remove-if-not #'at-loc-p objs)))  ;; Step 1: remove-if-not passes the full list of objs to at-loc-p above

(princ (objects-at 'living-room *objects* *object-locations*))


Thank you,

CGFAAS

Reply all
Reply to author
Forward
0 new messages