2

I understand that the command compares and can subtract values, but I don't see exactly how that works. I've used a TI BASIC programming tutorial site (http://tibasicdev.wikidot.com/movement-explanation) and I need clarification on List as a whole.

This portion of the code with List is as follows,:

:min(8,max(1,A+sum(ΔList(Ans={25,34→A
:min(16,max(1,B+sum(ΔList(K={24,26→B

and the website explains the code like this.:

"This is how this code works. When you press a key, its value is stored to K. We check to see if K equals one of the keys we pressed by comparing it to the lists {24,26 and {25,34. This results in a list {0,1}, {1,0}, or {0,0}. We then take the fancy command Δlist( to see whether to move up, down, left or right. What Δlist( does is quite simple. Δlist( subtracts the first element from the second in the previous list, and stores that as a new one element list, {1}, {-1}, or {0}. We then turn the list into a real number by taking the sum of the one byte list. This 1, -1, or 0 is added to A."

StealthDroid
  • 365
  • 1
  • 4
  • 14

1 Answers1

3

The ΔList( command subtracts every element in a list from its previous element. This code uses some trickery with it to compactly return 1 if a key is pressed and -1

ΔList( calculates the differences between consecutive terms of a list, and returns them in a new list.

ΔList({0,1,4,9,16,25,36})
   {1 3 5 7 9 11}

That is, ΔList({0,1,4,9,16,25,36}) = {1-0, 4-1, 9-4, 16-9, 25-16, 36-25} = {1 3 5 7 9 11}.

When there are only two elements in a list, ΔList({a,b}) is therefore equal to {b-a}. Then sum(ΔList({a,b})) is equal to b-a, since that's the only term in the list. Let's say that K is 26 in your example; that is, the > key is pressed.

B+sum(ΔList(K={24,26→B      Result of expression:
            K               26
            K={24,26        {0,1}
      ΔList(K={24,26        {1} = {0 - 1}
  sum(ΔList(K={24,26        -1
B                           [current x-position of player]
B+sum(ΔList(K={24,26→B      [add 1 to current x-pos. of player]

Similarly, B will be decreased if key 24, the left key, is pressed.

Community
  • 1
  • 1
lirtosiast
  • 592
  • 4
  • 15