Consider the following function that adds 1 to each element in the list.
f : {{x + 1} each x}
f[(1;2;3)]
2 3 4
However, the following function does not work -- I'm assuming that the incr
identifier is unbound in the latter expression.
f : {incr : 1; {x + incr} each x}
f[(1;2;3)]
'incr
[3] f@:{x + incr}
How do I express something like the following in kdb/q?
let f lst =
let incr = 1 in
List.map (fun x -> x + incr) lst
incr
does not exist in the inner function.
See https://code.kx.com/q4m3/6_Functions/#63-local-and-global-variables for more details. You need to pass it in from the outer function if you want to use it.
If what you are trying to achieve is 2 3 4
- you can simply do: 1 2 3+1
. You can simply add a list and an atom in kdb. There is no need to use an iterator (each
) in this case.
f:1+
f 1 2 3
See https://code.kx.com/q4m3/4_Operators/#403-extension-of-atomic-functions and https://code.kx.com/q/ref/add/ for more details.
I would also recommend not adding additional whitespace when defining a variable or function in kdb: var:val
.