Reputation:
if I have something like this
(define s (hi,there))
then how can I write in match
like
(match s [(,h , ,t)] ...)
But it is not working, because match
needs the ,
so how can I do this?
Upvotes: 6
Views: 1880
Reputation: 6755
I think you might be confused about where you need commas. In Racket, you do not use commas to separate elements in a list. Instead, you just use whitespace. Tell me if this is wrong, but what I imagine is that you are trying to match an expression like (define s '(hi there))
. To do that, you would use
(match s
[`(,h ,t) ...])
Then, in the area where the elipses is, the variable h
has the value 'hi
, and the variable t
has the value 'there
Upvotes: 1
Reputation: 53665
Use a backslash if you want to use comma as a symbol inside of a quoted section:
> (define s '(hi \, there))
> (match s [(list h c t) (symbol->string c)])
","
And use '|,|
for the standalone comma symbol.
> (match s [(list h '|,| t) (list h t)])
'(hi there)
In either case, you really should use whitespace to separate things, and use lists.
(define s (hi,there))
is not valid Racket.
Upvotes: 2
Reputation: 31147
First note that the comma ,
is a special reader abbreviation.
The (hi,there)
is a read as (hi (unquote there))
. This is
difficult to spot - since the default printer prints lists
whose first element is an unquote
in a 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 the pattern you need is '(list h (list 'unquote t))'.
> (define s '(hi,there))
> (match s [(list h (list 'unquote t)) (list h t)])
(list 'hi 'there)
Upvotes: 7