Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

HInts for Prolog Dice sum

Status
Not open for further replies.

isaacasimov

Programmer
Dec 5, 2010
1
DE
Hi,
I was wondering if anybody can help me with this problem that I would like to solve. I just need some advices on how to start.
Given N dice (thus N lists of [1,2,3,4,5]) I need to find the possible combinations that give as result another number.
Example:
dice(P, N, L) where P is the number we want, N the number of dice and L the list
dice (5, 2, L)
L = [1, 4]
L = [2, 3]
L = [3, 2]
L = [4, 1]

I don't know if recursion is what I need here or not. I just need a clue, I really want to try and solve this problem by myself.

Greetings from Vaduz, Lichtenstein :)
 
It can be done with recursion : dice(5, 2, L) = dice(4, 1, [1|L1]).
You can use a helper predicate
Code:
dice(P, N, L) :-
  dice_h(P, N, [], L).
the search succeeds when you reach
dice(0, 0, L, L).
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top