Create a recursive function in prolog

Click For Summary
SUMMARY

This discussion focuses on creating recursive functions in Prolog to sum two lists and count repeated elements. The function listSum(X, Y, Z) is defined to compute the element-wise sum of two lists, where Z_i = X_i + Y_i. A sample implementation is provided using the syntax [H|T] to handle list heads and tails. Additionally, the use of SWI Prolog's debugger is recommended for tracing execution.

PREREQUISITES
  • Prolog syntax and list handling
  • Understanding of recursion in programming
  • Basic knowledge of SWI Prolog debugging tools
  • Familiarity with Prolog predicates and their definitions
NEXT STEPS
  • Implement the listSum(X, Y, Z) function in Prolog
  • Explore advanced list manipulation techniques in Prolog
  • Learn about Prolog's built-in predicates for list operations
  • Investigate debugging techniques in SWI Prolog for recursive functions
USEFUL FOR

Prolog developers, computer science students, and anyone interested in functional programming and recursion in Prolog.

Wez
can anyone help me out with this please
i want to create a recursive function in prolog to do the following thing

sum lists(X; Y;Z) holds for lists X = [x1; : : : xn], Y = [y1; : : : yn] and
Z of numbers if and only if Z = [z1; : : : zn] and zi = xi+yi (1  i  n).
 
Technology news on Phys.org


This is 'Hello World' level for lists in Prolog.

For starters create a 'function' that sums a list.

sum([1,2,3],X).
X = 6.

You should get the idea.

Do you self study Prolog?
 


yeah i do...i have that one done...but i wanted for two list

e.g [1,2,3,4] and [1,2,3,4] would output [2,4,6,8]

and for the second would be how many elements are repeated

e.g [1,2,3,4] and [1,6,7,4] would output 2

thanks
 


First, state it in a logical manner. For instance,

Z is the sum of lists X and Y iff Z_i = X_i + Y_i. This is good, but this is not a recursive enough definition to be implemented directly in prolog. Perhaps a better way of saying the same thing would involve mentioning the first elements of the lists X, Y, and Z, and then what has to be true of the lists' tails...

So you would write:

listSum(X, Y, Z) :- < something about the lists' heads > , <something about the lists' tails>.
 


Are you familiar with the syntax of the lists? namely [H|T]? Notice that you can use [H|T] for your 'return' variable not just for 'incoming' variable this is what I mean:

e.g.

Code:
sum([1,2,3,4,5],Sum,ReverseSteps).
Sum = 15
ReverseSteps = [15, 14, 12, 9, 5]]

you are familiar with this

Code:
sum([],0).
sum([H|T],Sum):-
    sum(T,Sum1),
    Sum is Sum1 + H.

the only thing I can think you are not familiar is to pass something back in the list like this

Code:
sum1([],0,[]).
sum1([H|T],Sum,[Sum|ReturnList]):-
    sum1(T,Sum1,ReturnList),
    Sum is Sum1 + H.

the solution to your original problem is similar in concept to this - how to use the return of predicate in the list. In the example it made sense to jump into recursion in first step, it is not so in your problem.

HTH.
 


Aside. If you are using SWI Prolog, I recommend to use the debugger for tracing the execution - it is not great but it helps.
 


Code:
sum(X,Y,Z) :- X == [], write(over).
sum(X,Y,Z) :- [Tmp1 | X_new] = X, [Tmp2 | Y_new] = Y, Tmp is Tmp1 + Tmp2, [ Tmp | Z ]=Z1 , sum(X_new, Y_new, Z1) .
 

Similar threads

  • · Replies 14 ·
Replies
14
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 11 ·
Replies
11
Views
2K
Replies
31
Views
4K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 17 ·
Replies
17
Views
3K
  • · Replies 11 ·
Replies
11
Views
4K
  • · Replies 2 ·
Replies
2
Views
2K