Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Match if two values in an unorded list are the same

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

#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"])

The pattern is (list-no-order a a rest ...). But the interpretation of the program fails with:

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

To me it looks an error when transforming the macro. If one changes list-no-order to list the pattern works but of course only if the elements are at the beginning of the list.

Is my pattern wrong, if so how to correct it or is the intended pattern not possible and what is the best way to work around it?

like image 692
Pyfisch Avatar asked Apr 07 '16 20:04

Pyfisch


1 Answers

For now, the best work-around is to use a #:when condition:

#lang racket

(match (list 'foo 'bar 2 #t 42 9 2 'some)
  [(list-no-order a b rest ...)
   #:when (equal? a b)
   "Do some stuff"]
  [_ "Do some other stuff"])
like image 198
Ben Greenman Avatar answered Sep 30 '22 19:09

Ben Greenman