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))
>
Where oh where do I go?
Forum rules
Please respect your teacher's guidelines. Homework is a learning tool. If we just post answers, we aren't actually helping. When you post questions, be sure to show what you have tried or what you don't understand.
Please respect your teacher's guidelines. Homework is a learning tool. If we just post answers, we aren't actually helping. When you post questions, be sure to show what you have tried or what you don't understand.
-
- Posts: 94
- Joined: Mon Jul 21, 2008 7:26 am
- Location: München, Germany
- Contact:
Re: Where oh where do I go?
This is your code, correctly formatted:
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:
Since such a deeply nested expression is unwieldy, have a look at COND, and for this particular problem, CASE.
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)))))
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)))
...))))