1
votes

I have predicates monthyear that returns the current month if month and year are not supplied below.

monthyear(M,Y) :-
    get_time(Stamp),
    stamp_date_time(Stamp, DateTime, local),
    date_time_value(month, DateTime, M),
    date_time_value(year, DateTime, Y).

If month and year are supplied return supplied month and year. To achieve this i have written the following code below.

cal(X,Y,Z1,Z2):-
    Z1 is X,
    Z2 is Y.

cal(X,Y,Z1,Z2):-
    X == null,
    Y == null,
    monthyear(Z1,Z2).

However i am sure this code can be improved and i am also faced with an error

ERROR: is/2: Arguments are not sufficiently instantiated

How can i go about making this work?.

X=1,Y=2,cal(X,Y,Z1,Z2).
returns
Z1 = X
Z2 = Y

and

cal(X,Y,Z1,Z2).
returns
Z1 = 1,
Z2 = 2016
1
== null? What do you mean with == null? - Willem Van Onsem
Furthermore please provide the query, and examples. It is a bit unclear to me what you want to achieve... - Willem Van Onsem
that means that if values of X and Y are not supplied by a parent predicate. - Krishna Kalyan
That's not how this is done in Prolog... Mind Prolog is a logic programming languages, that's quite different compared to an imperative one like Java. - Willem Van Onsem
thats where i need help. Having a hard time. I have edited the question. Hope it makes it clear - Krishna Kalyan

1 Answers

1
votes

You can use two versions of cal:

cal(X,Y,Z1,Z2):-
    Z1 is X,
    Z2 is Y.

cal(Z1,Z2):-
    monthyear(Z1,Z2).

If you call cal(2, 2015, Z1, Z2) you will get

Z1 = 2,
Z2 = 2015.

If you call cal(Z1, Z2) you will get (today)

Z1 = 1,
Z2 = 2016.