Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Pairs and Lists is a great way to manipulate and store certain data, especially for recursive functions.
- ex: [rob,john,lisa,conner].
- [josh,jake,eats(cheese)].
- The arrays follow a basic structure:
- - The first element in the list is the HEAD, and then we logically denote the separation between the head and the rest of the list
- with a vertical bar '|';
- - The remaining items are the TAIL;
- [Head|Tail]
- Only non-empty lists can have a head and a tail.
- ex1: If we type the following in a query: ?- [Head|Tail] = [mirko, josh, jake, elisa, don, likes(jake, cars)].
- It is going to print the following: Head = mirko,
- Tail = [josh, jake, elisa, don, likes(jake, cars)]
- ex2: But the following query: ?- [FirstItem, SecondItem | Tail] = [mirko, josh, jake, elisa, don, likes(jake, cars)].
- Is going to print the following: FirstItem = mirko,
- SecondItem = josh,
- Tail = [jake, elisa, don, likes(jake, cars)]
- ex: Query: ?- [_, _, [_|X] | _] = [[], dead(z), [2, [b,c]], [], z, [2, [b,c]]].
- Output: X = [[b,c]]
- ### The Member Function ###
- The member function is a recursive function.
- member(X, [X|T]). ---> The termination condition (means, if the member we are looking for is the head, return true and stop)
- member(X, [H|T]):-
- member(X,T). ---> This means, if the element is not in the head, search in the tail. After this function is called, the first
- element in the tail will become the head element of the new list, and it will check again for the (now new)
- head of the list.
Add Comment
Please, Sign In to add comment