%Insert an element onto the n-th position of a list.
domains
element=integer
list=element*
predicates
insert(list, element, integer, integer, list)
clauses
%If initial list is empty(no elements in the list and CurrentPosition is still 1 because it was not incremented)
insert([], Element, InsertionPosition, 1, [Element]).
%We reached the moment in which the initial list is empty, but CurrentPosition is not 1(case handled above)
insert([], Element, InsertionPosition, CurrentPosition, []).
%Reached the case in which CurrentPosition is equal to InsertionPosition so we ll insert the element!
insert(List, Element, InsertionPosition, InsertionPosition, [Element| ResultedListTail]):-
CurrentPositionCopy = InsertionPosition + 1,
insert(List, Element, InsertionPosition, CurrentPositionCopy, ResultedListTail).
%By default we should copy element from initial list to resulted list and increment currentPosition
insert([Head| Tail], Element, InsertionPosition, CurrentPosition, [Head| ResultedListTail]):-
CurrentPositionCopy = CurrentPosition + 1,
insert(Tail, Element, InsertionPosition, CurrentPositionCopy, ResultedListTail).
goal
%We will insert element 2 on position 2
insert([1, 3, 4, 5, 6, 7], 2, 2, 1, ResultedList),
write(ResultedList).
Posts on different topics such as Java, PHP, freelancing, various frameworks and programming principles.
Sunday, October 27, 2013
Monday, October 7, 2013
Installing phpunit/PHP_Timer issue
After uninstalling phpunit/PHP_Timer and trying to install another version I was getting the message
No releases available for package "pear.phpunit.de/PHP_Timer" install failed
But I did not have PHP_Timer installed at all, the solution for this was to run
pear clear-cache
After that command to install phpunit/PHP_Timer pear install phpunit/PHP_Timer ran successfully!
No releases available for package "pear.phpunit.de/PHP_Timer" install failed
But I did not have PHP_Timer installed at all, the solution for this was to run
pear clear-cache
After that command to install phpunit/PHP_Timer pear install phpunit/PHP_Timer ran successfully!
Subscribe to:
Posts (Atom)