2016-09-20 6 views
1

私はMozart OZでクイックソートを実装しようとしていますが、変数を導入しないとエラーが発生します。私はこの言語で新しいです。私を助けてください。mozartで変数パーティションが導入されていませんOz

declare 
fun {QuickSort L} 
    case L 
    of X|L2 then Left Right SL SR in 
     {Partition L2 X Left Right} 
     SL={QuickSort Left} 
     SR={QuickSort Right} 
     {Append SL X|SR} 
    [] nil then nil 
end 
end 
{Browse {QuickSort[4 7 66 545 1 65 22 322]}} 
+0

導入されていない変数の名前は何ですか? – beroal

答えて

0
proc {Partition Xs Pivot Left Right} 
    case Xs 
    of X|Xr then 
     if X < Pivot 
     then Ln in 
     Left = X | Ln 
     {Partition Xr Pivot Ln Right} 
     else Rn in 
     Right = X | Rn 
     {Partition Xr Pivot Left Rn} 
     end 
    [] nil then Left=nil Right=nil 
    end 
end 


fun lazy {LQuickSort Xs} 
    case Xs of X|Xr then Left Right SortedLeft SortedRight in 
     {Partition Xr X Left Right} 
     {LAppend {LQuickSort Left} X|{LQuickSort Right}} 
    [] nil then nil 
    end 
end 
0

これは私の割り当てのいずれかです。ここ ソリューションです:

declare 
fun {LAppend Xs Ys} 
    case Xs of X|Xr then 
     X|{LAppend Xr Ys} 
    [] nil then Ys 
    end 
end 

declare 
proc {Partition L2 X L R} 
    case L2 
    of Y|M2 then 
     if Y<X then Ln in 
    L=Y|Ln 
    {Partition M2 X Ln R} 
     else Rn in 
    R=Y|Rn 
    {Partition M2 X L Rn} 
     end 
    [] nil then L=nil R=nil 
    end 
end 

declare 
fun {LQuicksort L} 
    case L of X|L2 then Left Right SL SR in 
     {Partition L2 X Left Right} 
     SL={LQuicksort Left} 
     SR={LQuicksort Right} 
     {LAppend SL X|SR} 
    [] nil then nil 
    end 
end 

declare 
Xs={LQuicksort [3 1 4 1 5 9 2 6 5]} 
{Browse Xs} 
関連する問題