سحابة محبه
14-05-08, 01:57 AM
انا عندي برنامج بالبرولوج بس ماضبط يطبق معي بالبرنامج وماعرفت كيف استعلم عنه ياليت اللي عندهم خبـره يسااااااااعدوني ضرووووري
,,,,,,,
How to Write a Prolog Program
1-First define the problem
Problem: discard negative numbers from a given list
2- Second define the input an output of the problem
Problem input: given list
Problem output: list that contains the only positive
Numbers
3-declare the predicate name that describe the problem (declare the method that solve problem)
Predicate_ name (predicate arguments)
Predicate_ name = Discard_negative
Predicate_arguments= input/output
Input given list that contains numbers
And prolog denote it as [H|T]
Output unknown list that contains only positive
Numbers.
And prolog denote it as any unknown list
(Any Variable and give it any name =ProcessedTail)
4-define the predicate
(Discuss all available situations that your problem will be face= discuss all available inputs that the user will be given to your program)
a- first the user may be insert one empty list
The solution will be empty list.
Predicate_name (predicate_in, predicate_out)
Discared_negative ([ ], [ ])
b- second the user may be insert a list that contain numbers :
Discared_negative ([H|T], ProcessedTail):-
H<0,!, Discared_negative (T,ProcessedTail).
Here we check is the first element of list is
Negative if yes then the cut operator will be
Delete all backtracking points finds earlier in your
Program and your method must be call itself to
Process the reset of list elements.
But what about if the element list is non negative
Number??
Here the condition H<0 will fail
So this number is my first own solution and
Must be contained in my solution list
To solve this point
Discared_negative ([H|T], [H|ProcessedTail):-
Discared_negative (T, ProcessedTail).
To understand what done Trace this goal:
Discared_negative ([-1, 2,-3, 4], L)
Call:Discared_negative ([ ], [ ]).
Match is fail
Call:Discared_negative ([H|T], ProcessedTail)
Match succeed
H=-1, T= [2,-3, 4]
, ProcessedTail=L
Here H=-1<0 succeed
! Succeed and prevent backtracking (prevent to call the third Discared_negative)
And the Discared_negative call it self by
([2,-3, 4], L)
Here Discared_negative ([2,-3, 4], L)
Call:Discared_negative ([ ], [ ]).
Match is fail
Call:Discared_negative ([H|T], ProcessedTail)
Match succeed and H=2, T= [-3, 4]
, ProcessedTail=L
Here H=2<0 fail.
Call: Third definition of:
Discared_negative ([H|T], [H|ProcessedTail):-
Discared_negative (T, ProcessedTail).
Discared_negative ([2| [-3, 4]], L):-
And the Discared_negative call it self by
Discared_negative ([-3, 4], L).
Here Discared_negative ([-3, 4], L).
Call:Discared_negative ([ ], [ ]).
Match is fail
Call:Discared_negative ([H|T], ProcessedTail)
Match succeed and H=-3, T= [4]
, ProcessedTail=L
Here H=-3<0 succeed
! Succeed and prevent backtracking
(Prevent to call the third Discared_negative)
And the Discared_negative call it self by
([4], L)
Here Discared_negative ([4], L)
Call:Discared_negative ([ ], [ ]).
Match is fail
Call:Discared_negative ([H|T], ProcessedTail)
Match succeed and H=4, T= [ ]
, ProcessedTail=L
Here H=4<0 fail.
Call :
Discared_negative ([4| [ ]], L):-
And the Discared_negative call it self by
Discared_negative ([ ], L).
Here Discared_negative ([ ], L).
Call:Discared_negative ([ ], [ ]).
Match is succeeding then the L= []
So the sub goal is succeed so the head of the goal will be succeed
Discared_negative ([H|T], [H|ProcessedTail):-
Discared_negative (T, ProcessedTail).
Discared_negative ([4| [ ]], [4| [ ]]):-
Discared_negative ([ ], [ ]).
But note that the second discared_negative
Discared_negative ([H|T], ProcessedTail):-
H<0,!, Discared_negative (T, ProcessedTail).
is called inside the third one for [-3, 4] and hence the head of this list is 2 will add to ProcessedTail by result of
The ProcessedTail= [2, 4]
And by logic the second discared_negative is called inside the third one for [ -1,2,-3,4] and the head of this list is unknown so the prolog denote -1 is the first element of the list so this call is not done and the prolog will report you by result [2,4].
,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,, ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
وهـــــــــــــــــــــــــذا بعـــــــــــــــــــــد ماعرفت لـــــــــــــــــــــــــــــه
Dynamic programming
The following Prolog program uses dynamic programming (http://en.wikipedia.org/wiki/Dynamic_programming) to find the longest common subsequence (http://en.wikipedia.org/wiki/Longest_common_subsequence) of two lists in polynomial time. The clause database is used for memoization (http://en.wikipedia.org/wiki/Memoization):
:- dynamic(stored/1).memo(Goal) :- ( stored(Goal) -> true ; Goal, assertz(stored(Goal)) ).lcs([], _, []) :- !.lcs(_, [], []) :- !.lcs([X|Xs], [X|Ys], [X|Ls]) :- !, memo(lcs(Xs, Ys, Ls)).lcs([X|Xs], [Y|Ys], Ls) :- memo(lcs([X|Xs], Ys, Ls1)), memo(lcs(Xs, [Y|Ys], Ls2)), length(Ls1, L1), length(Ls2, L2), ( L1 >= L2 -> Ls = Ls1 ; Ls = Ls2 ).
Example query:
?- lcs([x,m,j,y,a,u,z], [m,z,j,a,w,x,u], Ls).Ls = [m, j, a, u]
,,,,,,,
How to Write a Prolog Program
1-First define the problem
Problem: discard negative numbers from a given list
2- Second define the input an output of the problem
Problem input: given list
Problem output: list that contains the only positive
Numbers
3-declare the predicate name that describe the problem (declare the method that solve problem)
Predicate_ name (predicate arguments)
Predicate_ name = Discard_negative
Predicate_arguments= input/output
Input given list that contains numbers
And prolog denote it as [H|T]
Output unknown list that contains only positive
Numbers.
And prolog denote it as any unknown list
(Any Variable and give it any name =ProcessedTail)
4-define the predicate
(Discuss all available situations that your problem will be face= discuss all available inputs that the user will be given to your program)
a- first the user may be insert one empty list
The solution will be empty list.
Predicate_name (predicate_in, predicate_out)
Discared_negative ([ ], [ ])
b- second the user may be insert a list that contain numbers :
Discared_negative ([H|T], ProcessedTail):-
H<0,!, Discared_negative (T,ProcessedTail).
Here we check is the first element of list is
Negative if yes then the cut operator will be
Delete all backtracking points finds earlier in your
Program and your method must be call itself to
Process the reset of list elements.
But what about if the element list is non negative
Number??
Here the condition H<0 will fail
So this number is my first own solution and
Must be contained in my solution list
To solve this point
Discared_negative ([H|T], [H|ProcessedTail):-
Discared_negative (T, ProcessedTail).
To understand what done Trace this goal:
Discared_negative ([-1, 2,-3, 4], L)
Call:Discared_negative ([ ], [ ]).
Match is fail
Call:Discared_negative ([H|T], ProcessedTail)
Match succeed
H=-1, T= [2,-3, 4]
, ProcessedTail=L
Here H=-1<0 succeed
! Succeed and prevent backtracking (prevent to call the third Discared_negative)
And the Discared_negative call it self by
([2,-3, 4], L)
Here Discared_negative ([2,-3, 4], L)
Call:Discared_negative ([ ], [ ]).
Match is fail
Call:Discared_negative ([H|T], ProcessedTail)
Match succeed and H=2, T= [-3, 4]
, ProcessedTail=L
Here H=2<0 fail.
Call: Third definition of:
Discared_negative ([H|T], [H|ProcessedTail):-
Discared_negative (T, ProcessedTail).
Discared_negative ([2| [-3, 4]], L):-
And the Discared_negative call it self by
Discared_negative ([-3, 4], L).
Here Discared_negative ([-3, 4], L).
Call:Discared_negative ([ ], [ ]).
Match is fail
Call:Discared_negative ([H|T], ProcessedTail)
Match succeed and H=-3, T= [4]
, ProcessedTail=L
Here H=-3<0 succeed
! Succeed and prevent backtracking
(Prevent to call the third Discared_negative)
And the Discared_negative call it self by
([4], L)
Here Discared_negative ([4], L)
Call:Discared_negative ([ ], [ ]).
Match is fail
Call:Discared_negative ([H|T], ProcessedTail)
Match succeed and H=4, T= [ ]
, ProcessedTail=L
Here H=4<0 fail.
Call :
Discared_negative ([4| [ ]], L):-
And the Discared_negative call it self by
Discared_negative ([ ], L).
Here Discared_negative ([ ], L).
Call:Discared_negative ([ ], [ ]).
Match is succeeding then the L= []
So the sub goal is succeed so the head of the goal will be succeed
Discared_negative ([H|T], [H|ProcessedTail):-
Discared_negative (T, ProcessedTail).
Discared_negative ([4| [ ]], [4| [ ]]):-
Discared_negative ([ ], [ ]).
But note that the second discared_negative
Discared_negative ([H|T], ProcessedTail):-
H<0,!, Discared_negative (T, ProcessedTail).
is called inside the third one for [-3, 4] and hence the head of this list is 2 will add to ProcessedTail by result of
The ProcessedTail= [2, 4]
And by logic the second discared_negative is called inside the third one for [ -1,2,-3,4] and the head of this list is unknown so the prolog denote -1 is the first element of the list so this call is not done and the prolog will report you by result [2,4].
,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,, ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
وهـــــــــــــــــــــــــذا بعـــــــــــــــــــــد ماعرفت لـــــــــــــــــــــــــــــه
Dynamic programming
The following Prolog program uses dynamic programming (http://en.wikipedia.org/wiki/Dynamic_programming) to find the longest common subsequence (http://en.wikipedia.org/wiki/Longest_common_subsequence) of two lists in polynomial time. The clause database is used for memoization (http://en.wikipedia.org/wiki/Memoization):
:- dynamic(stored/1).memo(Goal) :- ( stored(Goal) -> true ; Goal, assertz(stored(Goal)) ).lcs([], _, []) :- !.lcs(_, [], []) :- !.lcs([X|Xs], [X|Ys], [X|Ls]) :- !, memo(lcs(Xs, Ys, Ls)).lcs([X|Xs], [Y|Ys], Ls) :- memo(lcs([X|Xs], Ys, Ls1)), memo(lcs(Xs, [Y|Ys], Ls2)), length(Ls1, L1), length(Ls2, L2), ( L1 >= L2 -> Ls = Ls1 ; Ls = Ls2 ).
Example query:
?- lcs([x,m,j,y,a,u,z], [m,z,j,a,w,x,u], Ls).Ls = [m, j, a, u]