Skip to content

The List

A list is a container with a collection of items of the same type. It can either be empty or contain one or more items.

Constructing the list

Empty list

Constructing an empty list block, simply remove all members from the input block toolbox [].

empty list

Fig. 1: The empty list block

[]
List()

empty list

Fig. 2: Connecting the empty list block to the variable block

val list_constructor = []
val list_constructor = List()

Non-empty list

The inhabitants of the list can be added or removed using the input block toolbox.

not empty list

Fig. 3: Constructing the non-empty list block

["my name ", "is ", "MNL"]
List("my name ", "is ", "MNL")

not empty list

Fig. 4: Connecting the non-empty list block to the variable block

val list_constructor = ["my name ", "is ", "MNL"]
val list_constructor = List("my name ", "is ", "MNL")

Operator

is empty

To check whether the list is empty, return true when it is empty; otherwise, return false.

is empty - true

Fig. 5: The is-empty block operator with the empty list block

val list_constructor = null([])
val list_constructor = List().isEmpty

is empty - false

Fig. 6: The is-empty block operator with the non-empty list block

val list_constructor = null([])
val list_constructor = List().isEmpty

Retrieve the first inhabitant from the list.

empty list

Fig. 7: The head list operator block

hd(["My name ", "is ", "MNL"])
List("My name ", "is ", "MNL").head

tail

Obtain the inhabitants of the list, starting from the second inhabitant to the last one.

empty list

Fig. 8: The tail list operator block

tl(["My name ", "is ", "MNL"])
List("My name ", "is ", "MNL").tail

append

Insert a new inhabitant into the list and put it into the first position.

empty list

Fig. 9: The append list operator block

("Hello, " :: ["My name ", "is ", "MNL"])
("Hello, " :: List("My name ", "is ", "MNL"))

empty list

Fig. 10: The append a new inhabitant and bind to a variable block.

val my_name_is_mnl = ["My name", "is", "MNL"]
val hello = ("Hello, " :: my_name_is_mnl)
val my_name_is_mnl = List("My name", "is", "MNL")
val hello = ("Hello, " :: my_name_is_mnl)

Example

Sum

empty list

Fig. 11: The head list operator block

fun sum_list (list_a) = if null(list_a)
  then
    0
  else
    (hd(list_a)  + sum_list(tl(list_a)))

val sum_list_application = sum_list([17, 2, 200, 4])
def sum_list (list_a: List[Float]) : Float = if (list_a.isEmpty)
  then
    0
  else
    ((list_a.head)  + sum_list((list_a.tail)))

val sum_list_application = sum_list(List(17, 2, 200, 4))