2017-04-19 49 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我正在修改我的原始代码来修复错误并取得了进展。感谢您的意见和帮助。 –