2016-04-11 18 views
1

他の多くのプログラミング言語では、関数を引数として別の関数に渡して、関数内から呼び出すことができます。関数をNetlogoのパラメータとして渡す

Netlogoでこれを行うにはどうしますか?次のような

は:

;; 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 

これは便利な機能だろう。私は、これが客観的な関数などを渡すためにいいと思う抽象的なローカル検索アルゴリズムを実装しようとしています。

答えて

4

タスクを作成し、runresultを使用してタスクを実行することによって、関数をパラメータとして渡すことができます。

;; 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 (runresult some-function x y (z + 2)) 
end 

to go 
    show g (task f) 1 2 3 
end 
+2

はい。タスクの詳細については、http://ccl.northwestern.edu/netlogo/docs/programming.html#tasksを参照してください。 –

1

関数として関数を渡すことはできませんが、確かに関数名をテキストとして渡してからrunresultプリミティブを使用して関数を実行することができます。面倒だが行ける。

+0

お寄せいただきありがとうございます。しかし、あなたの助けを借りて、私は解決策を見つけました。結果をStringで実行するべきではありませんが、代わりに、関数をタスクに変換し、適切な入力を使ってタスクを実行します。 – mattsap

2

Netlogo 6.0.1では、矢印の構文がタスクを置き換えました。以下は、受け入れられた解答と同じことをするが、更新された構文を用いる。

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 
関連する問題