3 ms·
It took me a while to understand the syntax in general and the primes example in particular. For those facing the same obstacle, here is what I understood: 1.
by henrydark 4y ago
It took me a while to understand the syntax in general and the primes example in particular. For those facing the same obstacle, here is what I understood:
1. Read single lines from right to left.
2. Imagine a stack of one variable that you keep replacing the top as you go from right to left, kind of like concatenative languages (e.g. FORTH, kitten, factor), but right to left instead of left to right.
3. Here is a step by step translation of the primes example to imepartive pseudo code:
op primes N = (not T in T o.\* T) sel T = 1 drop iota N
First of all this is a function definition, so can be put to
function primes(N)
return (not T in T o.\* T) sel T = 1 drop iota N
Now we apply the two rules above:
function primes(N)
x = iota(N) // 1..N
x = drop(1, x) // 2..N
T = x
ToT = outerproduct(T, T) // a*b for all a, b in T, so 4,6,...,N^2
return [x in T not in Tot]
So we get a O(N^2) (or maybe O(N^3), depending on how `in` is implemented) algorithm for primes until N.
Here is a similar algorithm in python that might help further:
def primes(N):
T = range(2, N)
ToT = [a*b for a in T for b in T]
return [x for x in T if x not in ToT]
- deleted 4y ago[deleted]
- otabdeveloper4 4y agodef primes (array.skip(count.@, 1).. tuple(@, {mul.@:combo(@,@)}).. tuple(T=second.@, ?[1!=has(T,@),@:first.@])), primes.3000 https://tab-lang.xyz https://tab-lang.xyz
- dan-robertson 4y agoI guess you’ve added a backslash in the outer product to prevent HN formatting weirdness. It isn’t an outer product where the operation is ‘scan product’.