Skip to main content

Empty List

rascal-1.0-SNAPSHOT

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);
|mvn://org.rascalmpl--rascal--0.43.0/List.rsc|(4461,9,<167,31>,<167,40>): EmptyList()
at head(|mvn://org.rascalmpl--rascal--0.43.0/List.rsc|(4430,45,<167,0>,<167,45>))
at $(|prompt:///|(0,8,<1,0>,<1,8>))

This is the case when taking the tail as well:

rascal>tail(L);
|mvn://org.rascalmpl--rascal--0.43.0/List.rsc|(18289,9,<716,37>,<716,46>): EmptyList()
at tail(|mvn://org.rascalmpl--rascal--0.43.0/List.rsc|(18252,51,<716,0>,<716,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