【问题标题】:LISP; Symbols And Globals口语;符号和全局变量
【发布时间】:2017-09-28 02:34:39
【问题描述】:

我的作业有些问题。我的目标是创建代表学生姓名、姓名以及他们的入学编号(属性列表)的符号。我还为我创建的所有学生使用了一个全局变量。

我的代码如下所示:

(defun student-create (firstName lastName matr)
(setq newStudent (gensym "studentNr."))
(setf (get 'newStudent 'firstName ) firstName )
(setf (get 'newStudent 'lastName ) lastName)
(setf (get 'newStudent 'matr ) matr)
;(append  (list newStudent)*stu-liste*)  ;no global list?
)

(student-create 'asdf 'dfgh '132654)
(student-create 'a 'b '123)
(student-create 'c 'd '234)
(student-create 'e 'f '345)

这就是我想要创建学生的功能。我输入名字和姓氏以及入学编号。

它适用于符号 newStudent,但正如您所见,该函数不是那么动态的。每次我使用该函数时,newStudent 都会被覆盖。 另外,我在将所有符号保存在全局变量中时遇到问题。

谁能给我一个提示?

【问题讨论】:

    标签: variables lisp global symbols


    【解决方案1】:

    newStudent 在您的代码中被视为全局变量,这就是为什么每次调用student-create 都会覆盖它。您应该利用这些知识重新考虑如何将学生列表(或任何数据结构)存储到全局变量中。

    student-create 中,您想要的是每次调用函数时生成的符号的新本地绑定。为此,您可以使用let

    其实这个作业看起来很像the example for get in the CLHS...

    【讨论】:

    • 感谢您的帮助!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-08
    • 1970-01-01
    • 1970-01-01
    • 2023-03-14
    • 1970-01-01
    相关资源
    最近更新 更多