[m-users.] How do you go about logging?

Oleg Sivokon olegsivokon at gmail.com
Sun Apr 6 04:51:27 AEST 2014


Hi list!

I'm trying to learn the language. I have some very limited and rusty
knowledge of Prolog and a little bit of Haskell, but all in all I'm
new to logic programming.

I'm trying to do Euler Project exercises in order to learn the
language. And here's something I've come across and I don't know how to
proceed:



% -*- mode: Prolog -*-

:- module e3.
:- interface.
:- import_module io.
:- pred main(io::di, io::uo) is det.

:- implementation.
:- import_module int, list, string, math, float.

:- pred nonedivides(int::in, list(int)::in, io::di, io::uo) is semidet.
nonedivides(_, [], !IO).
nonedivides(Test, [Factor | Factors], !IO):-
    io.format("Factor: %d\n", [i(Factor)], !IO),
    Factor mod Test \= 0, nonedivides(Test, Factors, !IO).

% :- pred prime_factors(int::in, int::in, int::in, list(int)::out) is det.
% prime_factors(Sqrt, Sqrt, Number, Factors):- Factors.
% prime_factors(Test, Sqrt, Number, Factors):-
%     (if Number mod Test = 0
%     then (if nonedivides(Test, Factors)
%          then prime_factors(Test + 2, Sqrt, Number, [Test | Factors])
%          else prime_factors(Test + 2, Sqrt, Number, Factors))
%     else prime_factors(Test + 2, Sqrt, Number, Factors)).

% :- func laregest_factor(int) = int.
% laregest_factor(Number) =
%     ([Head | Tail] = sort(prime_factors(1, floor_to_int(sqrt(float(Number)))),
%                           Number, []), Head).

% main(!IO):- io.format("Answer: %d\n", [i(laregest_factor(600851475143))], !IO).

main(!IO):-
    (if nonedivides(42, [4, 5, 8, 3], !IO)
    then io.format("Answer: %d\n", [i(0)], !IO)
    else io.format("Answer: %d\n", [i(1)], !IO)).


The commented code should show the intended complete solution, the
uncommented parts should show the particular problem I'm struggling
with.
I'm trying to log the values of the arguments while at every iteration
of the predicate in order to understand what went wrong, but I can't
because IO arguments aren't allowed in a semi-deterministic
predicate... so, how do I print something from a predicate like that?

Best,

Oleg



More information about the users mailing list