help for LISP program

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
di40n1
Posts: 2
Joined: Thu Apr 12, 2012 7:13 am

help for LISP program

Post by di40n1 » Thu Apr 12, 2012 7:57 am

So I need help i have to show to my teacher a LISP program for factory for bikes and we have the following information for each type of bike:
number of the model - nm
name of the model - name
amount that is produced of this model - produce
amount that is sold of this product - sell
single prduct price - price
that is for bikes:

Code: Select all

(DEFUN make-bike(nm name1 produce1 sell1 price1)
	(SETF (GET nm 'name) name1)
	(SETF (GET nm 'produce) produce1)
	(SETF (GET nm 'sell) sell1)
	(SETF (GET nm 'price) price1)
	nm
	)
	(SETQ  I ( LIST (make-bike 'n1234' Mountine 11 5 330 )
			(make-bike 'n2345' Sprinter 10 4 200 )
			(make-bike 'n3456' Jumper 5 2 890 )
			(make-bike 'n4567' OldFashion 3 1 1001 )
			(make-bike 'n5678' Kids 20 13 110)
	))
And Now i should make a function which tells us the amount of produced products and the single product price when we are given a number from keyboard

Code: Select all

	(DEFUN ponm (I nom)
		(COND  ((NULL I)(PRINC "No such a Bike"))
			((EQUAL nom(Get (CAR I) 'nm))(PRINT (GET (CAR I) 'produce))
			 (TERPRI)
			 (PRINT (GET (CAR I) 'price)))
			(T (ponm (CDR I) nom))
		))
So my question is: Is this program is corect and which program to use and how to make a program of that and show my teacher the funcion i created works correctly, becouse my teacher didn't teach us how to use a LISP program and test it on PC our works was only on paper PLEASE HELP !!!
Last edited by nuntius on Thu Apr 12, 2012 5:24 pm, edited 1 time in total.
Reason: add code tag

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

Re: help for LISP program

Post by nuntius » Thu Apr 12, 2012 5:44 pm

Your code looks fairly correct. Our CL FAQ has some information on good books and lisp implementations to use.

In the first code block, the biggest problem is your "(setq I ...". You should instead use defvar or defparameter.

Code: Select all

(defparameter *I* (list (make-bike ...)))
(There is a convention where most CL programmers use *earmuffs* to mark global variables.)


Your use of spacing with the single-quote character indicates confusion on how it works.
The quote protects the symbol or expression after it; it is not used in balanced pairs as a string delimiter.
So

Code: Select all

(make-bike 'n2345' Sprinter 10 4 200 )
is normally written as

Code: Select all

(make-bike 'n2345 'Sprinter 10 4 200 )

I also feel the need to note that production CL code uses DEFSTRUCT or DEFCLASS.
It is common for teachers to have CL students build a primitive object system using lists or arrays.
This helps students understand the essence of how any language builds objects, but often leaves the false impression that Lisp somehow lacks this basic feature.

I think there may be some other issues with your code, but don't have more time right now.
When you get a CL implementation installed, the following will help you inspect what is happening.

Code: Select all

(symbol-plist 'n1234)

di40n1
Posts: 2
Joined: Thu Apr 12, 2012 7:13 am

Re: help for LISP program

Post by di40n1 » Fri Apr 13, 2012 4:35 am

Never mind i solved my self. :)

like that:

Code: Select all

(DEFUN make-bike(nm name1 produce1 sell1 price1)
	(SETF (GET nm 'name) name1)
	(SETF (GET nm 'produce) produce1)
	(SETF (GET nm 'sell) sell1)
	(SETF (GET nm 'price) price1)
	nm
	)
	(SETQ  I ( LIST (make-bike 'n1234 '(Mountine) 11 5 330 )
			(make-bike 'n2345 '(Sprinter) 10 4 200 )
			(make-bike 'n3456 '(Jumper) 5 2 890 )
			(make-bike 'n4567 '(OldFashion) 3 1 1001 )
			(make-bike 'n5678 '(Kids) 20 13 110)
	))
	(DEFUN ponm (I nom)
		(COND ((NULL I)(PRINC "No such a bike"))
        ((EQUAL (CAR I) nom)
	 (PRINC "Produced of this type: ")(PRINT (GET(CAR I) 'produce))
	 (TERPRI)
         (PRINC "Price: ")(PRINT (GET(CAR I) 'price))
	 (TERPRI))
			(T (ponm (CDR I) nom))
		))

Post Reply