AFAS::Lists
From Agent Factory
This is a lesson from the AF-AgentSpeak language guide.
Contents |
Introduction
This next lesson explores how to use some of the built in functionality for creating and manipulating lists of terms. In AF-AgentSpeak, a list is denoted by a set of square brackets: [] is an empty list; [item1, item2, item3] is a list containing 3 (term) items. The language includes a number of special operators that allow you to manipulate lists. Details of these operators are described below.
Standard (Logical) List Operators
Logics containing support for lists typically provide 2 supporting operators:
- head(?list): return the head of the list (the first item in the list)
- tail(?list): return the tail of the list (the original list minus the first item)
Iterating through lists
Iterating through a list can be performed by combining the head and tail functions within a while loop, e.g.
#agent itlist
?mylist = [one,two,three],
while(?mylist != []) {
.println(head(?mylist)),
?mylist = tail(?mylist)
}
Creating list dynamically
Lists can be created dynamically based on existing beliefs of the agent. The format of the statement is somewhat like the set theory notation for defining restricted classes. The example below demonstrates how the functionality works:
#agent lister
info(B);
info(A);
info(C);
?mylist = list[?x | info(?x)],
while(?mylist != []) {
.println(head(?mylist)),
?mylist = tail(?mylist)
}
This program would generate a list containing A, B, and C in some order (the ordering is typically the order in which the respective beliefs are adopted by the agent, but this ordering is NOT GUARANTEED. Once the list has been created, it removes and prints out the head of the list until the list is empty (similarly to the previous example)
