Pooja Gupta
Pooja Gupta

Reputation: 357

No output in prolog

I am new to Prolog and was using it to solve a cryptarithmetic problem CROSS+ROADS = DANGER .However when the code is run ,there is no output can anybody tell me what is wrong with the program? I will be very thankful.

Code:

:- use_module(library(clpfd)).

cr_puzzle([C,R,O,S,S] + [R,O,A,D,S] = [D,A,N,G,E,R]) :-
    Puzzle = [ C,R ,O ,S ,A ,D, N ,G, E],
    Puzzle ins 0..9,

    all_different(Puzzle),
    labeling([],Puzzle),

    C*10000+R*1000+O*100+S*10+S+
            R*10000+O*1000+A*100+D*10+S #=
            D*100000 + A*10000+N*1000+G*100+E*10+R,

    C #\=0,R #\=0.

I am using SWI-Prolog

Upvotes: 1

Views: 1109

Answers (2)

repeat
repeat

Reputation: 18726

The most likely reason for your program not producing any output when run is that it is not run.

$ swipl --traditional
Welcome to SWI-Prolog (Multi-threaded, 64 bits, Version 7.3.4)
Copyright (c) 1990-2015 University of Amsterdam, VU Amsterdam
SWI-Prolog comes with ABSOLUTELY NO WARRANTY. This is free software,
and you are welcome to redistribute it under certain conditions.
Please visit http://www.swi-prolog.org for details.

For help, use ?- help(Topic). or ?- apropos(Word).

?- compile(cr).
true.

?- cr_puzzle(P).
P = ([9, 6, 2, 3, 3]+[6, 2, 5, 1, 3]=[1, 5, 8, 7, 4, 6]) ;
false.

Make sure you get acquainted with the !

Also, consult the manual(s) of your Prolog processor for details.

Upvotes: 1

Andrey Tyukin
Andrey Tyukin

Reputation: 44957

If you are using it as a script that you run with swipl myScript.pl, then you have to specify the entry point to your script as follows:

:-initialization(myProgEntryPoint).

% define thousand other predicates

myProgEntryPoint :- write("Do stuff"), halt.

The important part is :-initialization(...). and the halt in the very end. Remove the halt if you want to enter the interactive interpreter after running the script.

By the way: you should fix your indentation, otherwise the code becomes unreadable really quickly.

Upvotes: 1

Related Questions