Search code examples
prologwin-prolog

How to get both drive predicates working in prolog?


begin:- 

go,
initialise.             % drive predicate

begin:-
write('Sorry cannot help'),nl,nl.



go:-
write('Below is the list of the current toys available within the 

store.'),nl,nl,nl,nl,
loop_through_list([car, football, action_men, train_tracks, lego, football, bikes, control_car, drees_up, doll, bear, furbies, craft, doll_house]).
loop_through_list([Head|Tail]) :-
    write(Head),
    write(' '),
    loop_through_list(Tail).
initialise:-
nl,nl,nl,nl,
tab(40),write('******************************************'),nl,
tab(40),write('*** TOY GENERATING SYSTEM ***'),nl,
tab(40),write('******************************************'),nl,nl,
write('Please answer the following questions'),
write(' y (yes) or n (no).'),nl,nl, nl, nl.

The problem here is that the go:- and the initialise:- work separately when on their own, but not when put together. Is there a problem with all the nls?


Solution

  • The problem is that go/0 doesn't work corrently. While it prints the list, it fails at the end, meaning that the execution will stop afterwards. Therefore, when you run begin/0, initialize/0 will never run.

    To fix it you need to add a base case for loop_through_list/0:

    loop_through_list([]).
    loop_through_list([Head|Tail]) :-
        write(Head),
        write(' '),
        loop_through_list(Tail).
    

    As a side note, "print_list" would be a more descriptive name for loop_through_list/0