Skip to main content

Empty List

rascal-0.41.2

Synopsis

Illegal operation on an empty list.

Types

data RuntimeException = EmptyList();

Usage

import Exception; (only needed when EmptyList is used in catch)

Description

Rascal provides many operations and functions on lists, see list values and list functions. This error is generated when a function or operation cannot handle the empty list.

Remedies:

  • Guard the function or operation with a test on the empty list (isEmpty) and take alternative action in that case.
  • Catch the EmptyList yourself, see try catch.

Examples

Import the List library and introduce L with an empty list as value:

rascal>import List;
ok
rascal>L = [];
list[void]: []

Taking the head of an empty list gives an error:

rascal>head(L);
|std:///List.rsc|(4462,9,<168,31>,<168,40>): EmptyList()
at head(|std:///List.rsc|(4431,45,<168,0>,<168,45>))
at $(|prompt:///|(0,8,<1,0>,<1,8>))

This is the case when taking the tail as well:

rascal>tail(L);
|std:///List.rsc|(17576,9,<696,37>,<696,46>): EmptyList()
at tail(|std:///List.rsc|(17539,51,<696,0>,<696,51>))
at $(|prompt:///|(0,8,<1,0>,<1,8>))

We can also catch the EmptyList error. First import the Rascal exceptions (which are also included in Prelude) and IO:

rascal>import Exception;
ok
rascal>import IO;
ok
rascal>try
|1 >>>> println(head(L));
|2 >>>>catch EmptyList():
|3 >>>> println("Cannot take head of empty list");
Cannot take head of empty list
ok