Page 1 of 1

help for LISP program

Posted: Thu Apr 12, 2012 7:57 am
by di40n1
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 !!!

Re: help for LISP program

Posted: Thu Apr 12, 2012 5:44 pm
by nuntius
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)

Re: help for LISP program

Posted: Fri Apr 13, 2012 4:35 am
by di40n1
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))
		))