12 Legacy Forms

▼ |  | The Typed Racket Reference

1 | Type Reference
2 | Special Form Reference
3 | Libraries Provided With Typed Racket
4 | Typed Classes
5 | Typed Units
6 | Utilities
7 | Exploring Types
8 | Typed Racket Syntax Without Type Checking
9 | Typed Regions
10 | Optimization in Typed Racket
11 | Unsafe Typed Racket operations
12 | Legacy Forms
13 | Compatibility Languages
14 | Experimental Features

On this page:

lambda:
λ:
plambda:
opt-lambda:
popt-lambda:
case-lambda:
pcase-lambda:
let:
plet:
letrec:
let*:
let-values:
letrec-values:
let*-values:
let/ cc:
let/ ec:
define:
struct:
define-struct:
define-struct/ exec:
for:
for*/ and:
for*/ first:
for*/ flvector:
for*/ extflvector:
for*/ fold:
for*/ hash:
for*/ hasheq:
for*/ hasheqv:
for*/ last:
for*/ list:
for*/ lists:
for*/ set:
for*/ or:
for*/ product:
for*/ sum:
for*/ vector:
for*:
for/ and:
for/ first:
for/ flvector:
for/ extflvector:
for/ fold:
for/ hash:
for/ hasheq:
for/ hasheqv:
for/ last:
for/ list:
for/ lists:
for/ set:
for/ or:
for/ product:
for/ sum:
for/ vector:
do:
define-type-alias
define-typed-struct
require/ opaque-type
require-typed-struct
require-typed-struct/ provide
pdefine:
pred
Un
mu
Tuple
Parameter
Pair
values

6.6
top ← prev up next →

### 12`` Legacy Forms

The following forms are provided by Typed Racket for backwards compatibility.

> syntax (lambda: formals . body)
> formals |  | = |  | ([v : t] ...)
> |  | | |  | ([v : t] ... v : t *)
> |  | | |  | ([v : t] ... v : t ooo bound)

A function of the formal arguments v, where each formal argument has the associated type. If a rest argument is present, then it has type (Listof t).

> syntax (λ: formals . body)

An alias for the same form using lambda:.

> syntax (plambda: (a ...) formals . body)
> (plambda: (a ... b ooo) formals . body)

A polymorphic function, abstracted over the type variables a. The type variables a are bound in both the types of the formal, and in any type expressions in the body.

> syntax (opt-lambda: formals . body)
> formals |  | = |  | ([v : t] ... [v : t default] ...)
> |  | | |  | ([v : t] ... [v : t default] ... v : t *)
> |  | | |  | ([v : t] ... [v : t default] ... v : t ooo bound)

A function with optional arguments.

> syntax (popt-lambda: (a ...) formals . body)
> (popt-lambda: (a ... a ooo) formals . body)

A polymorphic function with optional arguments.

> syntax case-lambda:

An alias for case-lambda.

> syntax (pcase-lambda: (a ...) [formals body] ...)
> (pcase-lambda: (a ... b ooo) [formals body] ...)

A polymorphic function of multiple arities.

> syntax (let: ([v : t e] ...) . body)
> (let: loop : t0 ([v : t e] ...) . body)

Local bindings, like let, each with associated types. In the second form, t0 is the type of the result of loop (and thus the result of the entire expression as well as the final expression in body). Type annotations are optional.

Examples:

> > (: filter-even : (Listof Natural) (Listof Natural) -> (Listof Natural))
> > (define (filter-even lst accum)
> (if (null? lst)
> accum
> (let: ([first : Natural (car lst)]
> [rest : (Listof Natural) (cdr lst)])
> (if (even? first)
> (filter-even rest (cons first accum))
> (filter-even rest accum)))))
> > (filter-even (list 1 2 3 4 5 6) null)
> - : (Listof Nonnegative-Integer)
> '(6 4 2)

Examples:

