Create a recursive function in prolog

In summary, this conversation is about creating a recursive function in Prolog to sum two lists and return the resulting list. The participants discuss the logical structure of the function and provide an example of a similar function that sums a single list. The concept of using a return list in Prolog is also mentioned.
  • #1
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
  • #2


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?
 
  • #3


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
 
  • #4


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>.
 
  • #5


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.
 
  • #6


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


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) .
 

Related to Create a recursive function in prolog

1. What is a recursive function in Prolog?

A recursive function in Prolog is a function that calls itself repeatedly until a base case is reached. This allows for solving problems that can be broken down into smaller versions of the same problem.

2. How do you create a recursive function in Prolog?

To create a recursive function in Prolog, you first need to define a base case that will stop the function from calling itself. Then, you can define the recursive case, which will call the function with a smaller version of the problem until it reaches the base case.

3. What are the advantages of using recursive functions in Prolog?

Recursive functions in Prolog can simplify the code and make it more readable. They are also useful for solving problems that involve repetition or patterns. Additionally, they can be more efficient in some cases compared to using iterative methods.

4. Are there any limitations to using recursive functions in Prolog?

One limitation of using recursive functions in Prolog is that they can be memory-intensive, as each call creates a new stack frame. This can lead to stack overflow errors if the function is called too many times. Additionally, recursive functions may not be the most efficient solution for certain types of problems.

5. Can recursive functions in Prolog have more than one base case?

Yes, recursive functions in Prolog can have multiple base cases. This allows for more complex problem-solving and can make the function more flexible. However, it is important to ensure that the base cases are defined in a way that the function will eventually reach one of them.

Similar threads

  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
14
Views
2K
  • Programming and Computer Science
Replies
8
Views
964
  • Precalculus Mathematics Homework Help
Replies
17
Views
1K
  • Programming and Computer Science
Replies
31
Views
2K
  • Programming and Computer Science
Replies
6
Views
3K
  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
Replies
4
Views
1K
  • Programming and Computer Science
Replies
17
Views
2K
  • Set Theory, Logic, Probability, Statistics
Replies
5
Views
3K
Back
Top