Lesson Four revisited paradigm (2)
4.2 Logic Paradigm--when the algorithm loses control
Tao often does nothing but all of them--"Laozi Dau Sutra"
Keywords: programming paradigm, logical programming, Prolog, algorithm, logic, control
Summary: A second discussion on logical programming
? Questions
is the measurement of software complexity determined by the length of the code?
Why is a logical encoding generally simpler than a procedural style?
What are the advantages and disadvantages of logical programming compared to imperative programming?
: Explain
The question mark puts forward: "Is logic programming not also very special?" I don't seem to be introducing much in front of you. ”
"Then we'll implement a quicksort with the logical language Prolog." "The colon says to flip the slide--
/*快速排序法的Prolog实现*/
/* 定义划分法 */
partition(_,[],[],[]). /* 划分递归终点 */
partition(Pivot,[X|Rest],[X|Small],Big) :-
X < Pivot, partition(Pivot,Rest,Small,Big). /* 比基准小的归入Small */
partition(Pivot,[X|Rest],Small,[X|Big]) :-
X >= Pivot, partition(Pivot,Rest,Small,Big). /* 比基准大的归入Big */
/* 定义排序法 */
qsort([],[]). /* 排序递归终点 */
qsort([Pivot|Rest],Sorted) :-
partition(Pivot,Rest,Small,Big), /* 按基准划分子列 */
qsort(Small,SortedSmall), /* 对前面的子列递归 */
qsort(Big,SortedBig), /* 对后面的子列递归 */
append(SortedSmall,[Pivot|SortedBig],Sorted)./* 子列合并 */
Comma scratching head: "See not quite understand oh, fortunately I remember your sentence: tolerance ignorance." I've endured! ”
Everybody's happy.