Processing Lists, Strings and Sets

NetLogo Data Types

There are two types of types: simple and composite. A value of a simple type contains no component values, while a value of a composite type may.

NetLogo's only simple types are Number (integers and floats) and Boolean (true, false).

We may consider Turtle and Patch as composite types because turtles and patches can contain attribute values.

String is a composite types because strings contain substrings.

Note: NetLogo doesn't have a Character type. Instead, characters are represented by length-one strings: "a" "e" "i" "o" "u" etc.

AgentSet values are sets of turtles or sets of patches.

List is the most general composite type. Lists may contain any values:

[1 "2" true [4 5]]

Strings

A string is a sequence of characters delimited by double quotes:

set state “Mississippi”

Comparing Strings

We can compare strings using the same operators we use to compare numbers:

observer> print "aardvark" < "zebra"
true

The following reporter detects palindromes:

to-report is-palindrome [some-string]
  report some-string = reverse some-string
end

Traversing Strings

The following procedure uses the length and item procedures to traverse a string:

to spell [some-string]
  let num-chars length some-string
  let index 0
  repeat num-chars
  [
    let next-char item index some-string
    print next-char
    set index index + 1
  ]
end

For example:

observer> spell "Mississippi"
M
i
s
s
i
s
s
i
p
p
i

Concatenating Strings

The following procedure demonstrates how the word procedure is used to concatenate a string with other values:

to print-turtles
  ask turtles
  [
    print (word "turtle #" who " is on " patch-here)
  ]
end

For example, here’s the output produced from a model containing 3 randomly placed turtles:

observer> print-turtles
turtle #0 is on (patch -9 -11)
turtle #1 is on (patch -12 16)
turtle #2 is on (patch 14 4)

Extracting, Locating, and Replacing Substrings

to demo
  let state "Mississippi"
  print position "ss" state
  print substring state 2 4
  print replace-item 2 state "fff"
  print state
end

 

oserver> demo
2
ss
Mifffsissippi
Mississippi

Lists

Building Lists

The hard way using first-put (fput):

set vowels []
set vowels fput "u" vowels
set vowels fput "o" vowels
set vowels fput "i" vowels
set vowels fput "e" vowels
set vowels fput "a" vowels

The easy way:

set vowels (list "a" "e" "i" "o" "u")

And the super-easy way:

set vowels ["a" "e" "i" "o" "u"]

Note: The super-easy way only works for constants.

first, but-first, shuffle, remove-duplicates, one-of

The following procedure demonstrates these reporters:

to demo1
  let fibs [0 1 1 2 3 5 8 13 21 34]
  print first fibs
  print but-first fibs
  print shuffle fibs
  print remove-duplicates fibs
  print one-of fibs
  print fibs
end

Here's the output produced.

observer> demo1
0
[1 1 2 3 5 8 13 21 34]
[21 13 3 1 0 1 5 34 2 8]
[0 1 2 3 5 8 13 21 34]
2
[0 1 1 2 3 5 8 13 21 34]

Example: Connect the Dots

of

There are lots of other useful list reporters. For example:

observer> print mean [200 300 400]
300

Suppose each turtle has a fitness attribute and we want to print the average fitness. We can use the of reporter to convert the fitnesses of all turtles into a list of numbers:

print mean [fitness] of turtles

Agent Sets

ask, of, one-of, in-radius, other, count

turtles-own [fitness]

to init-model
  ca
  crt 50 [set fitness random 100 setxy random-xcor random-ycor]
end

to demo-sets
  print count turtles
  print mean [fitness] of turtles
  let chosen-turtle one-of turtles
  print chosen-turtle
  ask chosen-turtle
  [
    let candidates turtles in-radius 5
    let healthy-candidates candidates with [fitness > 50]
    let other-healthy-candidates other healthy-candidates
    let candidate one-of other-healthy-candidates
    if candidate != nobody
    [
      print candidate
    ]
  ]
end

Here's the output produced:

observer> demo-sets
50
53.06
(turtle 37)
(turtle 4)