LISP-通过其参数搜索特定功能的程序 [英] LISP - Program to search a specific function through its parameters
问题描述
对于一个课程项目,我必须用Lisp编写程序.
For a course project I got to write a program in lisp.
程序应包含最重要的lisp函数,其输入和输出参数以及可能的可选参数.
The program should contain the most important lisp functions, their input and output parameters and maybe optional parameters.
例如:函数-首先,输入-列表,输出-对象(列表的第一个成员).
For example: function - first, input - list, output - object (first member of list).
程序应以两种不同的方式工作:
The program should work in 2 different ways:
-
您为程序指定一个函数名称,它应该返回函数参数.
You give the program the name of a function and it should return the function parameters.
您输入函数参数,如果存在带有这些参数的函数,则应返回该函数的名称.
You enter function parameters and if a function with these parameters exists, it should return the name of the function.
我的问题:
-
在Lisp中处理这样的任务的正确方法是什么?我认为也许一棵树将是处理它的一种方式? (用所有函数和参数制作一棵树,然后编写一个处理它的程序).
What would be the right way to approach a task like this in lisp? I think maybe a tree would be a way to handle it? (make a tree with all functions and parameters and then write a program which handles it).
有没有比这更好的主意了?或从哪里/如何开始的一些建议?或包含任何信息的教程?
Does anyone have a better idea than that to approach this task? Or some suggestions where / how to start? Or Tutorials containing any info?
此刻,我有点不知道如何开始.您能提供的任何帮助将不胜感激.
At the moment I'm a little lost how to start. Any help you can give would be highly appreciated.
英语不是我的母语,所以我希望一切都可以理解.
English isn't my first language, so I hope everything is understandable.
问候.
推荐答案
从表面上看,任务似乎是在内存中构造一个简单的符号数据库,可通过两种方式进行搜索.数据库中的条目应理解为功能. 输出参数"可能被理解为一个或多个返回值.这些东西在ANSI Lisp中没有命名.对该任务的一种有用解释是,无论如何都要给返回值提供符号标签.此外,我们也许可以将类型符号用于返回值和参数.因此,例如, cons 函数的数据库条目可能类似于:
At face value, the task seems to be the construction of a simple symbolic database in memory, which is searchable in two ways. Entries in the database are understood to be functions. The "output parameters" can probably be understood as one or more return values. These things are not named in ANSI Lisp. A useful interpretation of the task is to give return values symbolic labels anyway. Moreover, we can perhaps use type symbols for the return values as well as parameters. So for instance, a database entry for the cons function might look like:
(cons (t t) cons) ;; function named cons takes two objects, returns a cons
类型t
是ANSI Lisp中所有类型的超类型;意思是任何值".
The type t
is the supertype of all types in ANSI Lisp; it means "any value".
可以将此类记录的列表放入某些全局变量中.然后,我们编写一个可能名为get-params-by-name
的函数,如下所示:
A list of such records can be put into some global variable. Then we write a function that is perhaps named get-params-by-name
such that:
(get-params-by-name 'cons) -> (t t)
和另一个:get-names-by-params
:
(get-names-by-params '(t t)) -> (cons)
此函数以列表的形式返回所有匹配的函数.多个功能可以具有此签名.
This function returns all the matching functions, as a list. More than one function could have this signature.
然后,诀窍是找到可选参数和rest参数的良好表示形式.该语言可能使用的是同一符号:
The trick is then finding a good representation of optional and rest parameters. It could just be the same notation that the language uses:
(list (&rest t) list) ;; list takes rest arguments of any type, returns list
由于我们只对精确匹配感兴趣,因此我们不必实际解析&rest
表示法.当用户按参数查询时,使用相同的语法,他们的查询对象实际上是(&rest t)
.
Since we are only interested in exact matches, we don't have to actually parse the &rest
notation. When the user queries by parameter, their query object will be literally (&rest t)
, in that same syntax.
equal
函数可用于判断两个符号列表是否相同:
The equal
function can be used to tell whether two lists of symbols are identical:
(equal '(&rest t) '(&rest t)) -> t
(equal '(t t) '(t t)) -> nil
因此练习并不困难:只需通过列表进行映射,寻找匹配项即可.
So the exercise is not difficult: just mapping through lists, looking for matches.
(defun get-name-by-params (database params)
(let ((matching-entries (remove-if-not (lambda (entry)
(equal (second entry) params))
database)))
(mapcar #'first matching-entries))) ;; just the names, please
此处,该函数将数据库列表作为参数,而不是引用全局变量.我们将其集成到其中的整个程序可以提供其他接口,但这是我们的低级查找功能.
Here, the function takes the database list as a parameter, instead of referring to a global variable. The overall program into which we integrate this can provide alternative interfaces, but this is our low-level lookup function.
测试:
[1]> (get-name-by-params '((cons (t t) cons) (list (&rest t) list)) '(integer string))
NIL
[3]> (get-name-by-params '((cons (t t) cons) (list (&rest t) list)) '(t t))
(CONS)
[4]> (get-name-by-params '((cons (t t) cons) (list (&rest t) list)) '(&rest t))
(LIST)
在作业要交之前,我会从讲师那里弄清楚这是否是对模糊要求的正确解释.
I'd get clarification from the instructor whether this is the right interpretation of the vague requirements, before the assignment is due.
这篇关于LISP-通过其参数搜索特定功能的程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!