Prolog Assigning integer to a variable -
I am new to Prol, and I am using GNU prol, so no clip (FD) is permitted. Whatever I'm trying to do, is for an integer n, make a list with elements of 1 ~ n. Set (3, T). Production T = [1,2,3] Even so far away from me:
set (0, []). Set (N, T): - Set (N-1, T1), (T1, [N], T). When I try Set (2, T), it crashes. I was debugged with a trace and found out that it is not evaluating N-1 but it is doing N -1-1-1 ...
Anyone can tell me that How to solve this? Thank you.
should be:
set (n, t): - N2N1, set (N2, T1), append (T1, [N], T). Arithmetic operations are done using the is / 2 . There is a cloak for N-1 , - (N, 1) (like N2 is N-1 is the short story (N2, N-1) ), then you were just making the infinite tree - (- (- (- (... (1), 1,1,1) A little educational note: If you want to have a set / 2 for a proper connection, then it's like Can answer the questions set (3, x) , set (x, [1,2,3]) and set (x, y) Without error, you should write this way: set (0, []). Net (value, list): - Length (list, value), attachment (shortlist, [value], list), ValueMinusOne value -1, set (valiuminson, shorter list). This is always possible to achieve the result of arithmetic operations in this way because the input value (length of the list) is either explicitly given or length / 1 . < / Html>
Comments
Post a Comment