ERROR: syntax: extra token "for" after end of expression

Hi, the following is the code.

function f(a,b)
       return a+b


julia> f(i,i) for i=1:3

I got an error message, saying that extra token “for” after end of expression.

On the other hand, sum( f(i,i) for i = 1:3 ) works out well. Could anyone kindly drop a comment?

I think what you’re looking for is (f(i, i) for i=1:3). These are known as generators, and they require parentheses when used directly inside of a code block.

Note that generators are lazy sequences of values; if you want to materialize a vector, you can replace the () with []: [f(i, i) for i=1:3].