# How can I write this simple function in mathematical notation?

POINTS_TABLE = [3, 5, 7, 1]

function score(answer) {
result = 0
for i in 0..4
result += POINTS_TABLE[answer[i]]
return result
}

answer = [1, 2, 1, 0]

s = score(answer)

The sum performed is 5 + 7 + 5 + 3 = 20. It uses the values from the input as the indexes to read from the POINTS_TABLE.

This is the style of the answer I'm trying to work out: $$score(answer) = \sum_{i=1}POINTS\_TABLE_{answer_i}$$

• I don't really understand. What's wrong with the expression you already have? "Mathematical notation" isn't a programming langauge with defined syntax and semantics; it's much more like a natural language and there are multiple ways of writing things. Some may be more or less appropriate, depending on what you want to use the notation for. Commented Jan 14, 2017 at 22:24
• Oh, I thought math notation was strict. The expression I have was my best attempt, I assumed it was wrong. Commented Jan 14, 2017 at 22:28
• So do mathematicians reading the notation it have to interpret it and try to guess what's meant? Commented Jan 14, 2017 at 22:29
• No, the author of the paper should explain all non-standard notation. Commented Jan 14, 2017 at 22:34
• However did you get the idea that this was a functional program? O.o Commented Jan 14, 2017 at 22:34

## 1 Answer

While you are free to use whatever notation you want, as long as you explain it, it seems that the simplest solution here would be

$$score(answer) = \sum_{i=0}^3 POINTS\_TABLE[answer[i]]$$

This should be self-explanatory in most circumstances.

• What makes this the simplest? I've never seen notation that uses an array index like that. Would a non-programmer understand it? Commented Jan 15, 2017 at 0:18
• It's something I bet a lot of readers will understand. It's definitely not the simplest. It's just one reasonable solutions, there are many others. Commented Jan 15, 2017 at 5:14