What is the prepend() function in Euphoria?

Overview

A sequence is a very important data type in Euphoria. The variables of the sequence data type have their values enclosed inside curly braces {} or inside double quotation marks "".

We can perform operations on elements of a sequence. These elements are indexed. However, the index starts from 1 instead of zero.

The prepend() function

The prepend() function is used to add elements at the beginning of a sequence. This function includes the specified value at the start of a sequence. This means that this new element now takes the index 1.

Syntax

prepend(sequence,newitem)

Parameter

  • The first parameter, sequence, is the sequence to which we’re going to add an item. It can be any sequence of length >= 0.
  • The second parameter, newitem, is the new element that we’re going to add to the sequence. This can be an atom or even another sequence.

Return value

This function returns a sequence with a length greater than the original sequence.

Example

--declare some sequence variables
sequence prependedTo, toBeAddedTo
--define an atom variable
atom scores = 5
toBeAddedTo = {6,7,8}
prependedTo = prepend(toBeAddedTo,scores)
print(1,prependedTo)

Explanation

  • Line 3: We declare two sequence variables, toBeAddedTo and prependedTo. The toBeAddedTo variable has the sequence to which we want to add add an item. prependedTo has the return value of the prepend() function.

  • Line 6: We define an atom scores. This is later on prepended to the sequence.

  • Line 10: We call the prepend() function and save its return value in the variable prependedTo.

  • Line 12: We print the return value of the variable prependedTo.

Free Resources