Hi everyone,

Why does the following compile:

import future

echo lc[x+2 | (x <- 1..10), int]

to give

@[3, 4, 5, 6, 7, 8, 9, 10, 11, 12]


import future

echo lc[x^2 | (x <- 1..10), int]

gives a compilation error:

Error: type mismatch: got (int, int literal(2))
but expected one of:
proc `^`(x: int): int
proc `^`[T](x: int; y: openArray[T]): int
> Process terminated with exit code 1

2017-09-13 12:26:14
import math too
2017-09-13 13:35:08

The problem is that ^ is not a binary operator in Nim, but used with array indexing to index the n-th element from the end (e.g. a[^1]).

If you want to square x, use:

echo lc[x*x | (x <- 1..10), int]

If you want xor (i.e. the semantics of ^ in C/C++), use:

echo lc[x xor 2 | (x <- 1..10), int]

2017-09-13 14:23:36
Ah, I see. I didn't know that I had to import math to get power, and I wasn't familiar with the syntax of binary operators to be able to interpret the error message appropriately. Thanks!
2017-09-14 07:46:56