Skip to content
This repository has been archived by the owner on Dec 29, 2018. It is now read-only.

Optional pattern #119

Open
dmitrys99 opened this issue Jan 17, 2016 · 2 comments
Open

Optional pattern #119

dmitrys99 opened this issue Jan 17, 2016 · 2 comments

Comments

@dmitrys99
Copy link

Is there a way to implement optional items in pattern, like "?" operation in regexp?
So, for example (list a b (? c)) will allow skip part 'c'?

@m2ym
Copy link
Owner

m2ym commented Jan 17, 2016

No, but you can use or pattern instead like (or (list a b) (list a b c)). It is possible to implement optional pattern as pattern expansion.

@guicho271828
Copy link
Contributor

Without explicit support, you will end up something like this.

(defpattern list? (&rest args)
  (process-list? args))

(defun process-list? (args)
  (let* ((pos (position-if (lambda (x) (and (consp x) (eq (car x) '?))) args))
          (? (elt pos args)) (content (second ?)))
     `(or ,(process-list? (subst content ? args))
            ,(process-list? (remove ? args)))))

Unfortunately however, this does not provide a general implementation of ?. You always have to implement ?-compatible version of whatever pattern you use, e.g., when you try to use ? for vector, you should implement and use vector?. Fundamentally this has the same problem as macro expansion (expansion cannot see the outer construct).

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants