|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Get inside! Sample the range of functionality easily built with JMSL Library for Time Series Data Analysis, Heat Maps, Portfolio Optimization, Monte Carlo Simulation, Stock Price Charting and more. Download Now! |
|
#1
|
|||
|
|||
|
Other - PROLOG Problem
First of all let me apologize for posting in the wrong forum ... but I didn't find one for prolog... please redirect me if we have one ... and anyone's response if he/she knows the answer is appreciated.
am trying to print the elements of a list.... one per line using a predicate of arity 1 .... which I am able to do with the code: Code:
display_list([]) :- !, write('\nThe list is empty'). % handles empty list
display_list([H|T]) :- write(' '), write(H), nl, display_list
The problem is: I want to have the sequence number of the list element in front of it: For example, 1 North 2 South 3 East 4 West I am not able to able to do that. The code I am writing is: Code:
increment(X,Y) :- Y is X+1.
display_list([]) :- !, write('\nThe list is empty').
display_list([H|T]) :- X = 0, increment(X,Y), write(Y),write(' '), write(H), nl, display_list(T).increment(X,Y) :- Y is X+1.
The output I get as expected is: 11 ?- display_list([north, south, east]). 1 north 1 south 1 east The list is empty true. Also I keep on getting this 'The list is empty' everytime the elements are exhausted and not only when the input list is empty ..... Please advice ...... Thanks |
|
#2
|
||||
|
||||
|
Did you look at the Other Programming Languages forum? Move requested.
__________________
Regards, Eddy Luten. Information: C, C++, STL, Boost, OpenMP, Scriptionary, Google Book of the moment: Design Patterns: Elements of Reusable Object-Oriented Software by Erich Gamma, Richard Helm, Ralph Johnson, John M. Vlissides This post and all subsequent posts by "Thr3ddy" are licensed under the Creative Commons Attribution United States License 3.0: attribute "Eddy Luten" for any code used which was extracted from "Thr3ddy's" posts. |
|
#3
|
||||
|
||||
|
Thread moved from PHP to Other Programming Languages ...
__________________
# Jeremy Explain your problem instead of asking how to do what you decided was the solution. |
|
#4
|
|||
|
|||
|
...nobody answers here?
So i do. The key idea is: You need 2 clauses. Try something like Code:
display_list([]) :-
write('The list is empty').
display_list(L) :-
displs(1, L).
displs(_, []).
displs(COUNTER, [X|REST]) :-
"your code here, with incrementing the counter
and calling this clause with REST"
Forget the cut and your increment clause. I hope this helps a little. |
![]() |
| Viewing: Dev Shed Forums > Programming Languages - More > Other Programming Languages > Other - PROLOG Problem |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|