0
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?

W_K
  • 33
  • 3
  • 4
  • 8

1 Answers1

2

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

Thanos Tintinidis
  • 5,828
  • 1
  • 20
  • 31
  • @W_K you're welcome; btw you can also use `writef/2` [http://www.swi-prolog.org/pldoc/doc_for?object=writef/2] for io: `writef("%w ", [Head])` – Thanos Tintinidis Nov 18 '12 at 16:44
  • @thanosQR: format/2 is the more common name for this. Do you see a reason, why there is `writef/2` at all? – false Nov 18 '12 at 23:10