Pattern matching is a very readable alternative to manual destructuring in let clauses. I was wondering whether it's possible to use it with streams, like we can with lists.
As an illustration, consider a simple add-between implementation to produce a sequence with an element inserted between each of the elements of the original sequence.
(define (add-between lst sep)
(match lst
['() '()]
[(cons v vs)
(cons v
(cons sep
(add-between vs sep)))]))
Usage:
(add-between (list 'a 'b 'c) 'and) ; => '(a and b and c and)
What if we want to do the same with arbitrary sequences, like streams?
(define (add-between seq sep)
(match seq
['() '()]
[(cons v vs)
(stream-cons v
(stream-cons sep
(add-between vs sep)))]))
This produces an error:
(add-between (stream 'a 'b 'c) 'and)
; match: no matching clause for #<stream>
; stdin:1:1
; Context:
; /Applications/Racket v7.5/collects/racket/match/runtime.rkt:24:0 match:error
; /Applications/Racket v7.5/collects/racket/repl.rkt:11:26
It would be ideal if we could pattern match on a generic sequence type, since that would encapsulate lists as well as streams.