Trung Bún
Trung Bún

Reputation: 1147

Do pattern matching in scheme

Does anyone have an idea how to match the pattern (entity ?e0 . ?attrs) with (entity e0 (prov:type "Revision") ??? The number 0 in ?e0 can be any number 1, 2, 3 .....

The result after matching is (?e0 . e0) (?attrs (prov:type "Revision"))

I have tried this:

  (define clause-match
  (lambda (statement1 statement2)
    (match statement2 [(list 'entity ?(car (cdr statement1)) (cons (car (cdr statement1)) (car (cdr statement2))))] [list _ 'no])))

But I didn't succeed .....

Error: match: syntax error in pattern (car (cdr statement1))

Please show me where I'm wrong and how to fix it!! Regular expression make me really confused ...

Upvotes: 1

Views: 342

Answers (2)

uselpa
uselpa

Reputation: 18937

Something like this?

(define (clause-match stmt)
  (match stmt
    [(list 'entity e0 attrs ...) 
     (list (list '?e0 e0) (cons '?attrs attrs))]
    [else #f]))

then

(clause-match '(entity e1 (prov:type "Revision")))
=> '((?e0 e1) (?attrs (prov:type "Revision")))

(clause-match '(entity e1 (prov:type "Revision") (one:two "Three")))
=> '((?e0 e1) (?attrs (prov:type "Revision") (one:two "Three")))

Upvotes: 1

woutervs
woutervs

Reputation: 1510

(match statement2 
[
(list 'entity ?
     (car (cdr statement1)
) 
(cons (car (cdr statement1)
      ) (car (cdr statement2)))
] [list _ 'no]))
)

Your closing brackets and so on don't match up. Fixing them should resolve the syntax error.

Upvotes: 0

Related Questions