CLISP:检查列表中两个元素是否依次排列 [英] CLISP : Check if two elements are in order one after another in a list

查看:50
本文介绍了CLISP:检查列表中两个元素是否依次排列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个清单

 L=(1 j 3 k 4 h 5 n 6 w)

我需要做一个功能验证,将验证第一个原子是否在第二个原子之前. 我想验证一下:

I need to do a function Verify that will verify if the first atom is before the 2nd. I want to verify this:

> Verify(3 k)

结果应返回

> T

//因为原子'3'在原子'k'之前

// because atom '3' is before atom 'k'

在这种情况下:

>Verify(h 4)

结果应返回

> NIL

//因为原子'h'在原子'4'之后

// because atom 'h' is after atom '4'

我必须检查每个元素的位置并比较位置

I have to check position of each element and compare positions

推荐答案

您在使用Lisp的哪种方言?以下是有关如何得出解决方案的一些提示,请填写空白:

What dialect of Lisp are you using? Here are some pointers on how to derive a solution, fill-in the blanks:

(define (verify lst a b)
        ; what happens if there's only one element left in the list?
  (cond ((null? (cdr lst)) <???>)
        ; how do we check if the current element is equal to the `a` parameter
        ; and the next element is equal to the `b` parameter?
        (<???> T)
        ; how do we continue traversing the rest of the list?
        (else (verify <???> a b))))

;;; tests

(define lst '(1 j 3 k 4 h 5 n 6 w))

(verify lst 3 'k)
> T
(verify lst 'h '4)
> F

这篇关于CLISP:检查列表中两个元素是否依次排列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