2017-04-19 47 views
1

我一直在教計算機科學很長一段時間。最常教的語言是C#,C++,Java,Python等。我在每學期添加其他語言的示例,如Perl,Ruby等,以便學生可以看到跨語言的共同點。我一直在Common Lisp上嘗試我的手,不得不承認,近40年來我第一次用語言撞牆。在ideone.com上運行Common Lisp?

Common Lisp讓我難以找到一個簡單的示例程序來編譯和運行。我需要在ideone.com上運行代碼,以便學生可以自行嘗試,並對發生的情況進行更改。我將不勝感激任何幫助,我可以得到這一整個一週的鬥爭是關於我可以採取的一切。

下面是代碼:

(defclass employee() ;;class definition header 
    ((empid :accessor employee-empid;;member variable accessible and preset 
     :initform 230 
     :initarg :empid) 
     (name :accessor employee-name;;member variable accessible and preset 
     :intform 'bill 
     :intarg :name) 
     (pay :accessor employee-pay;;member variable accessible and preset 
     :initform 10 
     :initarg :pay))) 

(defmethod infofun ((p employee));;member method to allow two member vars to be changed 
    (print "The Worker: " : (employee-name p)) 
    (setf (employee-pay p)) 
    (setf (employee-empid p))) 

(setq w1(make-instance 'employee :empid 100 :name 'worker1 :pay 47));;instance of class for w1 
(setq w2(make-instance 'employee :empid 102 :name 'worker1 :pay 57));;instance of class for w2 
(setq w3(make-instance 'employee :empid 103 :name 'worker1 :pay 67));;instance of class for w3 

(describe w1);;get general info from List about the instance w1 
(describe w2) 
(describe w3) 

(infofun w1);;run the member function, change the member vars 
(infofun w2) 
(infofun w3) 

(setf (employee-pay w1) 147);;change a member var by code 


(describe w1);;look at w1 again and note the values 
(infofun w1);;change w1 again 
(describe w1);;look at w1 one more time and check the new values  

我希望有人能幫助我與此有關。

感謝

博士牛逼

+0

您需要檢查INFOFUN:1)什麼是允許的參數打印功能。 b)單個冒號肯定是錯誤的c)如果你調用SETF它期望兩個參數。你只提供一個。 –

+1

另外:如果您發佈問題,您還應該提供錯誤消息。 –

+0

由於運行時發生故障,因此沒有錯誤消息。就像我期望的那樣沒有輸出。我一直堅信,問題是在infofun函數中,但我無法讓它正常工作...... –

回答

1

有錯別字代碼:intarg代替initarg,同樣有intform

此行

(print "The Worker: " : (employee-name p)) 

具有在midle冒號。

一些setf(setf (employee-pay p))需要兩個參數,如後面所做的:(setf (employee-pay p) SOMETHING)

我可以看到,只需通過寫在一個真正的IDE。

我建議你得到Portacle,它是一個可移植的多平臺Common Lisp開發環境,可直接安裝:下載和運行。您將能夠通過錯誤消息來嘗試您的代碼。 Portacle發佈了Emacs25,SBCL,Quicklisp,Slime和Git。

祝你好運!

+0

我已經指出了更正,並做了一些更多的研究,並獲得了原始代碼正常工作。我將獲得Portacle並定期對我的Lisp進行工作。再次感謝。 –

-1

我不想回答我的問題,但這裏去...

我花了幾個小時在這個昨晚的工作,儘管告訴自己不要的。如果你是程序員,那麼你知道那是什麼。最後燈亮了,我意識到我的課程標題有一個「類型」,但沒有解釋器綁定地址的地方。我剛剛添加了一個地方。那麼我就能夠創造實例!我還簡化了類定義,並將所有「有風險」的代碼取出,直到它運行。然後我回到添加行爲回來。

這是我想出了。我正在研究一個更好的版本。

(defclass employee-worker() 
    (person-name empid));;extremely simple common lisp class definition 

(defparameter *worker*(make-instance 'employee-worker));;constructor call for the first instance 
(setf (slot-value *worker* 'person-name) "Joe Brown") 
(setf (slot-value *worker* 'empid) "234") 

(defparameter *worker2*(make-instance 'employee-worker));;constructor call for the second instance 
(setf (slot-value *worker2* 'person-name) "John Brown") 
(setf (slot-value *worker2* 'empid) "235") 

(print "The first worker is: ") 
(print (slot-value *worker* 'person-name)) 
(print "And his employee ID number is: ") 
(print (slot-value *worker* 'empid)) 
(print "") 
(print"The second worker is: ") 
(print (slot-value *worker2* 'person-name)) 
(print "And his employee ID number is: ") 
(print (slot-value *worker2* 'empid)) 

(setf (slot-value *worker* 'person-name) "Joe Green");;change the slot values for the first worker 
(setf (slot-value *worker* 'empid) "432") 

(print "The first worker is: ");;look at the first worker again to see the changes 
(print (slot-value *worker* 'person-name)) 
(print "And his employee ID number is: ") 
(print (slot-value *worker* 'empid)) 

Success time: 0 memory: 38056 signal:0 

"The first worker is: " 
"Joe Brown" 
"And his employee ID number is: " 
"234" 
"" 
"The second worker is: " 
"John Brown" 
"And his employee ID number is: " 
"235" 
"The first worker is: " 
"Joe Green" 
"And his employee ID number is: " 
"432" 

I appreciate everyone helping. 

感謝 博士牛逼

+0

直到你知道CLOS如何工作,才使用'slot-value'。請使用訪問器和initargs。避免部分初始化。這不是一個解決方案。你只是讓代碼變得更糟,直到它「工作」,而不是理解和解決實際問題。 – Svante

+0

@Svante我正在修改我的原始代碼來修復錯誤並取得了進展。感謝您的意見和幫助。 –