【问题标题】:How to bind an unbound value in a List in Oz如何在 Oz 的列表中绑定未绑定的值
【发布时间】:2015-04-06 13:23:50
【问题描述】:

假设我们有以下代码:

local L in
    L = {List.make 10 $}
    % Bind nth element to something here
end

如何设置这些未绑定的值? Oz List Documentation 对此没有任何解释。我发现的唯一相关问题是:How do you change an element in a list in Oz? 答案没有为我编译,我也没有找到如何编译它。

【问题讨论】:

    标签: oz mozart


    【解决方案1】:

    我实际上可以使用Oz List Documentation 中的一些东西来让它工作!

    declare Temp Index Value L in
    L = {List.make 10 $}
    
    Index = %Index to be bound
    Value = %Value for bound
    
    Temp = {List.mapInd L 
       fun {$ I B}
          if I == Index then
             B = Value 
          else
             B
          end
       end
    $}
    

    【讨论】:

    • 或者,你也可以只做{Nth L Index} = Value
    • @wmeyer 什么鬼! :p 我试过了,但失败了,这就是为什么我最终选择了这个疯狂的实现。一定是在代码的其他地方犯了错误。谢谢你的提示:D!
    【解决方案2】:

    wmeyer 是对的,完整的实现可以是例如:

    declare
    local L I V in
       L = {List.make 10}
       I=5 %index to be bound
       V=1 %value for bound
       {List.nth L I}=V
       {Browse L} 
    end
    

    你会得到正确的输出[_ _ _ _ 1 _ _ _ _ _]。您也可以直接访问 en 元素,但这不是编程解决方案,因为您可能知道每个列表都是一个 cons (head|tail),因此您可以使用 L.1 访问第一个元素,然后使用 L.2.1 访问第二个元素,依此类推.. 最后一件事,您无需将“$”放在第二行,因为您已经将 List.make 的结果分配给 L。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-17
      • 1970-01-01
      • 2020-05-09
      • 1970-01-01
      • 2016-09-12
      相关资源
      最近更新 更多