Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
List 用Oz编程语言显示列表中的两个最大数字_List_Max_Oz - Fatal编程技术网

List 用Oz编程语言显示列表中的两个最大数字

List 用Oz编程语言显示列表中的两个最大数字,list,max,oz,List,Max,Oz,我需要制作一个程序,使用Oz编程语言显示列表中的两个最大数字。我有打印列表中最大数字的代码,但我需要两个最大的数字。以下是我到目前为止的情况: fun {Max L1} case L1 of nil then 0 [] X|Xr then if X>{Max Xr} then X else {Max Xr} end end end {Browse {Max [1 2 3 4 5 6 7 8 9]}} 这将显示列表中最大的数字,但只有一个,我需要显示两个最

我需要制作一个程序,使用Oz编程语言显示列表中的两个最大数字。我有打印列表中最大数字的代码,但我需要两个最大的数字。以下是我到目前为止的情况:

fun {Max L1}
 case L1
 of nil then 0
 [] X|Xr then
    if X>{Max Xr} then X
     else {Max Xr}
    end
 end
end
{Browse {Max [1 2 3 4 5 6 7 8 9]}}

这将显示列表中最大的数字,但只有一个,我需要显示两个最大的数字。我需要做什么?

在莫扎特的《绿野仙踪》中有很多方法可以解决这个问题。这是一个可能的解决方案,扩展到大小(长度)N的列表;但是,仅适用于可能数

proc {LengL Ls N R}
    case Ls
    of nil then N = R
    [] L|Lr then {LengL Lr N+1 R}
    end
end
proc {Drop1 L Y R}
    R=case L
    of nil then nil
    [] X|Xr then
        if X\=Y then X|{Drop1 Xr Y}
        else {Drop1 Xr Y}
        end
    end
end
proc {MaxN L1 N R1}
    proc {Max1 L R}
        R=case L
          of nil then 0
          [] X|Xr then
              if X>{Max1 Xr} then X
              else {Max1 Xr}
              end
          end
    end
    R2={Max1 L1} Lg={LengL L1 0} in
    R1={proc {$ R}
            R=if N==0 then nil
              elseif N>Lg then R2|{MaxN {Drop1 L1 R2} Lg-1}
              else R2|{MaxN {Drop1 L1 R2} N-1}
              end
       end}
end
{Browse {MaxN [~11 2 3 4 15 6 7 8 9] 12}}
请注意MaxN的行为,当N大于数字列表L1的长度时,浏览器将显示长度为L1的列表,其中前N个数字最大。负数被零代替。我希望这仍然有用