Lisp 查看的Scheme函数是端点匹配

Lisp 查看的Scheme函数是端点匹配,lisp,scheme,Lisp,Scheme,因此,这里有两个我想使用的已定义列表: (DEFINE list0 (LIST 'j 'k 'l 'm 'n 'o 'j) ) (DEFINE list1 (LIST 'a 'b 'c 'd 'e 'f 'g) ) (DEFINE list2 (LIST 's 't 'u 'v 'w 'x 'y 'z) ) (DEFINE list3 (LIST 'j 'k 'l 'm 'l 'k 'j) ) (DEFINE list4 (LIST 'n 'o 'p 'q 'q 'p 'o 'n) ) (DEF

因此,这里有两个我想使用的已定义列表:

(DEFINE list0 (LIST 'j 'k 'l 'm 'n 'o 'j) )
(DEFINE list1 (LIST 'a 'b 'c 'd 'e 'f 'g) )
(DEFINE list2 (LIST 's 't 'u 'v 'w 'x 'y 'z) )
(DEFINE list3 (LIST 'j 'k 'l 'm 'l 'k 'j) )
(DEFINE list4 (LIST 'n 'o 'p 'q 'q 'p 'o 'n) )
(DEFINE list5 '( (a b) c (d e d) c (a b) ) )
(DEFINE list6 '( (h i) (j k) l (m n) ) )
(DEFINE list7 (f (a b) c (d e d) (b a) f) )
我想做的是为“endsmatch”函数创建一个递归函数,该函数的作用如下:

尾端匹配:
(endsmatch 1st)
如果列表中的第一个元素与列表中的最后一个元素相同,则应返回
#t
,并返回
#f
否则。就是

(尾端匹配(s t u v w x y z))
将/应返回:
#f

将/应返回:
#t

(endsmatch'())
(endsmatch'(a))
应返回
#t

此外,该函数还可以读取复杂列表,例如:
(endsmatch'((a b)c(d e d)c(a b)))
然后返回:
#t
以及:

是否都应返回
#f


由于我是scheme的新手,因此可能会对该函数进行编码,并且会看到它的样子,提前谢谢您。

试试这个,它很简单:

(define (endsmatch lst)
  (if (null? lst)
      #t
      (equal? (first lst) (last lst))))
如果您的方案解释器不包括过程
first
last
,则它们的实现非常简单:

(define (first lst)
  (car lst))

(define (last lst)
  (cond ((null? lst) #f)
        ((null? (cdr lst)) (car lst))
        (else (last (cdr lst)))))

我提出了这个解决方案,但在您描述的最后两个测试中失败了:

(define (endsmatch lst)
  (let loop ((lst lst) (first '()) (last '()))
    (cond
      ((null? lst)         (eq? first last))
      ((pair? (car lst))   (loop (car lst) first last)
                           (loop (cdr lst) first last))
      ((null? first)       (loop (cdr lst) (car lst) (car lst)))
      (else                (loop (cdr lst) first (car lst))))))


; racket test code
(require rackunit)
(check-eq? (endsmatch '(s t u v w x y z)) #f)
(check-eq? (endsmatch (list 'j 'k 'l 'm 'n 'o 'j)) #t)
(check-eq? (endsmatch '()) #t)
(check-eq? (endsmatch '(a)) #t)
(check-eq? (endsmatch '((a b) c (d e d) c (b a))) #t)
; these fail
(check-eq? (endsmatch '((a b) c (d e d) c (b a))) #f)
(check-eq? (endsmatch '((y z) y)) #f)
事实上,你两者都说

"(endsmatch '((a b) c (d e d) c (b a)) ) which would then return: #t"


这是自相矛盾的。

谢谢,我要试试这个。空的不起作用list@finnw你说得对。我更新了我的答案,谢谢。如果将
lst
替换为
lis
instead@user1786512不管你用什么名字,只要它不是一个已经在使用的名字。例如,将其命名为
列表
,这是一个坏主意,因为这是一个内置过程。很抱歉,我无意中创建了一个拼写错误,如果您想重新检查它并了解我的实际意思,我会更正它。
(define (endsmatch lst)
  (let loop ((lst lst) (first '()) (last '()))
    (cond
      ((null? lst)         (eq? first last))
      ((pair? (car lst))   (loop (car lst) first last)
                           (loop (cdr lst) first last))
      ((null? first)       (loop (cdr lst) (car lst) (car lst)))
      (else                (loop (cdr lst) first (car lst))))))


; racket test code
(require rackunit)
(check-eq? (endsmatch '(s t u v w x y z)) #f)
(check-eq? (endsmatch (list 'j 'k 'l 'm 'n 'o 'j)) #t)
(check-eq? (endsmatch '()) #t)
(check-eq? (endsmatch '(a)) #t)
(check-eq? (endsmatch '((a b) c (d e d) c (b a))) #t)
; these fail
(check-eq? (endsmatch '((a b) c (d e d) c (b a))) #f)
(check-eq? (endsmatch '((y z) y)) #f)
"(endsmatch '((a b) c (d e d) c (b a)) ) which would then return: #t"
"(endsmatch '((a b) c (d e d) c (b a)) ) should return #f"