在方案中进行模式匹配

时间:2013-11-27 13:22:03

标签: regex scheme pattern-matching

有没有人知道如何将模式(entity ?e0 . ?attrs)(entity e0 (prov:type "Revision")匹配? ?e0中的数字0可以是任何数字1,2,3 ......

匹配后的结果为(?e0 . e0) (?attrs (prov:type "Revision"))

我试过这个:

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

但我没有成功.....

错误:match: syntax error in pattern (car (cdr statement1))

请告诉我我错在哪里以及如何解决它!正则表达让我很困惑......

2 个答案:

答案 0 :(得分:1)

这样的东西?

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

然后

(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")))

答案 1 :(得分:0)

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

您的右括号等不匹配。 修复它们应解决语法错误。