Question about example in A Beginner's Guide to Visual Prolog
Posted: 30 Jun 2020 1:41
This is the command that calls the procedure add1:
please note the NewList variable is unbound.
The following add1 procedure adds 1 to each element of the input list [1,2,3,4]. It stores the information in the output list NewList.
There is no explicit assignment of NewTail and NewHead to the output list.
I ran this code through the debugger. It works. The head of the input list is bound and 1 is added. The last step add1([],[]) the output list has all the elements +1 of the input list.
I don't see any assignment to NewTail. I don't see how NewHead is assigned in add1.
Code: Select all
listmanager::add1([1,2,3,4], NewList),
The following add1 procedure adds 1 to each element of the input list [1,2,3,4]. It stores the information in the output list NewList.
Code: Select all
clauses
add1([], []). /* boundary condition */
add1([Head|Tail],[NewHead|NewTail]):- /* separate the head from the rest of the list */
NewHead = Head+1, /* add 1 to the first element */
add1(Tail, NewTail). /* call element with the rest of the list */
I ran this code through the debugger. It works. The head of the input list is bound and 1 is added. The last step add1([],[]) the output list has all the elements +1 of the input list.
I don't see any assignment to NewTail. I don't see how NewHead is assigned in add1.