Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cocoa/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Racket 球拍-查找2D列表中具有相同元素的列表_Racket - Fatal编程技术网

Racket 球拍-查找2D列表中具有相同元素的列表

Racket 球拍-查找2D列表中具有相同元素的列表,racket,Racket,有人能告诉我如何解决这个问题/给我一个模板吗?我不允许使用map、lambda或任何其他高级内置函数,仅列出与输入字符串关联的国家: (define (get-country l s) (cond [(empty? (rest l)) (second (second (first l)))] [else (if (equal? s (first (first l))) (second (second (first l)))

有人能告诉我如何解决这个问题/给我一个模板吗?我不允许使用map、lambda或任何其他高级内置函数,仅列出与输入字符串关联的国家:

(define (get-country l s)
  (cond [(empty? (rest l)) (second (second (first l)))]
        [else (if (equal? s (first (first l)))
                  (second (second (first l)))
                  (get-country (rest l) s))]))
然后提取与该国家/地区关联的所有字符串:

(define (get-countries l s)
  (cond [(empty? l) '()]
        [else (if (equal? s (second (second (first l))))
                  (cons (first (first l)) (get-countries (rest l) s))
                  (get-countries (rest l) s))]))
然后把它们放在一起:

(define (same-country l s)
  (get-countries l (get-country l s)))
当我们进行评估时,我们得到的结果不同于
(列出“YUL”“YVR”“YWG”“YYZ”)

因此,我们检查结果是否是所需列表的排列。首先我们要做的是排列:

(define (is-permutation l1 l2)
  (and (not (and (cons? l1) (empty? l2)))
       (not (and (empty? l1) (cons? l2)))
       (or (and (empty? l1) (empty? l2))
           (and (is-member (first l1) l2)
                (is-permutation (rest l1)
                                (remove-one (first l1) l2))))))

(define (is-member e l)
  (and (not (empty? l))
       (or (equal? (first l) e)
           (is-member e (rest l)))))

(define (remove-one e nel)
  (cond [(empty? (rest nel)) '()]
        [else (if (equal? (first nel) e)
                  (rest nel)
                  (cons (first nel) (remove-one e (rest nel))))]))
然后我们可以测试:

> (is-permutation (same-country alist "YUL")
                  (list "YUL" "YVR" "YWG" "YYZ"))
#true

首先提取与输入字符串关联的国家:

(define (get-country l s)
  (cond [(empty? (rest l)) (second (second (first l)))]
        [else (if (equal? s (first (first l)))
                  (second (second (first l)))
                  (get-country (rest l) s))]))
然后提取与该国家/地区关联的所有字符串:

(define (get-countries l s)
  (cond [(empty? l) '()]
        [else (if (equal? s (second (second (first l))))
                  (cons (first (first l)) (get-countries (rest l) s))
                  (get-countries (rest l) s))]))
然后把它们放在一起:

(define (same-country l s)
  (get-countries l (get-country l s)))
当我们进行评估时,我们得到的结果不同于
(列出“YUL”“YVR”“YWG”“YYZ”)

因此,我们检查结果是否是所需列表的排列。首先我们要做的是排列:

(define (is-permutation l1 l2)
  (and (not (and (cons? l1) (empty? l2)))
       (not (and (empty? l1) (cons? l2)))
       (or (and (empty? l1) (empty? l2))
           (and (is-member (first l1) l2)
                (is-permutation (rest l1)
                                (remove-one (first l1) l2))))))

(define (is-member e l)
  (and (not (empty? l))
       (or (equal? (first l) e)
           (is-member e (rest l)))))

(define (remove-one e nel)
  (cond [(empty? (rest nel)) '()]
        [else (if (equal? (first nel) e)
                  (rest nel)
                  (cons (first nel) (remove-one e (rest nel))))]))
然后我们可以测试:

> (is-permutation (same-country alist "YUL")
                  (list "YUL" "YVR" "YWG" "YYZ"))
#true

谢谢你的回答!尽管我想问如何构建我自己的排序函数来生成一个新列表,其中所有字符串都按升序排列?与顶层问题无关,但您要查找的内容与类似,只是在
insert
中不使用
=
按降序排列数字,而是使用
string谢谢您的回答!尽管我想问如何构建我自己的排序函数来生成一个新列表,其中所有字符串都按升序排列?与顶层问题无关,但您要查找的内容与类似,除了在
insert
中使用
=
以降序排列数字之外,您使用的是
string