|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
SlickEdit: Code in over 40 languages across 7 platforms. SlickEdit’s unmatched power, speed, and flexibility allows even the most accomplished developers to write better code faster. Download a free trial today! |
|
#1
|
|||
|
|||
|
Other Language - PROLOG Problem
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
|
||||
|
||||
|
I've never really worked with Prolog before, so I may be off here (especially regarding where to put the cut), but from what I read here I think you need a rule that matches a one-element list between the empty list match and the match for a mist of more than one element:
Code:
display_list([H]) :- X = 0, increment(X,Y), write(Y),write(' '), write(H), nl, !
__________________
Rev First Speaker Schol-R-LEA;2 JAM LCF ELF KoR KCO BiWM TGIF #define KINSEY (rand() % 7) λ Scheme is the Red Pill Scheme in Short • Understanding the C/C++ Preprocessor Taming Python • A Highly Opinionated Review of Programming Languages for the Novice, v1.1 FOR SALE: One ShapeSystem 2300 CMD, extensively modified for human use. Includes s/w for anthro, transgender, sex-appeal enhance, & Gillian Anderson and Jason D. Poit clone forms. Some wear. $4500 obo. tverres@et.ins.gov |
|
#3
|
||||
|
||||
|
Unless there's a particular reason that you're limiting yourself to a single predicate then try this:
Code:
display_list(List) :- display_list(List,1).
display_list([Head|Tail],Count) :-
Number is Count + 1,
write(Count), write(' '), write(Head), nl,
display_list(Tail,Number).
Providing a simplified interface to a predicate like this is a very common idiom in Prolog. It also allows access to the raw predicate should you need it. The output is as expected. Code:
?- display_list(['North','South','East','West']). 1 North 2 South 3 East 4 West fail. ?- display_list(['North','South','East','West'], 0). 0 North 1 South 2 East 3 West fail. ?- I hope this helps, Mark. |
![]() |
| Viewing: Dev Shed Forums > Programming Languages - More > Other Programming Languages > Other Language - PROLOG Problem |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|