> > (: filter-even-loop : (Listof Natural) -> (Listof Natural))
> > (define (filter-even-loop lst)
> (let: loop : (Listof Natural)
> ([accum : (Listof Natural) null]
> [lst : (Listof Natural) lst])
> (cond
> [(null? lst) accum]
> [(even? (car lst)) (loop (cons (car lst) accum) (cdr lst))]
> [else (loop accum (cdr lst))])))
> > (filter-even-loop (list 1 2 3 4))
> - : (Listof Nonnegative-Integer)
> '(4 2)

> syntax (plet: (a ...) ([v : t e] ...) . body)

A polymorphic version of let:, abstracted over the type variables a. The type variables a are bound in both the types of the formal, and in any type expressions in the body. Does not support the looping form of let.

> syntax (letrec: ([v : t e] ...) . body)
> syntax (let*: ([v : t e] ...) . body)
> syntax (let-values: ([([v : t] ...) e] ...) . body)
> syntax (letrec-values: ([([v : t] ...) e] ...) . body)
> syntax (let*-values: ([([v : t] ...) e] ...) . body)

Type-annotated versions of letrec, let*, let-values, letrec-values, and let*-values. As with let:, type annotations are optional.

> syntax (let/cc: v : t . body)
> syntax (let/ec: v : t . body)

Type-annotated versions of let/cc and let/ec. As with let:, the type annotation is optional.

> syntax (define: v : t e)
> (define: (a ...) v : t e)
> (define: (a ... a ooo) v : t e)
> (define: (f . formals) : t . body)
> (define: (a ...) (f . formals) : t . body)
> (define: (a ... a ooo) (f . formals) : t . body)

These forms define variables, with annotated types. The first form defines v with type t and value e. The second form does the same, but allows the specification of type variables. The third allows for polydotted variables. The fourth, fifth, and sixth forms define a function f with appropriate types. In most cases, use of : is preferred to use of define:.

Examples:

> > (define: foo : Integer 10)
> > (define: (A) mt-seq : (Sequenceof A) empty-sequence)
> > (define: (add [first : Integer]
> [rest : Integer]) : Integer
> (+ first rest))
> > (define: (A) (poly-app [func : (A A -> A)]
> [first : A]
> [rest : A]) : A
> (func first rest))

> syntax struct:

An alias for struct.

> syntax define-struct:

An alias for define-struct.

> syntax define-struct/exec:

An alias for define-struct/exec.

> syntax for:

An alias for for.

> syntax for*/and:
> syntax for*/first:
> syntax for*/flvector:
> syntax for*/extflvector:
> syntax for*/fold:
> syntax for*/hash:
> syntax for*/hasheq:
> syntax for*/hasheqv:
> syntax for*/last:
> syntax for*/list:
> syntax for*/lists:
> syntax for*/set:
> syntax for*/or:
> syntax for*/product:
> syntax for*/sum:
> syntax for*/vector:
> syntax for*:
> syntax for/and:
> syntax for/first:
> syntax for/flvector:
> syntax for/extflvector:
> syntax for/fold:
> syntax for/hash:
> syntax for/hasheq:
> syntax for/hasheqv:
> syntax for/last:
> syntax for/list:
> syntax for/lists:
> syntax for/set:
> syntax for/or:
> syntax for/product:
> syntax for/sum:
> syntax for/vector:

Aliases for the same iteration forms without a :.

> syntax do:

An alias for do.

> syntax define-type-alias

Equivalent to define-type.

> syntax define-typed-struct

Equivalent to define-struct:

> syntax require/opaque-type

Similar to using the opaque keyword with require/typed.

> syntax require-typed-struct

Similar to using the struct keyword with require/typed.

> syntax require-typed-struct/provide

Similar to require-typed-struct, but also provides the imported identifiers.

> syntax pdefine:

Defines a polymorphic function.

> syntax (pred t)

Equivalent to (Any -> Boolean : t).

> syntax Un

An alias for U.

> syntax mu

An alias for Rec.

> syntax Tuple

An alias for List.

> syntax Parameter

An alias for Parameterof.

> syntax Pair

An alias for Pairof.

> syntax values

An alias for Values.
top ← prev up next →