implemented viterbi as a scan instead of mutable state