On this page:
keyword-case-lambda
mutable-keyword-case-lambda
clause->proc/  keyword-case-lambda
7.7

3 keyword-case-lambda

 (require keyword-lambda/keyword-case-lambda)
  package: hash-lambda

syntax

(keyword-case-lambda clause ...)

 
clause = [kw-formals maybe-when body-expr ...+]
  | [#:args-hash match-pattern maybe-when body-expr ...+]
  | [#:rest match-pattern maybe-when body-expr ...+]
  | 
[#:kws match-pattern #:kw-args match-pattern #:rest match-pattern
       maybe-when body-expr ...+]
  | [_ maybe-when body-expr ...+]
  | [else maybe-when body-expr ...+]
  | [#:when condition-expr body-expr ...+]
  | [#:unless condition-expr body-expr ...+]
     
maybe-when = 
  | #:when condition-expr
  | #:unless condition-expr
     
kw-formals = (arg ...)
  | (arg ... . rest-id)
  | rest-id
     
arg = id
  | [id default-expr]
  | keyword id
  | keyword [id default-expr]
like case-lambda , but accepting keyword- and optional arguments, and allowing args-hashes with the #:args-hash keyword, and pattern matching with the match-patterns.

The kw-formals work just like the kw-formals for lambda.

The #:rest match-pattern clause matches the list of arguments against match-pattern, which can be any pattern that can be used with match (not just an identifier).

The #:args-hash match-pattern clause matches the args-hash against match-pattern, which again can be any pattern that can be used with match.

The #:kws match-pattern #:kw-args match-pattern #:rest match-pattern clause matches the keywords, keyword-arguments, and by-position arguments (from keyword-lambda) against the 3 match-patterns.

The _ and else clauses recive any arguments, including keyword-arguments, but it can still go on to the next clause if the #:when condition is is false (or the #:unless condition is true).

The clauses starting with #:when and #:unless recive any arguments, (including keyword-arguments), but can still go on to the next clause if the #:when condition is false (or the #:unless condition is true).

It also allows you to use #:when and #:unless in the clauses.

keyword-case-lambda does not create a mutable-match-lambda-procedure, but it uses mutable-match-lambda-clause-append (which creates a normal immutable procedure), so it supports going to the next clause with mutable-match-lambda-next. (mutable-keyword-case-lambda creates a mutable-match-lambda-procedure)

Examples:
> (define f
    (keyword-case-lambda))
> (procedure-arity f)

'()

> (f)

f: arity mismatch;

 the expected number of arguments does not match the given

number

  given: 0

> (define f
    (keyword-case-lambda
     [() 0]
     [(x) 1]
     [(x y) 2]
     [args (length args)]))
> (f)

0

> (f 1)

1

> (f 1 2)

2

> (f 1 2 3 4 5 6)

6

> (define pythag
    (keyword-case-lambda
     [(#:a a #:b b) (sqrt (+ (sqr a) (sqr b)))]
     [(#:c c #:a a) (sqrt (- (sqr c) (sqr a)))]
     [(#:c c #:b b) (sqrt (- (sqr c) (sqr b)))]
     [(#:a a #:b b #:c c) (= (+ (sqr a) (sqr b)) (sqr c))]))
> (pythag #:a 3 #:b 4)

5

> (pythag #:c 5 #:a 3)

4

> (pythag #:c 5 #:b 4)

3

> (pythag #:a 3 #:b 4 #:c 5)

#t

> (pythag #:a 3 #:b 4 #:c 6)

#f

> (define f
    (keyword-case-lambda
     [#:args-hash args-hash args-hash]))
> (f 0 1 2 #:kw "kw-arg")

'#hash((1 . 1) (0 . 0) (2 . 2) (#:kw . "kw-arg"))

> (define f
    (keyword-case-lambda
     [#:args-hash (hash-table ['#:m (? number? m)] ['#:v (? number? v)]) `(m: ,m v: ,v)]
     [(#:mass [m 0] #:velocity [v 0]) #:when (andmap number? (list m v)) `(mass: ,m velocity: ,v)]
     [#:rest `(m: ,m v: ,v) #:when (andmap number? (list m v)) `(m: ,m v: ,v)]
     [else (error "error")]))
> (f #:m 2 #:v 1)

'(m: 2 v: 1)

> (f #:mass 2)

'(mass: 2 velocity: 0)

> (f #:mass 2 #:velocity 1)

'(mass: 2 velocity: 1)

> (f)

'(mass: 0 velocity: 0)

> (f 'm: 2 'v: 1)

'(m: 2 v: 1)

> (f "something")

error

syntax

(mutable-keyword-case-lambda clause ...)

creates a clause-proc that can be used for a mutable-match-lambda-procedure, so you can use (clause->proc #:keyword-case-lambda clause).