12.3.4 Application of a function to arguments

The Dylan function apply takes as arguments a function and one or more additional arguments, the final one of which must be a sequence. The apply function calls its first argument — the function — and passes that function the remaining arguments to apply. But instead of passing its final argument as a sequence, it passes each element of the sequence as an individual argument.

The apply function is perhaps most useful in the body of a function that receives a variable number of arguments and must pass those arguments to another function that also takes a variable number of arguments. For example, we can use apply to write a recursive version of the sum function that we defined iteratively in Section 12.2.3, page 172:

// Sum one or more values
define method sum (value, #rest more-values)
  // If only one value, that is the answer
  if (empty?(more-values))
    value;
  // Otherwise, add the first value to the sum of the rest
  else
    value + apply(sum, more-values);
  end if;
end method sum;