/* CLP(R) EXAMPLES FOR 341 LECTURE */ /* This file is on ~borning/clpr/myprogs/lecture on orcas */ /* CENTIGRADE-FAHRENHEIT TEMPERATURE CONVERSION */ cf(C,F) :- F-32.0 = 1.8*C. /* the same program in Scheme: (define (c-to-f c) (+ (* 1.8 c) 32.0)) (define (f-to-c f) (/ (- f 32.0) 1.8)) */ /* FIND THE MAX OF TWO NUMBERS */ max(X,Y,X) :- X>=Y. max(X,Y,Y) :- X= x y) x y)) in Miranda: max x y = x, if x>=y = y, if x0, length(Xs,N-1). /* Miranda version of length: length [] = 0 length (a:as) = 1 + length as */ /* QUICKSORT */ quicksort([],[]). quicksort([X|Xs],Sorted) :- partition(X,Xs,Smalls,Bigs), quicksort(Smalls,SortedSmalls), quicksort(Bigs,SortedBigs), append(SortedSmalls,[X|SortedBigs],Sorted). partition(Pivot,[],[],[]). partition(Pivot,[X|Xs],[X|Ys],Zs) :- X <= Pivot, partition(Pivot,Xs,Ys,Zs). partition(Pivot,[X|Xs],Ys,[X|Zs]) :- X > Pivot, partition(Pivot,Xs,Ys,Zs).