2

I am new to prolog , I have a list in prolog like A=[1,2,3,4], and than I accessed nth element using nth(N,[_|T],R). Now I have Nth element in R, than I have done some calculation on R. Now what I want is to update that nth element in list.

Because of I am doing a lot of calculations with each element in list I can't make a new list each time.

I didn't find any method to update list.

yagyavrat
  • 107
  • 2
  • 11
  • You do make a new list each time technically, Prolog is not an imperative language, but a logical one, it solves by finding states for the "variables". You need to state the rule so that the result of your calculation is the list with the nth item altered. – Orbling Nov 19 '12 at 17:04
  • basically what i want is to is it possible to write a function like setElement(List,Index,Value). – yagyavrat Nov 19 '12 at 17:05
  • 2
    In which case - Related: http://stackoverflow.com/questions/8519203/prolog-replace-an-element-in-a-list-at-a-specified-index – Orbling Nov 19 '12 at 17:14
  • Making it simple...assume..I am solving a list addition problem...I have two list and addition of nth element of two lists are assigned two nth element of third list.. so I have to update third list n time.. how can i do it.. – yagyavrat Nov 19 '12 at 17:19
  • Does the third list already exist, or are you building it as you go for each element? – Orbling Nov 19 '12 at 17:29
  • I am making it with each element... – yagyavrat Nov 19 '12 at 17:32

2 Answers2

1

With regard to our conversation, you can add two lists together, creating a third, by specifying that the two head elements of the source lists, added together, make the head element of the result list, and that this applies to the remainder of the lists.

There is also a need for a base case, that is, when the two source lists are empty, so should the result list.

addLists([X|A], [Y|B], [Z|C]) :- Z is X+Y, addLists(A, B, C).
addLists([], [], []).

Remember you are always aiming to specify the constraints of the answer, more than the method of answering it. Prolog is very different to other programming languages in that you do not tell it how to do something, you simply tell it conditions that are true for the answer and let it extrapolate it.

Orbling
  • 20,413
  • 3
  • 53
  • 64
1

From the comments you exchanged with @Orbling seems that what you need is a kind of maplist/4

process_list(A, B, C) :-
   maplist(process_elem, A, B, C).

process_elem(A, B, C) :- C is A + B. % or whatever needed

If you are using the index in process_elem then this is not appropriate. Then make a recursive visit of list, passing down the index

process_list(A, B, C) :-
   process_list(1, A, B, C).
process_list(I, [A|As], [B|Bs], [C|Cs]) :-
   C is A + B * I, % or whatever needed
   J is I + 1,
   !, process_list(J, As, Bs, Cs).
process_list(_, [], [], []).

edit Just to add to the various ways exposed in answers to the question @Orbling suggests, here a way using nth0/4

?- I = 6, nth0(I,"hello world",_,T), nth0(I,U,0'W,T), format('~s',[U]).
hello World
Community
  • 1
  • 1
CapelliC
  • 59,646
  • 5
  • 47
  • 90