itemX
and
itemY
to number, and the type of
data
to list.
When you click Apply
, you should be able to see the block with two round number input spots and one list input spot.
(By the way, we called the list input
data
instead of
list
because people have commented that it's too easy to confuse a
list
variable block with the block to create an empty list: .)
Finish entering the complete script:
Here is an example of how the block will be used: If we run the block with the arguments
where game board
is the list
then the list should become
The
replace item
blocks in this script are an example of a new technique, list mutation--changing items in an existing list, instead of creating a new list the way
map
and
keep
do. As in this example, mutation can be more efficient than recopying the entire list except for the two changed items. On the other hand, mutation introduces the possibility of new kinds of bugs that can't happen with functional programming, the technique we've been using with lists until now. If two parts of a large program are using the same list for different purposes, and one part mutates the list, the other part will get confused. That's why we showed you functional programming first, and why functional programming should be your first instinct.
We have used mutation before with non-list data; for example, the
for
block changes the value of its variable
i
each time through the loop. Mutation isn't so dangerous in that context, because that variable exists only inside the
for
loop, there's no way another part of the program can be relying on an old value. Mutating global variables does have the same risk, though, which is why we've been saying that script variables are a better choice when possible. But a game board is an example of a value that's unavoidably both global and mutable. We'll go into this further in the next lesson.