Home > Archive > Prolog > April 2005 > please help me
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]
|
|
| imamabudaud@gmail.com 2005-04-06, 12:44 pm |
| I am beginner on PROLOG..
I know about LISP, and C, but know nothing about PROLOG
I wanna make a program to output median of several datas.
eg.
median([1,2,3,4,5],Out)
Out=3
median)([1,2,3,4],Out)
Out=2.5
I have made the first one, but the second are too difficult..
please
| |
| tmp123 2005-04-06, 12:44 pm |
| Could you post your implementation of the first one?
| |
| Bill Spight 2005-04-06, 12:44 pm |
| Dear imamabudaud,
> I wanna make a program to output median of several datas.
>
> eg.
>
> median([1,2,3,4,5],Out)
> Out=3
>
> median)([1,2,3,4],Out)
> Out=2.5
>
>
> I have made the first one, but the second are too difficult..
>
Please show us your program for the first one. You can probably modify
it to handle the second one.
Thanks,
Bill
| |
| Georgios Tagalakis 2005-04-06, 12:44 pm |
| > I wanna make a program to output median of several datas.
Search this NG for a thread titled "Find median from a number list"
--- Georgios Tagalakis
| |
| imamabudaud 2005-04-06, 12:44 pm |
| /*here is my first one,
the func is MID(list,real)*/
domains
list=symbol*
predicates
reverse(list,list)
reverses(list,list,list)
mid(list,real)
clauses
reverse(Masukan,Keluaran):-
reverses(Masukan,[],Keluaran).
reverses([],Out,Out).
reverses([H|B],Tm,Keluaran):-
reverses(B,[H|Tm],Keluaran).
mid([H|[]],Out).
mid([_|Ekor],Out):-
reverse(Ekor,[_|Temp]),
reverse(Temp,Badan),
mid(Badan,Out).
| |
| imamabudaud 2005-04-06, 12:44 pm |
| "tmp123" <tmp123@menta.net> wrote in message news:<1112454239.585928.89250@z14g2000cwz.googlegroups.com>...
> Could you post your implementation of the first one?
/*here..*/
domains
list=symbol*
predicates
reverse(list,list)
reverses(list,list,list)
mid(list,real)
clauses
reverse(Masukan,Keluaran):-
reverses(Masukan,[],Keluaran).
reverses([],Out,Out).
reverses([H|B],Tm,Keluaran):-
reverses(B,[H|Tm],Keluaran).
mid([H|[]],Out).
mid([_|Ekor],Out):-
reverse(Ekor,[_|Temp]),
reverse(Temp,Badan),
mid(Badan,Out).
| |
| tmp123 2005-04-06, 12:44 pm |
| imamabudaud wrote:
> /*here is my first one,
> the func is MID(list,real)*/
>
> domains
> list=symbol*
> predicates
> reverse(list,list)
> reverses(list,list,list)
> mid(list,real)
>
> clauses
> reverse(Masukan,Keluaran):-
> reverses(Masukan,[],Keluaran).
> reverses([],Out,Out).
> reverses([H|B],Tm,Keluaran):-
> reverses(B,[H|Tm],Keluaran).
>
> mid([H|[]],Out).
> mid([_|Ekor],Out):-
> reverse(Ekor,[_|Temp]),
> reverse(Temp,Badan),
> mid(Badan,Out).
Hi,
comment1: from your algorithm, it seems you take "median" as "the term
in the middle", not as the "mathematical statistical median". Or you
assume that always the input list will be sorted.
comment2: mid([H|[]],Out) could contain an editorial mistake and can be
simplified: mid([H],H).
comment3: take care with backtraking. mid([H|[]],Out) and
mid([_|Ekor],Out) are both valid for a call like mid([1],R). (if you
think about the addition of a "cut", well, I will not say no).
Now, the subject of your question:
You have correctly implemented the basic idea of a recursive rule as
"mid": a rule for the end of recursion, and a recursive rule. The first
one covers the case for a list of 1 element. The second, the case of
list with 2 (warning here) or more elements.
The problem is that if a 2 elements list is passed to the recursive
rule, it will call with a cero elements list, a case not covered and
without solution. Sugestion: write also a rule for the specific case of
input with 2 elements: mid([A,B],...) :- ... .
Note: If you want to improve the general method (it expends too much in
reverse twice the list), I suggest you review an old post about how to
split one list in two. Google for it.
| |
| imamabudaud 2005-04-06, 12:44 pm |
| Thats very helpfull,
thank you dude..!
btw, I have lots of gmail invitation..
mail me if you wanna one..!
mail to : imamabudaud@gmail.com
thank a lot
| |
| tmp123 2005-04-06, 12:44 pm |
|
imamabudaud wrote:
> Thats very helpfull,
> thank you dude..!
>
> btw, I have lots of gmail invitation..
> mail me if you wanna one..!
>
> mail to : imamabudaud@gmail.com
>
> thank a lot
I'm glad you find it useful.
Something I forgot: "mid" of one list is the same than "mid" of the
reverse list. So, why reverse it twice?
| |
| student 2005-04-06, 12:44 pm |
| imamabudaud@gmail.com wrote:
> I am beginner on PROLOG..
>
> I know about LISP, and C, but I know nothing about PROLOG.
>
> I want to make a program to output the median of a set of data.
>
> For example, I want
>
> median([1,2,3,4,5],Out)
> Out=3
>
> but
>
> median([1,2,3,4],Out)
> Out=2.5
>
>
> My method seems to work with lists that have an odd number of terms,
> but fails on lists that have an even number of terms.
>
[Method not shown.]
A Prolog predicate is a definition,
so writing Prolog predicates is a matter
of defining your terms.
Suppose I define the median of a well-ordered list of ground terms
to be the center term of the list if the list is odd-sized and
the two center terms of the list, in their same relative order,
if the list is even-sized.
Examples:
1 2 2 2 (2) 3 4 55 666
1 2 2 2 (2 3) 3 4 55 666
Note that in both cases the number of terms
that come before the median is equal to the number
of terms that come after it.
Note also that it makes no sense to speak of the median
of a list unless the entire list is present and it is in either
ascending or descending order.
Thus,
get_median(L,Z) :- /* mode(i,o) */
sort(L,SL),
is_median(SL,Z).
is_median(SL,list_was_odd_with_center_te
rm(Z)) :-
append(Smaller,[Z|Bigger],SL), ... etc.
is_median(SL,list_was_even_with_center_t
erms(Z1,Z2)) :-
append(Smaller,[Z1,Z2|Bigger],SL), ... etc.
whence
?- get_median([1,2,2,2,2,3,4,55,666],X).
X = list_was_odd_with_center_term(2) ;
?- get_median([1,2,2,2,2,3,3,4,55,666],X).
X = list_was_even_with_center_terms(2, 3) ;
--
billh
|
|
|
|
|