# Pattern matching k or more items

**URL:** <https://racket.discourse.group/t/pattern-matching-k-or-more-items/3860>\
**Category:** Questions & Answers\
**Created:** [July 19, 2025, 4:37pm UTC](https://racket.discourse.group/t/pattern-matching-k-or-more-items/3860 "2025-07-19T16:37:59Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jeh](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/jeh/32/617_2.png) [@jeh](https://racket.discourse.group/u/jeh)\
**Post date:** [July 19, 2025, 4:37pm UTC](https://racket.discourse.group/t/pattern-matching-k-or-more-items/3860/1 "2025-07-19T16:37:59Z")

</div>

Given a pattern like

> (match '(1 2 3 4)  
> [(list 1 a ..3) a]  
> [\_ 'else])

in the above must the ..k form be a literal non-negative integer? Can k not be an identifier bound to a non-negative identifier?

---

<div class="post-metadata">

**Author:** ![bakgatviooldoos](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/bakgatviooldoos/32/1381_2.png) [@bakgatviooldoos](https://racket.discourse.group/u/bakgatviooldoos)\
**Post date:** [July 19, 2025, 5:44pm UTC](https://racket.discourse.group/t/pattern-matching-k-or-more-items/3860/2 "2025-07-19T17:44:54Z")

</div>

Hi, @jeh.

As far as I know, you can't do what you're asking out of the box, but if you're willing to mess around with some [`match-expander`s](https://docs.racket-lang.org/reference/match.html#%2528form._%2528%2528lib._racket%252Fmatch..rkt%2529._define-match-expander%2529%2529), you could probably mock-up what you're looking for:

```scheme
#lang racket/base

(require
  racket/match
  (for-syntax
   racket/base
   racket/syntax))

(define-for-syntax (make-slice n)
  (datum->syntax #false (string->symbol (format "..~a" (syntax-e n)))))

(define-match-expander slice
  (lambda (stx)
    (syntax-case stx ()
      [(_ id count) #``(,id #,(make-slice #'count))])))

(match '(1 2 3 4)
  [`(,@(slice a 3) ,rest ..1)
   (list a rest)]
  
  [_ #false])

;=> '((1 2 3) (4))

```

For what it's worth, there are a couple of patterns which seem like they should be doable with normal matches, but aren't at present, mostly related to splicing-patterns.

I hope that helps.

---

<div class="post-metadata">

**Author:** ![notjack](https://avatars.discourse-cdn.com/v4/letter/n/e47774/32.png) [@notjack](https://racket.discourse.group/u/notjack)\
**Post date:** [July 19, 2025, 6:52pm UTC](https://racket.discourse.group/t/pattern-matching-k-or-more-items/3860/3 "2025-07-19T18:52:01Z")

</div>

You can also use `#:when`

```scheme
(match (list 1 2 3 4)
  [(list 1 as ...)
   #:when (>= (length as) n)
   as]
  [_ 'else])

```

---

<div class="post-metadata">

**Author:** ![soegaard](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/soegaard/32/19_2.png) [@soegaard](https://racket.discourse.group/u/soegaard)\
**Post date:** [July 19, 2025, 7:58pm UTC](https://racket.discourse.group/t/pattern-matching-k-or-more-items/3860/4 "2025-07-19T19:58:03Z")

</div>

> [@jeh](#):
>
> must the ..k form be a literal non-negative integer?

> ... must the ..k form be a literal non-negative integer?  
> Yes.
