"Checkpoint 1 — Choose a coefficient convention"
| coefficients |
coefficients := #(2 -3 0 5).
{coefficients size - 1. coefficients last}

"Checkpoint 2 — Write the nested form first"
| x |
x := 2.
((((2 * x) - 3) * x) + 0) * x + 5

"Checkpoint 3 — Package the recurrence in a block"
| evaluate |
evaluate := [:terms :argument |
 terms isEmpty ifTrue: [Error signal: 'At least one coefficient is required'].
 terms allButFirst inject: terms first
  into: [:accumulator :coefficient | (accumulator * argument) + coefficient]].
evaluate value: #(2 -3 0 5) value: 2

"Checkpoint 4 — Keep a trace instead of guessing"
| coefficients x accumulator trace |
coefficients := #(2 -3 0 5). x := 2.
accumulator := coefficients first.
trace := OrderedCollection with: accumulator.
coefficients allButFirst do: [:coefficient |
 accumulator := (accumulator * x) + coefficient.
 trace add: accumulator].
trace asArray

"Checkpoint 5 — Handle constants and invalid input"
| evaluate rejected |
evaluate := [:terms :argument |
 terms isEmpty ifTrue: [Error signal: 'At least one coefficient is required'].
 terms allButFirst inject: terms first
  into: [:accumulator :coefficient | (accumulator * argument) + coefficient]].
rejected := [evaluate value: #() value: 2. false]
 on: Error do: [:error | true].
{evaluate value: #(7) value: 999. rejected}

"Checkpoint 6 — Preserve exact arithmetic when possible"
| evaluate answer |
evaluate := [:terms :argument |
 terms isEmpty ifTrue: [Error signal: 'At least one coefficient is required'].
 terms allButFirst inject: terms first
  into: [:accumulator :coefficient | (accumulator * argument) + coefficient]].
answer := evaluate value: #(2 -3 0 5) value: (1 / 2).
{answer. answer = (9 / 2). answer isInteger}

"Checkpoint 7 — Evaluate a derivative with the same block"
| coefficients degree derivative evaluate |
coefficients := #(2 -3 0 5).
degree := coefficients size - 1.
derivative := degree = 0
 ifTrue: [#(0)]
 ifFalse: [(1 to: degree) collect: [:index |
  (coefficients at: index) * (degree - index + 1)]].
evaluate := [:terms :argument |
 terms isEmpty ifTrue: [Error signal: 'At least one coefficient is required'].
 terms allButFirst inject: terms first
  into: [:accumulator :coefficient | (accumulator * argument) + coefficient]].
{derivative. evaluate value: derivative value: 2}

"Checkpoint 8 — Compare floating results with a tolerance"
| evaluate approximate exact tolerance |
evaluate := [:terms :argument |
 terms isEmpty ifTrue: [Error signal: 'At least one coefficient is required'].
 terms allButFirst inject: terms first
  into: [:accumulator :coefficient | (accumulator * argument) + coefficient]].
approximate := evaluate value: #(2 -3 0 5) value: 0.1.
exact := evaluate value: #(2 -3 0 5) value: (1 / 10).
tolerance := 0.000000000001.
{exact. (approximate - exact asFloat) abs < tolerance}