Scheme 模式匹配——同时匹配 cons 对组件和整个 cons 对

Scheme pattern matching - Match cons pair components and the whole cons pair at the same time

如何在使用 match-lambda 的方案中对一个缺点对组件(carcdr)和整个缺点对进行模式匹配?我正在寻找一种简洁而优雅的方法来提取缺点对组件并同时引用整个缺点对。

这个问题可以拆分成两个问题

  1. cons 对组件(carcdr)如何在方案中进行模式匹配?
  2. 如何使用 match-lambda 在单个引用中捕获具有匹配组件的整个模式匹配表达式?我知道以上可以在 lambda.
  3. 中使用 match 来完成

我试过下面的方法,但没有成功

(match-lambda
  [(car-component . cdr-component) ... use car-component and cdr-component])

理想情况下,我正在寻找类似

的东西
(match-lambda
  [((car-component . cdr-component) as whole-pair) ... use car-component, cdr-component, and whole pair])

如果匹配器完全可以匹配 cons 对,那么您需要一个 and 模式:

(match-lambda
  [(and whole
        (cons a b))
   ;; whole, a, b bound here
   ...]
  ...)

将使用 Racket 的 match-lambda

看起来不同的实现有不同的语法来匹配 conses 而不是正确的列表,我不清楚,例如,Guile's 可以,但它们似乎都有 and, 意思是 'all the patterns must match, binding suitable identifiers'.

(我个人认为不让你匹配 conses 的实现有点没用,所以我希望他们都这样做。但我不是 Scheme 人,真的。)