For Programmers: Free Programming Magazines  


Home > Archive > Prolog > November 2005 > Integer_division/3....stuck on the recursion part.









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 Integer_division/3....stuck on the recursion part.
sikkwanchu@yahoo.co.uk

2005-11-27, 7:00 pm

integer_division(_X,0,error).

integer_division(0,_,_).

integer_division(X,Y,Z):-
X >= Y,
N is X - Y,
integer_division(N,Y,A),
Z = A + 1.

im stuck in where the recurison ends. it doesnt knw what A is and i
cant inital... and prolog's recursion are weird..i never understand
it... could any experts give me a hands plz?

Pierpaolo BERNARDI

2005-11-27, 9:56 pm


sikkwanchu@yahoo.co.uk wrote:
> integer_division(_X,0,error).
>
> integer_division(0,_,_).


?

> integer_division(X,Y,Z):-
> X >= Y,
> N is X - Y,
> integer_division(N,Y,A),
> Z = A + 1.
>
> im stuck in where the recurison ends.


What's the result when X < Y ?

Just add a clause which covers this case.

P.

NEFuchs

2005-11-28, 3:57 am

Pierpaolo BERNARDI wrote:
> sikkwanchu@yahoo.co.uk wrote:
>
> ?
>
>
> What's the result when X < Y ?
>
> Just add a clause which covers this case.
>
> P.


Two further hints.

1. The clause "integer_division(0,_,_)" constitutes the base case of
your recursion. What should be the result?

2. "Z = A + 1" unifies Z with A + 1, but does not assign the value of
A + 1 to Z. Use the predicate is/2 for this purpose.

sikkwanchu@yahoo.co.uk

2005-11-28, 7:01 pm


NEFuchs wrote:
> Pierpaolo BERNARDI wrote:
>
> Two further hints.
>
> 1. The clause "integer_division(0,_,_)" constitutes the base case of
> your recursion. What should be the result?
>
> 2. "Z = A + 1" unifies Z with A + 1, but does not assign the value of
> A + 1 to Z. Use the predicate is/2 for this purpose.


i got the second part.. but i dont get the first part still...since
when X is 0 then i return X,Y and Z as 0,_,Z or 0,_,_ or nothing even?

thx for the help

sikkwanchu@yahoo.co.uk

2005-11-28, 7:01 pm

integer_division(_X,0,error).

integer_division(X,Y,Z):-
X < Y,
Z is 0.

integer_division(0,Y,_):-
integer_division(0,Y,0).

integer_division(X,Y,Z):-
X >= Y,
N is X - Y,
integer_division(N,Y,A),
Z is A + 1.

I can get the answer now.. but it came up another problem which it
tries to backtrack and give me another answer.. but the thing is. that
is only one answer existed...

what should i do stop backtacking? should i use cuts? i dont think i
need cuts in this exercise tho.

sikkwanchu@yahoo.co.uk

2005-11-28, 7:01 pm

hmmm.... put a cut right after X < Y, to prevent backtracking but is
there a way to do it without using a cut?

Geoffrey Summerhayes

2005-11-28, 7:01 pm

Do you really need the third clause? :-)
--
Geoff

Sponsored Links







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

Copyright 2008 codecomments.com