On this page:
values
values
values
values
values
fold
call_  with_  values
8.16.0.4

3.7 Multiple Values🔗ℹ

expression

values

 

expression

values(expr, ...)

 

repetition

values

 

repetition

values(repet, ...)

A values form by itself acts as a function that takes any number of values and returns them as multiple result values. If only one expr is provided, the result is the same as just expr, except that it is not in tail position with respect to the values form. Any other number of values must be received by a context that is expecting multiple values, such as with a values binding pattern.

When values is immediately called using the default #%call operator, then static information for the call expression propagates static information for the expr arguments (using statinfo_meta.values_key).

The values operator can also be used to form a repetition.

> values("apple")

"apple"

> values("apple", 1)

"apple"

1

> block:

    let (x, y) = values(1, 2)

    [x, y]

[1, 2]

binding operator

values(bind, ...)

The values binding operator can only be used in places where it’s specifically recognized, normally to match multiple result values. For example, the lhs_bind position of def recognizes values.

Plain parentheses as a binding (as implemented by the #%parens form) work as an alias for values in the places that recognize values.

> def values(x, y) = values(1, 2)

> x+y

3

annotation

values(annot, ...)

The values annotation can only be used in places where it’s specifically recognized, normally to annotate multiple result values. For example, the maybe_res_annot position of fun recognizes values.

> fun two_numbers() :: values(Int, Int):

    values(1, 2)

> def (x, y) = two_numbers()

> x+y

3

reducer

values(id maybe_annot init, ...)

 

reducer

fold(id maybe_annot init, ...)

 

maybe_annot

 = 

:: annot

 | 

:~ annot

 | 

ϵ

 

init

 = 

= expr

 | 

: body; ...

A reducer used with for, expects as many results from a for body as ids. When id is _, a fresh identifier is used, otherwise id is bound as follows. For the first iteration of the for body, each id’s value is the result of the corresponding init. The results of a for body for one iteration then serve as the values of the ids for the next iteration. The values of the whole for expression are the final values of the ids.

The fold reducer form is an alias for values.

> for fold(sum = 0) (i in 1..=10):

    sum + i

55

> for values(sum = 0, product = 1) (i in 1..=10):

    values(sum + i, product * i )

55

3628800

function

fun call_with_values(producer :: Function.of_arity(0),

                     consumer :: Function)

Calls producer with no arguments, and then calls consumer with the result value(s) from producer.

Use call_with_values to dispatch on the number of values that are produced by an expression. The match form cannot make that distinction, because it always expects a single result value from its initial subexpression.

fun get_fruit(n :: NonnegInt):

  match n

  | 0: values()

  | 1: "apple"

  | 2: values("apple", "banana")

  | ~else: values("apple", n +& " bananas")

fun

| show(): println("nothing")

| show(s): println(s)

| show(a, b): println(a +& " and " +& b)

> call_with_values(fun (): get_fruit(0), show)

nothing

> call_with_values(fun (): get_fruit(1), show)

apple

> call_with_values(fun (): get_fruit(2), show)

apple and banana

> call_with_values(fun (): get_fruit(3), show)

apple and 3 bananas