Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass a function as a parameter in Netlogo

In many other programming languages, you can pass a function as an argument to another function and call it from within the function.

Is there anyway to do this in Netlogo?

Such as the following:

;; x,y,z are all ints
to-report f [x y z]
  report x + y + z
end

;; some-function is a function
;; x y and z are ints
to-report g [some-function x y z]
  report (some-function x y z) + 2
end

to go
  show g f 1 2 3
end

This would be a nice feature. I'm trying to implement an abstract local search algorithm which this would be nice for passing in objective functions and such.

like image 997
mattsap Avatar asked Oct 24 '25 15:10

mattsap


1 Answers

As of Netlogo 6.0.1, the arrow syntax replaced tasks. The below does the same thing as the accepted answer but with the updated syntax.

to-report f [x y z]
  report x + y + z
end

;; some-function is a function
;; x y and z are ints
to-report g [some-function x y z]
  report (runresult some-function x y (z + 2))
end


to go
  show g [[x y z] -> (f x y z)] 1 2 3
end
like image 82
mattsap Avatar answered Oct 27 '25 15:10

mattsap