(This is related to the problem for this year’s Advent of Code day 4, 
so...SPOILERS.)
(I did solve both parts of today's problem, so this is more for my own 
edification.)

The problem involves finding lists of numbers, one of the criteria for 
which is that the list has at least two consecutive identical numbers. I 
was able to solve this part of the problem easily with `match`:

    (define (cool? lst)
      (match lst
        [(list _ ... a a _ ...) #t]
        [_ #f]))

    > (cool? '(1 2 3 4 5 6))
    #f
    > (cool? '(1 2 1 4 5 6))
    #f
    > (cool? '(1 1 1 4 5 6))
    #t

The second part (SPOILER) involves finding lists containing **exactly two** 
consecutive identical numbers.
I was trying to find a way to use pattern matching for this problem and 
could not.

As a simple case:
 
    (define (super-cool? lst)
      (match lst
        [(list a b b c) #t]
        [_ #f]))

    > (super-cool? '(1 1 1 4))
    #t                       ; I want it to be #f!

Using `b b` in the middle of the pattern allows me to require that those 
two elements be identical. But there is no requirement that the outside 
elements be *not* identical despite my giving them different ids. I also 
can't do (list (not a) a a (not a)) because, as the docs say[1], "*instances 
of an `id` in different `or` and `not` sub-patterns are independent. The 
binding for `id` is not available in other parts of the same pattern.*"

So it seems easy to match "*at least *N identical elements".
But is there a method for matching "*no more than *N identical elements"?

[1]: https://docs.racket-lang.org/reference/match.html

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/racket-users/5225f7e3-4e96-4366-9978-63375995a5fb%40googlegroups.com.

Reply via email to