PROLOG Help

Discussion in 'C' started by metahuman, Apr 9, 2008.

  1. metahuman

    metahuman New Member

    Joined:
    Feb 8, 2008
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    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(T).

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

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice