7.7
defpat
1 defpat
source code: https://github.com/AlexKnauth/defpat
defpat is a version of
define for functions where the
arguments can be
match patterns.
(defpat id expr)
|
(defpat (head args) body ...+) |
|
head | | = | | id | | | | | | (head args) | | | | | | args | | = | | arg ... | | | | | | arg ... . rest-id | | | | | | arg | | = | | arg-pat | | | | | | [arg-pat default-expr] | | | | | | keyword arg-pat | | | | | | keyword [arg-pat default-expr] |
|
like
define, except that each
arg-pat can be an
arbitrary
match pattern.
The arg-pat can’t start with a [ though, because
square brackets are used to specify optional arguments:
|
> | | ;; If the second point is not specified, it computes the | ;; distance to the origin. | (defpat (distance (list x1 y1) [(list x2 y2) (list 0 0)]) | (sqrt (+ (* (- x2 x1) (- x2 x1)) | (* (- y2 y1) (- y2 y1))))) |
|
|
|
5 |
|
5 |
expands to
(pat-lambda kw-formals body ...+)
|
|
kw-formals | | = | | (arg ...) | | | | | | (arg ...+ . rest-id) | | | | | | rest-id | | | | | | arg | | = | | arg-pat | | | | | | [arg-pat default-expr] | | | | | | keyword arg-pat | | | | | | keyword [arg-pat default-expr] |
|
like
lambda, except that each
arg-pat can be an arbitrary
match pattern.
Just as with defpat, the arg-pat can’t start with a [, and
you have to use square brackets to specify an optional argumentIt is very similar to match-lambda**, except that it doesn’t support multiple clauses, and
it allows optional arguments, keyword arguments, and a rest argument.
As an example,
expands to
and for keyword-arguments,
expands to
2 match-case-lambda
(match*-case-lambda clause ...)
|
|
clause | | = | | [args body ...+] | | | | | | [args (=> id) body ...+] | | | | | | [args #:when cond-expr body ...+] | | | | | | args | | = | | [arg-pat ...] | | | | | | rest-id | | | | | | [arg-pat ... . rest-id] |
|
As an example,
is equivalent to
Clauses with the same arity are grouped together into a single
case-lambda clause with multiple match* clauses
within it.
3 opt-case-lambda
(case-lambda/opt clause ...)
|
|
clause | | = | | [args body ...+] | | | | | | args | | = | | (arg-id ... [arg-id default-expr] ...) | | | | | | rest-id | | | | | | (arg-id ... [arg-id default-expr] ... . rest-id) |
|
like
case-lambda, except that it supports optional arguments.
4 opt-match-case-lambda
(match*-case-lambda/opt clause ...)
|
|
clause | | = | | [args body ...+] | | | | | | args | | = | | (arg-pat ... [arg-pat default-expr] ...) | | | | | | rest-id | | | | | | (arg-pat ... [arg-pat default-expr] ... . rest-id) |
|