## Where oh where do I go?

You have problems, and we're glad to hear them. Explain the problem, what you have tried, and where you got stuck.
Feel free to share a little info on yourself and the course.
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.

### Where oh where do I go?

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))
>
g0blu324

Posts: 6
Joined: Sat May 12, 2012 2:16 pm

### Re: Where oh where do I go?

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.

Kompottkin

Posts: 90
Joined: Mon Jul 21, 2008 7:26 am
Location: München, Germany

Return to Homework

### Who is online

Users browsing this forum: No registered users and 1 guest