For Programmers: Free Programming Magazines  


Home > Archive > Prolog > May 2004 > a little help regarding a simple problem !!!









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 a little help regarding a simple problem !!!
harry270768

2004-05-12, 9:20 pm

here is the problem :-

Wise William passed away and left the following will:

Everyone in my family, who holds a PhD should receive $100,000.
Everyone in my family, receives $3,000 for every aunt they have who holds
a PhD.
Everyone in my family, receives $2,000 for every uncle they have who
holds
a PhD.
Everyone in my family receives $100 for every sibling holding a
University
degree.
The rest of my fortune will go to a charity.



So wat i had done was defined say ,
uncle(Person,Uncle).
aunt(Person,Aunt).

aunt(john,linda).
aunt(john,lisa).

hold_degree(linda,phd).
hold_degree(lisa,phd).

so when i inquire aunt(john,X)?
X= linda
X=lisa

but now when i say
inheritance(Person,Amount):-
aunt(Person,Aunt),hold_degree(Aunt,phd)->Amount is 3000+What can
i
add here so that amount increments each time by 3000 for every aunt
;Amount is 0.


So now when i query inheritance(john,X)?
shud give me X=6000

Thanx guys

Bart Demoen

2004-05-12, 9:20 pm

harry270768 wrote:

> so when i inquire aunt(john,X)?
> X= linda
> X=lisa
>
> but now when i say
> inheritance(Person,Amount):-
> aunt(Person,Aunt),hold_degree(Aunt,phd)->Amount is 3000+What can
> i
> add here so that amount increments each time by 3000 for every aunt
> ;Amount is 0.
>
>
> So now when i query inheritance(john,X)?
> shud give me X=6000
>
> Thanx guys
>


Never try to "update" a (ground) term in Prolog.
What you can do is collect the "subinheritances" and afterwards
sum them. Like in

inherits(Person,Sum) :-
findall(PartInheritance,partial_inherits
(Person,PartInheritance),AllPartIs),
sum(AllPartIs,Sum).

(I think that's just in more detail what Bill suggested)

partial_inherits/2 would backtrack over all the reasons there are for giving Person
a part of the inheritance - I am not happy with the name of this predicate - I am
not a lawyer :-)

Cheers

Bart Demoen
Nick Wedd

2004-05-13, 6:31 am

In message
< 4140a0f9f691e1945573201125900625@localho
st.talkaboutprogramming.com>,
harry270768 <harnit_b@mail.com> writes
>here is the problem :-
>
>Wise William passed away and left the following will:
>
>Everyone in my family, who holds a PhD should receive $100,000.
>Everyone in my family, receives $3,000 for every aunt they have who holds
>a PhD.
>Everyone in my family, receives $2,000 for every uncle they have who
>holds
>a PhD.
>Everyone in my family receives $100 for every sibling holding a
>University
>degree.
>The rest of my fortune will go to a charity.
>
>
>
>So wat i had done was defined say ,
>uncle(Person,Uncle).

This clause says that anyone (and anything) is uncle to anyone (and
anything) else.

>aunt(Person,Aunt).
>
>aunt(john,linda).
>aunt(john,lisa).
>
>hold_degree(linda,phd).
>hold_degree(lisa,phd).
>
>so when i inquire aunt(john,X)?
>X= linda
>X=lisa
>
>but now when i say
>inheritance(Person,Amount):-
> aunt(Person,Aunt),hold_degree(Aunt,phd)->Amount is 3000+What can
>i
>add here so that amount increments each time by 3000 for every aunt
>;Amount is 0.
>
>
>So now when i query inheritance(john,X)?
>shud give me X=6000


You need to find how many solutions there are to
aunt(Person,Aunt), hold_degree(Aunt,phd)
for some Person. You can do this with findall, or bagof, or setof.

Nick
--
Nick Wedd nick@maproom.co.uk
Sponsored Links







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

Copyright 2008 codecomments.com