简体   繁体   English

在Scheme列表中查找元素

[英]Finding element in list in Scheme

i'm very beginner of Scheme and try to figure out how it is functioning; 我是Scheme的初学者,请尝试弄清楚它的功能; so i want to write a basic code: first of all i have a definition set: zipcode (ZIPCODE CITY STATE) 所以我想写一个基本代码:首先,我有一个定义集: 邮政编码(邮政编码城市名)

(define zipcodes '(
 (96774 ookala hawaii)
 (90001 losangeles california)
 (90263 malibu california)
 (10044 newyork newyork)
 ))

i try to write a function that input is zipcode and return city and state name for example: 我尝试编写一个输入为邮政编码并返回城市和州名称的函数,例如:

 >(find '10044)
 (list 'newyork 'newyork)

 >(find '99999)
 empty because there is not zipcode like that.

Thanks a lot... 非常感谢...

I'm not allowed to us LET function 我不允许我们使用LET功能

Use assoc 使用assoc

> (assoc 90001 zipcodes)
(90001 losangeles california)
> (cdr (assoc 90001 zipcodes))
(losangeles california)
(define filter
   (lambda (proc lst)
       (cond ((null? lst) '())
             ((proc (car lst)) (cons (car lst) (filter proc (cdr lst))))
             (else
                (filter proc (cdr lst))))))

(define find-zip
   (lambda (zip lst)
      (define match (filter (lambda (item) (= zip (car item))) lst))
      (if (null? match)
          '()
          (cdar match))))

(define (find zip)
    (find-zip zip zipcode))

I think that will work for you. 我认为这对您有用。 Filter will apply its first argument (a procedure) to each item in its second argument, which needs to be a list. 过滤器会将其第一个参数(一个过程)应用于其第二个参数中的每个项目,后者需要是一个列表。 The first argument needs to return a boolean value for each item it's passed. 第一个参数需要为传递的每个项目返回一个布尔值。 Filter will then return a list with all of the items that returned true when the first argument was applied. 然后,过滤器将返回一个列表,其中包含应用第一个参数时返回true的所有项目。 Otherwise, it returns an empty list. 否则,它将返回一个空列表。

In this case, each item in the list you're passing is itself a list of 3 items, so it compares the first item in that list with the zip code you're looking for. 在这种情况下,您要传递的列表中的每个项目本身就是3个项目的列表,因此它将该列表中的第一个项目与所需的邮政编码进行比较。 If it's a match, it returns true. 如果匹配,则返回true。 So if the zip code is in the list, it will return the three item sub-list. 因此,如果邮政编码在列表中,它将返回三项子列表。 Then we check to see if we got an empty list, if so, then we return an empty list. 然后,我们检查是否有一个空列表,如果有,则返回一个空列表。 Otherwise, we take the cdr of the car to get your desired city and state. 否则,我们将使用汽车的CDR来获取您想要的城市和州。

Well so you can basically just filter the list for the zip code, I have sketched some code below (I'd write it differently, except I don't know what you have available outside of what is defined in RnRS). 好吧,所以您基本上可以只过滤邮政编码列表,我在下面草绘了一些代码(我写的方式有所不同,除了我不知道RnRS中定义的功能之外,还有其他功能)。

(define find-zip
  (lambda (zip codelist)
   (if (empty? codelist) empty
    (if (= zip (car (car codelist)) (list (cadr (car codelist)) (caddr (car codelist)))
        (find-zip zip (cdr codelist))))))

It would probably be better should you use a let, and I think most implementations have a filter function that let you do this better. 如果使用let可能会更好,而且我认为大多数实现都具有过滤器功能,可以让您更好地做到这一点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM