Match if two values ​​in an unordered list are the same

I have a Racket list with some values (list 'foo 'bar 2 #t 42 9 2 'some) . In fact, these values ​​follow some more specific patterns, but for the question it does not matter. I want to test a test if there are two identical values ​​in the list, in this case the number 2 and get the element and other elements. This is my attempt:

 #lang racket (match (list 'foo 'bar 2 #t 42 9 2 'some) [(list-no-order aa rest ...) "Do some stuff"] [_ "Do some other stuff"]) 

Sample (list-no-order aa rest ...) . But the interpretation of the program is not performed:

 a11: unbound identifier; also, no #%top syntax transformer is bound in: a11 

For me, this looks like an error when converting a macro. If you change list-no-order to list , then the template works, but, of course, only if the items are at the top of the list.

Is my template wrong, if so, how can I fix it, or is it impossible, and how can I get around it in the best way?

+6
source share
1 answer

I wonder why you are trying to match something. This is not clear to me regarding your question and code. I would approach your problem through pure list processing (at least as far as I understand)

 (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)))) 
0
source

All Articles