I am trying to make my own pattern-matching system in Scheme. To begin I am making a parser for s-expressions that divides them into tokens like this:
'(1 2 b (3 4))
=> '(number number symbol (number number))
It should be noted that I have not used define-syntax
before in Scheme so that may be where I am messing up. Chez Scheme throws me this error:
Exception: invalid syntax classify at line 21, char 4 of pmatch.scm
. Note that the line numbers won't correspond exactly to the snippet here. Does anyone know what I am doing wrong?
(define-syntax classify
(syntax-rules ()
((_ checker replacement)
((checker (car sexpr)) (cons replacement (classify-sexpr (cdr sexpr)))))))
(define (classify-sexpr sexpr)
(cond
((null? sexpr) sexpr)
(classify list? (classify-sexpr (car sexpr)))
(classify number? 'number)
(classify symbol? 'symbol)
(else
(cons 'symbol (classify-sexpr (cdr sexpr))))))
(display (classify-sexpr '(1 (b 3) (4 5) 6)))
Your code is hugely confused. In fact it's so confused I'm not sure what you're trying to do completely: I've based my answer on what you say the classifier should produce at the start of your question.
sexpr
which has no meaning in the macro, and because Scheme macros are hygienic it will definitely not refer to thesexpr
which is the argument toclassify-sexpr
.cond
is botched beyond repair: I can't work out what it's trying to do.list
classification will never be needed: if you want to classify(1 2 3 (x))
as(number number number (symbol))
then you'll simply never reach a case where you have a list which you want to classify since you must walk into it to classify its elements.Instead just write the obvious functions do do what you want:
And now
It may be that what you really want is something which classifies
(1 2 (x 2))
as(list number number (list symbol number))
say. You can do this fairly easily:And now