Puzzled by the NetLogo syntax (`word` and` runresult`)
The model "Artificial neural network - multilayer" has this code (the objective function comes from the interface and has the value "xor" or "or"):
to-report target-answer
let a [activation] of input-node-1 = 1
let b [activation] of input-node-2 = 1
;; run-result will interpret target-function as the appropriate boolean operator
report ifelse-value run-result
(word "a " target-function " b") [1][0]
end
I don't understand how "a" and "b" are converted to true or false if they are inside quotes. Why don't they just look like "a" and "b"? If the code was
(word a " " target-function " " b) [1][0]
which actually also works, I can understand the syntax, but "a" and "b" will puzzle me. Can anyone explain why this code works?
source to share
The key is the result of the execution . run-result
executes the given line as if it were code. Therefore, if target-function
or
, then
ifelse-value run-result
(word "a " target-function " b") [1][0]
equivalent to
ifelse-value run-result "a or b" [1][0]
which is equivalent
ifelse-value (a or b) [1][0]
As to why this is preferred (word a " " target-function " " b)
, this is partly a matter of taste. It's less keystrokes, maybe a little cleaner looking. It also does better with certain types of values. (word a " " target-function " " b)
inserts the values a
and b
before creating the row to be run, whereas (word "a " target-function " b")
sticks to the values ββwhen actually running the row. While it will never matter here, it would be important if we had code like:
let s "some string"
print runresult (word "length " s)
These are mistakes with Nothing named SOME has been defined
as he literally tries to evaluate length some string
. Compare this with
let s "some string"
print runresult (word "length s")
which outputs 11
.
Tasks
In the comments, Seth brings up the excellent view that the modern way of doing this is with tasks . To do this with tasks, we set target-function
to task or
(shorthand task [?1 or ?2]
) elsewhere in the code.
Then we would change run-result (word "a " target-function " b")
to:
(run-result target-function a b)
It just says, "Run the code stored in target-function
using a
and b
as inputs and give me the result." Much cleaner!
source to share