For Programmers: Free Programming Magazines  


Home > Archive > Prolog > December 2006 > Prolog and Logical definitions of plus/subtract









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Prolog and Logical definitions of plus/subtract
John Todd

2006-10-30, 7:13 pm

Hello,

I know that the logical definition of and plus and subtract in Prolog
is:
plus(0,N,N).
plus(s(M),N,s(Z)) :- plus(M,N,Z).

where s(X) is the successor of number X.

However, what would you do if you wanted to represent negative numbers?


Thanks,
John

Jussi Piitulainen

2006-10-30, 7:13 pm

John Todd writes:

> I know that the logical definition of and plus and subtract in
> Prolog is:
> plus(0,N,N).
> plus(s(M),N,s(Z)) :- plus(M,N,Z).
>
> where s(X) is the successor of number X.
>
> However, what would you do if you wanted to represent negative
> numbers?


Represent +3 as (3 - 0) and -3 as (0 - 3), with 3 and 0 as before.

Then (a - b) + (c - d) = (a + c) - (b + d), with right + as before.

Normalize by (s(X) - s(Y)) = (X - Y) till at least one of X, Y is 0.
This is optional, since (7 - 4) is also +3, and (4 - 7) is -3.
martial.ribault@gmail.com

2006-12-11, 7:10 pm


John Todd a =E9crit :

> Hello,
>
> I know that the logical definition of and plus and subtract in Prolog
> is:
> plus(0,N,N).
> plus(s(M),N,s(Z)) :- plus(M,N,Z).
>
> where s(X) is the successor of number X.
>
> However, what would you do if you wanted to represent negative numbers?
>
>
> Thanks,
> John


p(Y) is the predecessor of number Y.
p(s(X)) :- X.
s(p(Y)) :- Y.

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com