我一直在教授计算机科学。最常教授的语言是C#,C ++,Java,Python等。我每学期都会添加Perl,Ruby等其他语言的示例,以便学生可以看到跨语言的共性。我一直在尝试使用Common Lisp,并且不得不承认,近40年来我第一次用语言碰壁了。
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
我希望有人能帮助我。
谢谢
Dr t
答案 0 :(得分: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。
祝你好运!答案 1 :(得分:-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.
由于 博士