I’m currently in the works of an AppleScript calculator that extrapolates arithmetic linear sequences (sounds fancy but it really isn’t). One of the processes it must go through is finding the common difference between each item. Plugging this difference (d) into the equation for arithmetic progression, along with the term you want to know (n) will predict the nth term in any sequence.
Link to website explaing arithmetic sequences: https://www.chilimath.com/lessons/intermediate-algebra/arithmetic-sequence-formula/
To determine the common difference, you must first find the local difference between each pair of numbers in the sequence. Then, compare every difference; if they’re all the same, the sequence is linear and the common difference is equal to the local difference. If not, the sequence is nonlinear and there is no common difference. I’ve figured out how to find the local differences of each number, but the latter seems to be a bigger challenge. The code I’ve made to determine the local differences spits out a list of numbers. My theoretical process would just compare the first item to every other, and if every comparison returns as true, then set the common difference to the same as the local difference. Below is my way of finding the local difference of a list of numbers:
set sequence to {1, 2, 3, 4, 4}
set x to 0
set dif_check to {}
repeat ((number of items of sequence) - 1) times
set x to (x + 1)
set selected_item to (item x of sequence)
set local_dif to ((item (x + 1) of sequence) - selected_item)
set dif_check to dif_check & {local_dif}
end repeat
return dif_check
--> result: {1, 1, 1, 0}
(* Since every item in the resulting list isn't equal, it would be nonlinear and the common difference would just be n/a. *)
How would I determine if every item in a list is the same? I’ve tried numerous times, including this script I experimented with, but I realized it didn’t get me anywhere closer to the resolution:
set dif_check to {1, 1, 1, 0}
set first_term to (first item of dif_check)
set x to 1
set common_dif_check to {}
repeat ((number of items of dif_check) - 1) times
set x to (x + 1)
(item x of dif_check) = first_term
set common_dif_check to common_dif_check & {result}
end repeat
return common_dif_check
--> result: {true, true, false}
(* This takes the first item of the list and compares it with every item after, returning a true or false and adding the result to another list. I thought this would have solved it, but instead just turns out as the same situation as before. *)
To reiterate, how would I determine if every item in a list is the same? Any help with this would be greatly appreciated. Thanks in advance.
Also, for some reason, the “Operating system” drop-down in the “System info” section doesn’t include 10.15 or 11.0. I’m currently running the macOS Big Sur 11.0.1 Beta 2.