# In every file, the constants (including functions) are exported. @io [quicksort: [?a]]: if ((count quicksort) == 0) then {quicksort} else: head = quicksort.0 tail = tail quicksort smaller = quicksort (filter tail keep {(?a) _ < head}) bigger = quicksort (filter tail keep {(?a) _ >= head}) # Put all smaller elements on the left. smaller + [head] + bigger print (quicksort [3 1 4 2]) # Let's say we need a new type. Types are generated by constant instances. Complex = [real: Int; imag: Int] [+ a: Complex; b: Complex]: [real: (real a) + (real b); imag: (imag a) + (imag b)] top: real 5 imag 7 left: real -4 imag 0 print (top + left) #> 1, 7 [each = [?item]; do = {(?item)}]: i: 0 while {i < (length each)} do: call do with (each.i) i += 1 res = readLine stdin if res is Error then: print (message res) if res is String then: print res if res is (str: String, count: Int) then: print str # Implementation of the Sieve of Atkin. [sieve max:0 into primes:(chan 0)]: sqrtmax := sqrt max # Construct the prime data. isPrime: array bool size max from 0 to max do {[i:0] (isPrime.i) = no } # Integers with an odd number of representations by certain quadratic forms. from 1 to sqrtmax do: [x:0] x2: x*x from 1 to sqrtmax do: [y:0] y2: y*y n: 4*x2 + y2 if ((n <= max) & (((n % 12) == 1) | ((n % 12) == 5)) then: (isPrime.n) = not (isPrime.n) n = 3*x2 + y2 if ((n <= max) & ((n % 12) == 7)): (isPrime.n) = not (isPrime.n) n = 3*x2 - y2 if ((x > y) & (n <= max) & ((n % 12) == 11)): (isPrime.n) = not (isPrime.n) # Sieving happens here. from 5 to sqrtmax do: [n:0] n2: n * n if (isPrime.n) then: coeff: 1 k: coeff * n2 while (k < max) do: (isPrime.n) = no coeff += 1 k = coeff * n2 # Produce the primes. put 2 in primes put 3 in primes from 5 to max do: [n:0] if (isPrime.n) then: put n in primes [main]: chan: channel int get chan do {[i:0] println i} sieve 1000 into channel