Match patterns with Kleene star by structure type in Racket

Recently, I started playing with the Racket pattern matching system and got into a problem that I cannot understand.

If I do:

(match (list 1 2 3 4 5 6 7 8 9 10 11 12)
    [(list _ x y z ...) (list y ': x)]) 

in repl i get

'(3 : 2)

as my desired result.

If I do:

(match (current-date)
    [(date* _ x y z ...) (list y ': x)])

or

(match (date* 5 18 13 18 11 2011 5 321 #f 3600 0 "W. Europe Standard Time")
    [(date* _ x y z ...) (list y ': x)])

I get this error:

match: wrong number for fields for structure date*: expected 12 but got 5 in: (_ x y z ...)

I suspect that the Klein star ...for some reason does not work with the structural type. Why is this so?

+5
source share
2 answers

It seems that what you really want to do is map a subset of the fields in the structure, and not bind the remaining fields in the structure to (z ...). In this case, you can try using the struct * matching pattern .

Here is an example:

(match (date* 5 18 13 18 11 2011 5 321 #f 3600 0 "W. Europe Standard Time")
   [(struct* date ([minute x] [hour y])) (list y ': x)])
+10

, Kleene ... - .

. ... ..k ___ __k , list -matching hash-table -matching . , , lvp ( pat, pat ... ) , pat.

?

, ... - - , . , , ... - β€” , - β€” , , . , .

+8