rooms of a house

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.
Post Reply
g0blu324
Posts: 6
Joined: Sat May 12, 2012 2:16 pm

rooms of a house

Post by g0blu324 » Sat May 12, 2012 2:22 pm

I'm working on my first ever assignment dealing with Lisp. My function deals with 9 rooms of a house. 1.Library 2. Upstairs-bedroom 3. back-stairs 4. front-stairs 5. downstairs-bedroom 6. living-room 7.dining-room 8. kitchen 9.pantry
-The input is a room and the output is the number of direct rooms you can go from that room. The living room and Dining room's output is 3, the rest is 2.
-I tried 2 approaches to this, but not very successful. Could someone tell me what I'm doing wrong or the more efficient way of doing this?

Approach #1:
> (defun how-many-choices (loc)
(setq library 2)
(setq back-stairs 2)
(setq downstairs-bedroom 2)
(setq upstairs-bedroom 2)
(setq front-stairs 2)
(setq living-room 3)
(setq dining-room 3)
(setq kitchen 2)
(setq pantry 2))
HOW-MANY-CHOICES
> (how-many-choices 'back-stairs)
2
> (how-many-choices 'living-room)
2
>
--The living-room's output should be 3, not 2

Approach #2:
(defun how-many-choices (loc)
(if(eql loc 'living-room 'dining-room)
3
(= loc 2)))
HOW-MANY-CHOICES
> (how-many-choices 'library)
error: too many arguments
>

nuntius
Posts: 538
Joined: Sat Aug 09, 2008 10:44 am
Location: Newton, MA

Re: rooms of a house

Post by nuntius » Sat May 12, 2012 7:30 pm

So in your first approach, the parameter loc isn't being used. Every time you call the function, it sets a number of (undeclared, assumed to be global) variables to integer values. The function always returns the value 2 because that is the return value of the expression (setq pantry 2) which is the last expression in the function.

Your second approach has a much better form, but eql is only specified for two parameters, and you are passing it three.

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

Re: rooms of a house

Post by g0blu324 » Sun May 13, 2012 8:03 am

Ok thanks. Other than that function, I figured out 2 more functions from that out of 8 total. So now I have another question.

This time, I need to return either 2 or 3 rooms based on directional from each room, not just 1 number or string.
Example:
Input: Dining-Room
Output: ((east downstairs-bedroom) (west pantry) (north living-room)
Input: Library
Output: ((east upstairs-bedroom)(south back-stairs))

Should I be using append? condition statement (cond ((eql..... )) ? how many parameters should I send?

So here's what I've been trying:

#1 > (defun choices (loc)
(cond
(((eq loc 'library)'east 'upstairs-bedroom)'south 'back-stairs)
((((eq loc 'dining room) 'west 'downstairs-bedroom) 'east 'pantry) 'north
'living-room)))
CHOICES
> (choices 'library)
error: bad function - (EQ LOC (QUOTE LIBRARY))
> (choices 'dining-room)
error: bad function - (EQ LOC (QUOTE LIBRARY))

#2
> (defun choices (loc)
(if(eql loc 'library)
(append '(east upstairs-bedroom) '(back-stairs))
if(eql loc 'dining-room)
(append '(west downstairs-bedroom) '(east pantry) '(north living-room))))
CHOICES
> (choices 'library)
error: too many arguments
>

How is it too many arguements when I only took account 2 out of 9 rooms sofar

#3. I've tried doing a bunch of setq's and if statements but that's too many arguements.

Once I figure out how to do it for either 2 or 3 outputs, I can figure out the rest since there's always either 2 or 3 outputs.

Post Reply