import random, algorithm # a < b ==> cmp(a, b) < 0 proc cmp(a, b: int): int = (a > b).int - (a < b).int proc quicksort(a: var openarray[int]; left, right: int; cp: proc (a, b: int): int) = proc qsort(left, right: int) = var l, r, p: int let len = right - left let len2 = len div 2 if true: # insertion sort on tiny array for i in left + 1 .. right: for j in countdown(i, left + 1): if cp(a[j], a[j - 1]) >= 0: break # this is the line with error swap(a[j], a[j - 1]) return qsort(a.low, a.high) var x: array[2e6.int, int] quicksort(x, x.low, x.high, cmp) e.nim(15, 17) Error: illegal capture 'a
I tried writing it in this way because recursively passing the cmp proc makes sorting a bit slower. I tried pragmas procvar and closure, but that seems to make no difference.