English 中文(简体)
载有清单或组合的搜索清单
原标题:Searching lists that contain lists or conses

让我们说,我们就象这样一份清单(其中有许多内容,但方法应当相同):

(define l  ((cons  name  john)
            (cons  sur  asurname)
            (cons  name  george)
            (cons  sur  georgesurname)))

我们能够永远在现有清单中增加更多的内容。 最有效的方式是写写出使用人姓名的职称:

例如:

(define display-surname
  (lamdba (name)
   (...)))

这些案件的一般做法是什么? 谁能举出一个榜样/联系?

最佳回答

代表每个记录<代码>(姓名、名称)的最好想法,然后把数据作为一份表格清单处理:

(define lst  (((name . a) (sur . b)) ((name . c) (sur . d))))

在上述代表处,找到一个名字(即姓)与这个词一样简单(假设记录中的每一领域总是在同一职位上发现):

(define display-surname
  (lambda (lst name)
   (cond ((null? lst) #f)
         ((eq? (cdaar lst) name) (cdadar lst))
         (else (display-surname (cdr lst) name)))))

您可以进一步简化(假设记录中的每一领域总是处于相同地位)的方法,将上述数据表述如下,并相应修改<代码> 显示-surname:

(define lst  ((a . b) (c . d)))

(define display-surname
  (lambda (lst name)
   (cond ((null? lst) #f)
         ((eq? (caar lst) name) (cdar lst))
         (else (display-surname (cdr lst) name)))))

这两种做法都是:

(display-surname lst  a)
>  b

(display-surname lst  x)
> #f
问题回答

暂无回答




相关问题
Lisp code called from Java

Long story: I am doing a project for my functional programing class, and I thought of writing an AI controller in Lisp, for the Mario AI competition. I was looking over frameworks/libraries/ways of ...

Emacs, Zen-Coding mode, and Putty

I use emacs via Putty and since Putty doesn t send certain key combinations to the remote console I generally need to re-bind them to other key combinations. After installing the amazing Zen-Coding ...

In Which Cases Is Better To Use Clojure? [closed]

I develop in Lisp and in Scheme, but I was reading about Clojure and then I want to know, in which cases is better to use it than using Lisp or Scheme? Thanks

lambda-gtk negative pointer

I was trying to write my own put-pixel on (Gdk) pixbuf in Lisp. When I finally realized how I can operate on C pointers in CL, new obstacle came along - (gdk:pixbuf-get-pixels pb) returns me negative ...

Is there a common lisp package naming convention?

I have created some of my own user packages and have run into a name clash. In Java, the naming convention is to use your domain name in the package name: e.g. import com.example.somepackage;. Are ...

SOAP request from within an AutoLISP/AutoCAD macro

We have built a webservice for a client that uses AutoCAD. They have a macro that runs in AutoCAD that builds a SOAP request. But they have not figured out how to actually send() the soap request to ...

热门标签