defpat
1 defpat
defpat
pat-lambda
2 match-case-lambda
match*-case-lambda
3 opt-case-lambda
case-lambda/  opt
4 opt-match-case-lambda
match*-case-lambda/  opt
7.0

defpat

 (require defpat) package: defpat

    1 defpat

    2 match-case-lambda

    3 opt-case-lambda

    4 opt-match-case-lambda

1 defpat

source code: https://github.com/AlexKnauth/defpat

This module provides the forms defpat and pat-lambda.

defpat is a version of define for functions where the arguments can be match patterns.

pat-lambda is a version of lambda where (again) the arguments can be match patterns.

syntax

(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.

>

 

(require defpat/defpat)

>

 

(defpat (distance (list x1 y1) (list x2 y2))
  (sqrt (+ (* (- x2 x1) (- x2 x1))
           (* (- y2 y1) (- y2 y1)))))

>

 

(distance (list 0 0) (list 3 4))

5

>

 

(distance (list 0 3) (list 4 0))

5

The arg-pat can’t start with a [ though, because square brackets are used to specify optional arguments:

>

 

(require defpat/defpat)

>

 

;; 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)))))

>

 

(distance (list 0 3) (list 4 0))

5

>

 

(distance (list 3 4))

5

(defpat (head . args) body ...)

expands to

(defpat head (pat-lambda args body ...))

syntax

(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 argument

It 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,
(pat-lambda ((list x y) (vector z))
  body)
expands to
(lambda (%1 %2)
  (match-define (list x y) %1)
  (match-define (vector z) %2)
  body)
and for keyword-arguments,
(pat-lambda (#:kw (list x y))
  body)
expands to
(lambda (#:kw %#:kw)
  (match-define (list x y) %#:kw)
  body)

2 match-case-lambda

 (require defpat/match-case-lambda) package: defpat

syntax

(match*-case-lambda clause ...)

 
clause = [args body ...+]
  | [args (=> id) body ...+]
  | [args #:when cond-expr body ...+]
     
args = [arg-pat ...]
  | rest-id
  | [arg-pat ... . rest-id]
like case-lambda, except that each arg-pat can be an arbitrary match pattern.

As an example,
(match*-case-lambda
  [[(list x y) (vector z)]
   body])
is equivalent to
(case-lambda
  [(tmp1 tmp2)
   (match* [tmp1 tmp2]
     [[(list x y) (vector z)]
      body])])

Clauses with the same arity are grouped together into a single case-lambda clause with multiple match* clauses within it.

3 opt-case-lambda

 (require defpat/opt-case-lambda) package: defpat

syntax

(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

 (require defpat/opt-match-case-lambda) package: defpat

syntax

(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)
like match*-case-lambda, except that it supports optional arguments.