How coincidence in a match at Racket?

if I have something like this (define s (hi,there)) then how can I write in a match like (match s [(,h , ,t)] ...) But it does not work, because match needs in , so how can I do this?

+6
source share
3 answers

First of all, note that the comma is a special abbreviation for the reader. (hi,there) reads as (hi (unquote there)) . This is difficult to determine - since the list of printers is printed by default, the first element of which is unquote special way.

 Welcome to DrRacket, version 5.3.0.14--2012-07-24(f8f24ff2/d) [3m]. Language: racket. > (list 'hi (list 'unquote 'there)) '(hi ,there) 

Therefore, you need a template (list h (list "unquote t") ".

 > (define s '(hi,there)) > (match s [(list h (list 'unquote t)) (list ht)]) (list 'hi 'there) 
+7
source

Use a backslash if you want to use a comma as a character inside the quoted section:

 > (define s '(hi \, there)) > (match s [(list hct) (symbol->string c)]) "," 

And use '|,| for a single comma character.

 > (match s [(list h '|,| t) (list ht)]) '(hi there) 

In any case, you really need to use spaces to separate things and use lists.

(define s (hi,there)) invalid. Racket.

+2
source

I think you may be confused about where you need commas. In Racket, you do not use commas to separate items in a list. Instead, you just use spaces. Tell me if this is wrong, but I think you are trying to match an expression like (define s '(hi there)) . For this you should use

 (match s [`(,h ,t) ...]) 

Then in the area where the elipses are located, the variable h has the value 'hi , and the variable t has the value 'there

+1
source

Source: https://habr.com/ru/post/921446/


All Articles