How to Determine If All Items In A List Are The Same

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.

bez9199. This is my suggestion, which basically works the same as your script.

``````set dif_check to {1, 1, 1, 0}
set dif_exists to false

repeat with i from 2 to (count dif_check)
if item i of dif_check is not equal to item 1 of dif_check then
set dif_exists to true
exit repeat
end if
end repeat

dif_exists --> true
``````

FWIW, your first script could be made a bit more compact as follows:

``````set sequence to {1, 2, 3, 4, 4}
set diff_check to {}

repeat with i from 2 to (count sequence)
set end of diff_check to (item i of sequence) - (item (i - 1) of sequence)
end repeat

diff_check --> {1, 1, 1, 0}
``````

If you only need to determine whether a common difference exists, and if so what that difference is, then you could combine the scripts as follows:

``````set sequence to {1, 3, 5, 7, 9}
set dif_exists to false

set theDifference to (item 2 of sequence) - (item 1 of sequence)

repeat with i from 3 to (count sequence)
if ((item i of sequence) - (item (i - 1) of sequence)) is not equal to theDifference then
set dif_exists to true
exit repeat
end if
end repeat

if not dif_exists then
set common_dif to theDifference
else
set common_dif to missing value
end if

common_dif --> 2

``````

Better to leave the repeat loop right after the first difference, meaning the sequence is non-linear:

``````
set sequence to {1, 2, 3, 4, 4}

tell sequence to set previousDif to (item 2) - (item 1)
repeat with n from 2 to (count sequence) - 1
tell sequence to set nextDif to (item (n + 1)) - (item n)
if nextDif ≠ previousDif then return display notification "The sequence is non-linear"
set previousDif to nextDif
end repeat

display notification "The sequence is linear"

``````

And, here is variation of the script using acceptable approximation error:

``````
property approximationError : 0.6
set sequence to {1, 2, 3, 4, 4.5}

tell sequence to set previousDifference to (item 2) - (item 1)
repeat with n from 2 to (count sequence) - 1
tell sequence to set nextDifference to (item (n + 1)) - (item n)
if not ((nextDifference - previousDifference) < approximationError) then
return display notification "The sequence is non-linear"
end if
set previousDifference to nextDifference
end repeat

display notification "The sequence is practically linear"
``````