Page 1 of 1

Where oh where do I go?

Posted: Sun May 13, 2012 6:22 pm
by g0blu324
So I've posted a question about this topic before, but I figured this is better than trying to get a response from a reply. Anyways, I've tried what someone suggested and now I'm confused on a small thing. So the input is a room in a house and the output is the rooms that are 1 room away from that input room. The only output working correctly is the last. Every other one returns nil. Why?

> (defun choices (loc)
(if (eql loc 'library)
(append '((east upstairs-bedroom)) '((south back-stairs)))
)
(if (eql loc 'dining-room)
(append '((east pantry)) '((west downstairs-bedroom)) '((north living-room))
))
(if (eql loc 'back-stairs)
(append '((north library)) '((south downstairs-bedroom))
))
(if (eql loc 'downstairs-bedroom)
(append '((north back-stairs)) '((east dining-room))
))
(if (eql loc 'upstairs-bedroom)
(append '((south front-stairs)) '((west library))
))
(if (eql loc 'front-stairs)
(append '((north upstairs-bedroom)) '((south living-room))
))
(if (eql loc 'living-room)
(append '((north front-stairs)) '((east kitchen)) '((south dining-room))
))
(if (eql loc 'kitchen)
(append '((west living-room)) '((south pantry))
))
(if (eql loc 'pantry)
(append '((north kitchen)) '((east dining-room))
)))

> (choices 'library)
NIL
> (choices 'kitchen)
NIL
> (choices 'pantry)
((NORTH KITCHEN) (EAST DINING-ROOM))
>

Re: Where oh where do I go?

Posted: Mon May 14, 2012 1:44 am
by Kompottkin
This is your code, correctly formatted:

Code: Select all

(defun choices (loc) 
  (if (eql loc 'library) 
      (append '((east upstairs-bedroom)) '((south back-stairs))))
  (if (eql loc 'dining-room) 
      (append '((east pantry)) '((west downstairs-bedroom)) '((north living-room))))
  (if (eql loc 'back-stairs) 
      (append '((north library)) '((south downstairs-bedroom))))
  (if (eql loc 'downstairs-bedroom) 
      (append '((north back-stairs)) '((east dining-room))))
  (if (eql loc 'upstairs-bedroom) 
      (append '((south front-stairs)) '((west library))))
  (if (eql loc 'front-stairs) 
      (append '((north upstairs-bedroom)) '((south living-room))))
  (if (eql loc 'living-room) 
      (append '((north front-stairs)) '((east kitchen)) '((south dining-room))))
  (if (eql loc 'kitchen) 
      (append '((west living-room)) '((south pantry))))
  (if (eql loc 'pantry) 
      (append '((north kitchen)) '((east dining-room)))))
Now look at what the code says. It's a DEFUN form whose body is a series of IF expressions. When you call CHOICES, each body expression is evaluated in turn and the result of the last is returned. All other expressions' results are discarded.

What you want to have is a single expression, like this:

Code: Select all

(if (eql loc 'library) 
    (append '((east upstairs-bedroom)) '((south back-stairs)))
    (if (eql loc 'dining-room) 
        (append '((east pantry)) '((west downstairs-bedroom)) '((north living-room)))
        (if (eql loc 'back-stairs) 
            (append '((north library)) '((south downstairs-bedroom)))
            ...))))
Since such a deeply nested expression is unwieldy, have a look at COND, and for this particular problem, CASE.