Tagged Values
1 Overview
2 API Reference
tag
variant
apply/  variant
call-with-variant
let*-variant
8.16

Tagged Values🔗ℹ

 (requirevariant) package:variant

1 Overview🔗ℹ

This package implements variants (tagged values) as the dual of Racket’s native multiple values, establishing a mathematical correspondence between programming constructs and set operations:

2 API Reference🔗ℹ

struct

(struct tag (number)
    #:transparent)
  number : natural?
A structure type for tags.

Examples:
> (tag 1)

(tag 1)

> (tag 0)

(tag 0)

> (tag -1)

tag: contract violation

  expected: natural?

  given: -1

procedure

(variant v ... [#:tag n])  any

  v : any/c
  n : natural? = 0
A variant-aware version of values. Constructs tagged values. When n is 0 (default), returns plain values.

Examples:
> (variant 1 2 3)

1

2

3

> (variant 1 2 3 #:tag 0)

1

2

3

> (variant 1 2 3 #:tag 1)

(tag 1)

1

2

3

procedure

(apply/variant proc v ... lst [#:tag n])  any

  proc : procedure?
  v : any/c
  lst : list?
  n : natural? = 0
A variant-aware version of apply. Applies proc to (list*v...lst) with a tagn.

Examples:
> (apply/variant + 1 2 (list 3))

6

> (apply/variant + 1 2 (list 3) #:tag 0)

6

> (apply/variant + 1 2 (list 3) #:tag 1)

application: procedure does not accept keyword arguments

  procedure: +

  arguments...:

   1

   2

   3

   #:tag 1

> (apply/variant
   (λ (a b #:tag [n 0])
     (cons (vector a b) n))
   (list 1 2)
   #:tag 1)

'(#(1 2) . 1)

procedure

(call-with-variant generator receiver)  any

  generator : (-> any)
  receiver : procedure?
A variant-aware version of call-with-values. Applies receiver to the variant produced by generator.

Examples:
> (call-with-variant (λ () (variant 'a 'b)) cons)

'(a . b)

> (call-with-variant (λ () (variant 'a 'b #:tag 0)) cons)

'(a . b)

> (call-with-variant (λ () (variant 'a 'b #:tag 1)) cons)

application: procedure does not accept keyword arguments

  procedure: cons

  arguments...:

   'a

   'b

   #:tag 1

> (call-with-variant
   (λ () (variant 'a 'b))
   (λ (a b #:tag [n 0])
     (cons (vector a b) n)))

'(#(a b) . 0)

> (call-with-variant
   (λ () (variant 'a 'b #:tag 1))
   (λ (a b #:tag [n 0])
     (cons (vector a b) n)))

'(#(a b) . 1)

syntax

(let*-variant ([kw-formals rhs-expr] ...) body ...+)

 
kw-formals = (arg ...)
  | (arg ...+ . rest-id)
  | rest-id
     
arg = id
  | [id default-expr]
  | #:tag id
  | #:tag [id default-expr]
A variant-aware version of let*-values. Works with variants.

Examples:
> (let*-variant ([v* (variant 1 2 3)]) v*)

'(1 2 3)

> (let*-variant ([(v . v*) (variant 1 2 3)]) (cons v* v))

'((2 3) . 1)

> (let*-variant ([(v . v*) (variant 1 2 3 #:tag 0)]) (cons v* v))

'((2 3) . 1)

> (let*-variant ([(v . v*) (variant 1 2 3 #:tag 1)]) (cons v* v))

application: procedure does not accept keyword arguments

  procedure: ...gs/variant/main.rkt:51:6

  arguments...:

   1

   2

   3

   #:tag 1

> (let*-variant ([(#:tag n v . v*)
                  (variant 1 2 3 #:tag 1)])
    (cons (cons v* v) n))

'(((2 3) . 1) . 1)

> (let*-variant ([(#:tag [n 0] v . v*)
                  (variant 1 2 3)])
    (cons (cons v* v) n))

'(((2 3) . 1) . 0)

> (let*-variant ([(#:tag n v . v*)
                  (variant 1 2 3)])
    (cons (cons v* v) n))

application: required keyword argument not supplied

  procedure: ...kgs/variant/main.rkt:51:6

  required keyword: #:tag

  arguments...:

   1

   2

   3

> (let*-variant ([(#:tag n v . v*)
                  (variant 1 2 3 #:tag 0)])
    (cons (cons v* v) n))

application: required keyword argument not supplied

  procedure: ...kgs/variant/main.rkt:51:6

  required keyword: #:tag

  arguments...:

   1

   2

   3

 
close