匹配如果无序列表中的两个值相同

Pyf*_*sch 6 equality pattern-matching racket

我有一个带有一些值的球拍列表(list 'foo 'bar 2 #t 42 9 2 'some).实际上,这些值遵循一些更具体的模式,但对于这个问题,这是无关紧要的.我想测试测试列表中是否有两个相同的值,在这种情况下是数字2,并获取元素和其他元素.这是我的尝试:

#lang racket

(match (list 'foo 'bar 2 #t 42 9 2 'some)
  [(list-no-order a a rest ...)
     "Do some stuff"]
  [_ "Do some other stuff"])
Run Code Online (Sandbox Code Playgroud)

模式是(list-no-order a a rest ...).但该计划的解释失败了:

a11: unbound identifier;
 also, no #%top syntax transformer is bound in: a11
Run Code Online (Sandbox Code Playgroud)

对我来说,转换宏时看起来是错误的.如果一个人改变list-no-orderlist模式工作,但当然只是如果这些元素在列表的开头.

我的模式是错误的,如果是这样,如何纠正它或者是不可能的预期模式,解决它的最佳方法是什么?

rob*_*kuz 0

我想知道你为什么要尝试模式匹配某些东西。通过你的问题和代码我不清楚。我会通过纯列表处理来解决你的问题(至少据我所知)

(filter
    (lambda (x)
        ;;filter for the first element of the prev created tuple and 
        ;;check if its larger than 1
        (> (first x) 1))
    (map
        (lambda (x)
            ;;tuple of the length of the prevously created group and the group itself
            (cons (length x) x))
        (group-by
            ;;just the element it seld
            (lambda (x)
                x)
            (list 'foo 'bar 2 #t 42 9 2 'some))))
Run Code Online (Sandbox Code Playgroud)