Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

please help me
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


Report this thread to moderator Post Follow-up to this message
Old Post
imamabudaud@gmail.com
04-06-05 05:44 PM


Re: please help me
Could you post your implementation of the first one?


Report this thread to moderator Post Follow-up to this message
Old Post
tmp123
04-06-05 05:44 PM


Re: please help me
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

Report this thread to moderator Post Follow-up to this message
Old Post
Bill Spight
04-06-05 05:44 PM


Re: please help me
> 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



Report this thread to moderator Post Follow-up to this message
Old Post
Georgios Tagalakis
04-06-05 05:44 PM


Re: please help me
/*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).

Report this thread to moderator Post Follow-up to this message
Old Post
imamabudaud
04-06-05 05:44 PM


Re: please help me
"tmp123" <tmp123@menta.net> wrote in message news:<1112454239.585928.89250@z14g2000cwz.goog
legroups.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).

Report this thread to moderator Post Follow-up to this message
Old Post
imamabudaud
04-06-05 05:44 PM


Re: please help me
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.


Report this thread to moderator Post Follow-up to this message
Old Post
tmp123
04-06-05 05:44 PM


Re: please help me
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

Report this thread to moderator Post Follow-up to this message
Old Post
imamabudaud
04-06-05 05:44 PM


Re: please help me
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?


Report this thread to moderator Post Follow-up to this message
Old Post
tmp123
04-06-05 05:44 PM


Re: please help me
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

Report this thread to moderator Post Follow-up to this message
Old Post
student
04-06-05 05:44 PM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

Prolog archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 06:56 PM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.