netlogo函数可以接受可变长度的参数吗?

问题描述

我有一些重复多次的代码,我试图制作一个函数,以便只用args调用函数,但是我想知道netlogo中的同一函数是否接受可变数量的args吗?>

例如:

to set-attributes [ x-occupiedepitopes x-size x-exempt_from_immobilisation x-aggregated ]
  set heading random 360
  set occupiedepitopes x-occupiedepitopes
  set size x-size
  set leader self
  set exempt_from_immobilisation x-exempt_from_immobilisation
  set aggregated x-aggregated
end

to set-attributes2 [ x-occupiedepitopes x-size x-exempt_from_immobilisation x-aggregated x-color ]
  set heading random 360
  set occupiedepitopes x-occupiedepitopes
  set size x-size
  set leader self
  set exempt_from_immobilisation x-exempt_from_immobilisation
  set aggregated x-aggregated
  set color x-color
end

除了一个额外的arg #x-color

之外,以上两个函数是相同的

谢谢

解决方法

NetLogo不支持可变参数号(至少在用户过程中不支持),也不支持overloading

您仍然可以通过执行以下操作来减少代码的重复性:

to set-attributes [ x-occupiedepitopes x-size x-exempt_from_immobilisation x-aggregated ]
  set heading random 360
  set occupiedepitopes x-occupiedepitopes
  set size x-size
  set leader self
  set exempt_from_immobilisation x-exempt_from_immobilisation
  set aggregated x-aggregated
end

to set-attributes2 [ x-occupiedepitopes x-size x-exempt_from_immobilisation x-aggregated x-color ]
  set-attributes x-occupiedepitopes x-size x-exempt_from_immobilisation x-aggregated
  set color x-color
end

另一种方法是接受列表作为参数,然后让您的过程根据列表中的项目数量而有所不同,但这可能比它的价值更复杂。