LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 10-09-2011, 02:27 PM   #1
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,566
Blog Entries: 1

Rep: Reputation: 1026Reputation: 1026Reputation: 1026Reputation: 1026Reputation: 1026Reputation: 1026Reputation: 1026Reputation: 1026
struggling with a function in Common Lisp


I'm having fun with The Land of Lisp. I've been trying to write the game that the book describes. I'm kind of stuck with the last function in this code:

Code:
(defparameter *nodes* '((living-room (you are in the living-room.
                            a wizard is snoring loudly on the couch.))
                        (garden (you are in a beautiful garden.
                            there is a well in front of you.))
                        (attic (you are in the attic.
                            there is a giant welding torch in the corner.))))

(defun describe-location (location nodes)
   (cadr (assoc location nodes)))

(defparameter *edges* '((living-room (garden west door)  
                                     (attic upstairs ladder))
                        (garden (living-room east door))
                        (attic (living-room downstairs ladder))))

(defun describe-path (edge)
  `(there is a ,(caddr edge) going ,(cadr edge) from here.))

(defun describe-paths (location edges)
  (apply #'append (mapcar #'describe-path (cdr (assoc location edges)))))

(defparameter *objects* '(whiskey bucket frog chain))

(defparameter *object-locations* '((whiskey living-room)
                                   (bucket living-room)
                                   (chain garden)
                                   (frog garden)))

(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)))
Code:
CL-USER> (objects-at 'living-room *objects* *object-locations*)
(WHISKEY BUCKET)
Where does it say in the objects-at function to GO OVER all the elements of the obj-locs list? eq? No. cadr? No. Assoc? No.
Where does the obj variable take its value from? And what is it?
I believe I understand the function's separate parts but as a whole I don't think I know how exactly it filters *object-locations* to extract the objects in the living room.

Thanks for your patience (beware: newbie)
 
Old 10-09-2011, 08:13 PM   #2
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian
Posts: 2,492

Rep: Reputation: 850Reputation: 850Reputation: 850Reputation: 850Reputation: 850Reputation: 850Reputation: 850
Code:
(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)))
Quote:
Where does it say in the objects-at function to GO OVER all the elements of the obj-locs list? eq? No. cadr? No. Assoc? No.
The function remove-if-not takes a list and goes over the list and returns a new list with those elements that fail the predicate removed.

Quote:
Where does the obj variable take its value from? And what is it?
labels defines a local function called at-loc-p that takes one parameter, obj. It is called by remove-if-not for every value of objs.
 
Old 10-10-2011, 04:04 AM   #3
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,566
Blog Entries: 1

Original Poster
Rep: Reputation: 1026Reputation: 1026Reputation: 1026Reputation: 1026Reputation: 1026Reputation: 1026Reputation: 1026Reputation: 1026
Thanks a lot. For some reason I assumed that remove-if-not would not be responsible for it. I was wrong.

Thanks for the explanation.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Common Lisp tuning for Gedit entz Linux - Software 12 08-30-2010 08:46 AM
Common Lisp functions. zaxonus Programming 2 07-09-2010 04:30 PM
can I make C + common lisp simpler than C alone? ta0kira Programming 7 03-08-2009 10:56 PM
Programming Ideas in Common Lisp Trizon Programming 1 03-03-2008 02:07 PM
Common Lisp stand alone question Trizon Programming 5 05-23-2007 09:07 AM


All times are GMT -5. The time now is 02:09 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration