©2010 Compass Learning Technologies ← Home ← TI-Nspire Authoring ← Dynamic Models using Scatter Plots and Notes ← Programming Dynamic Strings
Programming Dynamic Strings
1. Building Strings
As already discussed, anything within "quotes" is a string to TI-Nspire, including text, numbers, lists and even variables. But the quotes are very literal: if you have created a variable called v1 and you type "v1" then you will get back exactly what lies between the quotes. If, however, you use the string() command, the value of the variable is returned and will update as the variable changes. This is the basis for our use of dynamic strings.
Using the & character, we can now create useful and interesting combinations of text and variables: we can create dynamic questions! For example, suppose we wish to create an addition sum. We can use two variables, v1 and v2 and combine them with some text as shown: string(v1)&" + "&string(v2).
If our goal is to write programs for teaching and learning, then it would be pretty useful to know what the correct answer is to our question, so that we can check our students' responses. For this, we use the opposite of the string() command: expr(). If string() takes any expression and turns it into a string, then expr() takes any valid mathematical string and turns it into a numerical result. So expr("2+3") -> 5, and, in our example, expr(string(v1)&" + "&string(v2)) -> v1+v2.
USEFUL TIP: Presentation is Everything!
Did you notice that our variable numbers always seem to bring along a decimal point? This is because a slider value will always be approximate, but it is quite distracting, especially for younger or less able students. We can suppress this using the ugly but useful command approxRational().
Just put approxrational( in front of the value that you wish to change, and make sure you remember to close off the parentheses after the value: for example: approxrational(v1). You do NOT need to add the ",5.E-14" - it gets added automatically. Your integers will now be presented correctly. Ugly but effective.
The main purpose of the approxrational( command is, of course, to approximate rational numbers: it just happens to have this neat side effect with integers. See what happens with approxrational(0.6), approxrational(0.2+1/5). The tolerance (",5E-14") comes in useful when you consider recurring decimals, like approxrational(0.666). Left at full accuracy, approxrational(0.666,5E-14) -> 333/500 but if you reduce the accuracy: approxrational(0.666,5E-2) -> 2/3.
Are you starting to get ideas about creating all sorts of interesting questions for your students? Everything you learn here about manipulating strings can be used directly on a Notes page (or a spreadsheet, or Calculator…) OR within a program.
The basis of most interesting tutorial programs will be the creation of random questions, so that each time the student uses the program, they are asked different questions. For this we use the randInt() command: for example, randInt(1,10) will output a random integer between 1 and 10. It is that simple.
But what if we want our random integer to be dynamic - say, controlled by a slider? A little trickery here: create a slider, say v3. Changing the slider does not affect the random number. But if you link the random number to the slider, then it will: randInt(1,10)+v3-v3. Note that this method does not change the actual number produced, but it DOES link to the slider: each change in the slider causes the random integer to update and recalculate!
Review: So far we have covered…
- &
- string()
- expr()
- approxrational()
- randInt()
Check that you understand and can use each of these!
2. Breaking Strings Apart
Before we can go ahead and start creating powerful learning tools, we need to learn the other side of working with strings: how to break them apart (or, in computer terms, how to "parse" a string and tell what is in it). The first useful command for this purpose is very simple: inString().
For example, consider the string "steve". Then
inString("steve","s") -> 1
inString("steve","v") -> 4
inString("steve","e") -> 3
inString("steve","st") -> 1
inString("steve","a") -> 0
Get the idea? This will even work for a space (" ") so inString("steve arnold"," ") -> 6
So if we have a string that we want to break into component pieces, like "steve arnold" or "2x+1=4-3x" then this is where we start.
We also need another very simple command: dim(). The "dim" command gives the dimension, or length, of a string (it also works for a list!). So dim("steve") -> 5, dim("steve arnold") -> 12 and dim("2x+1=4-3x") -> 9.
Now we are ready - but take a deep breath because this next bit can get a little messy.
The two key commands to break strings apart are, quite simple, left() and right(). For example, left("steve arnold",6) -> "steve ". Note that it includes the 6th character (the space). So if we want to pull out everything to the left of the space, but not include the space, we would use left("steve arnold",5) -> "steve".
In general, left("steve arnold",inString("steve arnold"," ")-1) -> "steve".
Study this closely. Make sure it makes sense to you. Now look at the way it has been done in the image of the Notes page, which should help you to see the general structure of the command. I create a string (str) and a variable that captures the position of the space in that string - if one exists (instr). So I can collect all characters to the left of the space using the command left(str,instr-1).
Now try str1:="2x+1=4-3x" and, instead of the space in instr1 use the "=".
So far so good. Now if only the right() command behaved in the same way. At first glance it appears to: right("steve arnold",6) -> "arnold" just as we might hope - it outputs all characters to the right of the 6th character (the space). But this is only because my name is symmetrical around the space. Try right("s arnold",2) -> "ld". Hmmm…
left() counts characters from the beginning of the string; right() counts from the end of the string backwards.
So if we knew how many characters were in the string, we could subtract from that and it should work: right("s arnold",dim("s arnold")-inString("s arnold"," ")) -> "arnold".
Yuck. Once again, study the Notes image to the right. The structure to find characters in a string ("str") to the right of a given character ("char") is right(str,dim(str) - instr) where instr:= inString(str,char).
I do nearly all my string work with left and right but there is one other useful command: mid(). Try mid("steve arnold",1,3) -> "ste". This returns the 3 characters counting from the first term, while mid("steve arnold",7,3) -> "ran" gives the three characters counting from the 7th term. So we can return 1 or more characters from a string this way.
Even better, though: if you don't specify the number of characters, it returns the rest of the string: so mid(str,instr+1) -> "arnold". And this is actually much simpler than using the right() syntax for this task!
Well that was hard work - but, truthfully, that is as hard as this stuff gets, so take your time, try some examples yourself, and you will get it.
Review: In this section we have covered…
- dim()
- inString()
- left()
- right()
- mid()
Check that you understand and can use each of these!
Let's put all this together in a simple example. We have defined str1:="2x+1=4-3x" and instr1:=inString(str1,"=") so insert a Graphs page and type:
f1(x) = expr(left(str1,instr1-1))
f2(x) = expr(mid(str1,instr1+1))
Can you see what is happening here and why this works? Because the left and mid expressions are valid mathematical expressions for graphing, the expr() command means that they can be acted upon in a mathematical way - and we get the graphs!
Now we are ready to write some programs!
©2010 Compass Learning Technologies ← Home ← TI-Nspire Authoring ← Dynamic Models using Scatter Plots and Notes ← Programming Dynamic Strings