Home > Archive > Prolog > April 2004 > prolog assignment
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]
|
|
|
| how r ya guys??
well i nned somehelp here in some questions in prolog..
i have many question to implement but i didnt know how to do these
ones..i will be more than happy if u can help..
-------------------------------------------
==A==
Define a predicate last(X, L) that takes two arguments, an element X
and a set L. The predicate will succeed if X is the last element in L,
otherwise it will fail.
Test cases
last (X, [ics 313, ics 334, ics 202, ics 101]) X = ics 101.
last (ahmad, [ali, ahmad, said, mohammad]) no.
last (mohammad, [ali, ahmad, said, mohammad]) yes.
---------------------------------------------
==B==
Define a predicate delete(X, L, NL) that takes three arguments, an
element X and two sets L and NL. NL is constructed from the elements
of L by removing X if it is there.
Test cases
delete (35, [40, 35, 30, 20], NL) NL = [40, 30, 20].
delete (50, [40, 35, 30, 20], NL) NL = [40, 35, 30, 20].
-------------------------------
==C==
Define a predicate substitute (X, L, S, NL) that takes four arguments,
an element X, a set L, an element A, and another set NL. NL is
constructed from the elements of L except that X is substituted by M
in NL.
Test cases
substitute (apple, [banana, grape, apple], kiwi, NL) NL = [banana,
grape, kiwi].
substitute (apple, [banana, grape, apple], kiwi, NL) NL = [banana,
grape, kiwi].
--------------------
==D==
Define a predicate subset(S, L) that takes two lists S and L. The
predicate will succeed if S is a subset of L, otherwise it will fail.
Test cases
subset ([25, 30], [35, 25, 30, 50]) yes.
subset ([25, 30], [35, 25, 38, 50]) no.
----------------
==D==
Define a predicate union (L1, L2, UL) that takes three sets L1, L2 and
UL. The predicate will succeed if UL is the union of the two sets L1
and L2, otherwise it will fail.
Test cases
union ([25, 30], [35, 25, 30, 50], UL) UL = [35, 25, 30, 50].
union ([15, 20], [35, 25, 30], UL) UL = [15, 20, 35, 25, 30].
----------------
==E==
Define a predicate intersection (L1, L2, UL) that takes three sets L1,
L2 and UL. The predicate will succeed if UL is the intersection of the
two sets L1 and L2, otherwise it will fail.
Test cases
intersection ([25, 30], [35, 25, 30, 50], UL) UL = [25, 30].
intersection ([15, 20], [35, 25, 30], UL) UL = [ ].
intersection ([15, 20], [35, 25, 30], UL) UL = [ ].
==================
we r suppose to use SWI-Prolog Version 5
so please can u help me at least on few of them ...
thanks alot
| |
| Andrzej Lewandowski 2004-04-14, 9:32 pm |
| On 14 Apr 2004 17:34:18 -0700, dreams_mix@hotmail.com (m-flo) wrote:
>how r ya guys??
>
Write in English, please...
>well i nned somehelp here in some questions in prolog..
>i have many question to implement but i didnt know how to do these
>ones..i will be more than happy if u can help..
[..]
>so please can u help me at least on few of them ...
>
What sort of help are you expecting? That somebody will DO this
assignment for you? Sorry, this is not free homework service.
Think. Learn Prolog. Try to solve. Show what problems you have and
where. For sure, somebody will HELP you.
A.L.
| |
| Bill Spight 2004-04-15, 3:33 am |
| Dear m-flo,
> how r ya guys??
>
> well i nned somehelp here in some questions in prolog..
> i have many question to implement but i didnt know how to do these
> ones..i will be more than happy if u can help..
>
Your questions have to do with lists. What is a list in Prolog? It is
either the empty list,
[] ,
or a Head plus a Tail,
[Head | Tail].
The Head can be anything, but the Tail is a list.
To find out something about a list, you normally ask questions about the
empty list, the head of the list, and the tail. A common pattern
involves walking down the list to its end via recursion:
predicate([], ...) :- .... % If the list is empty.
predicate([Head | Tail], ...) :-
/* What about the Head? */
...,
predicate(Tail, ...).
Here is a hint about your first problem, about the last element in the
list.
last([], LastItem).
fails because the empty list has no last element, does it?
last([LastItem], LastItem).
succeeds because a list with one item has only a last element, and we
know that it is the one in question.
Take it from there.
Good luck!
Bill
| |
|
| hi
thx bill
for the ==A== question i have this logic in mind...
1- reverse the give list (call it List_1)<== this one i know how to do
it
2-take the head of the List_1 and compare it with X if they r equal<==
dont know how to do it
what do u think bill .. is this logic correct??
| |
| Bill Spight 2004-04-15, 7:37 am |
| Dear m-flo,
m-flo wrote:
>
> hi
>
> thx bill
>
> for the ==A== question i have this logic in mind...
> 1- reverse the give list (call it List_1)<== this one i know how to do
> it
> 2-take the head of the List_1 and compare it with X if they r equal<==
> dont know how to do it
>
> what do u think bill .. is this logic correct??
Problem A is easier than reversing a list. I think that you will learn
something by solving it on its own terms. :-)
Best,
Bill
| |
|
| yeah thx bill i have done it
lst(X,[X]).
lst(X,[_|Tail]):- lst(X,Tail).
thx for ur hint..
can u please give me hints for the rest??
and can i contact u with ur email? cuz google takes 3 houres to post a message
thanks
| |
| Bill Spight 2004-04-15, 8:33 am |
| Dear m-flo,
m-flo wrote:
>
> yeah thx bill i have done it
>
> lst(X,[X]).
> lst(X,[_|Tail]):- lst(X,Tail).
>
Good. :-)
> thx for ur hint..
>
> can u please give me hints for the rest??
Lists are used to represent sets. One thing about sets which is not
stated, but seems to be the case with the lists, is that each element of
a set is unique: no duplicates. You need to be careful of that when you
find the union of two sets.
<<
==B==
Define a predicate delete(X, L, NL) that takes three arguments, an
element X and two sets L and NL. NL is constructed from the elements
of L by removing X if it is there.
Test cases
delete (35, [40, 35, 30, 20], NL) NL = [40, 30, 20].
delete (50, [40, 35, 30, 20], NL) NL = [40, 35, 30, 20].
It often helps to solve simple examples first. As with the last element
problem, you may need the simplest example as the base clause in a
recursion. Even if that is not the case, solving simple examples may
give you the idea of how to solve the whole problem. What is the
simplest example for delete?
[color=darkred]
> and can i contact u with ur email? cuz google takes 3 houres to post a message
>
> thanks
I'm sorry, I have time to respond at most once a day or so. If you post
here someone else may help you before I could.
Best,
Bill
| |
|
| thx bill i have complate it ..
it took a while to know how stuff work..but with ur help i have done it..
thanks
| |
| Bill Spight 2004-04-17, 9:31 pm |
| Dear m-flo,
> thx bill i have complate it ..
Good going. :-)
Best,
Bill
|
|
|
|
|