All Source For slang |
Ackermann's Function |
% $Id: ackermann.slang,v 1.0 2002/11/29 10:39:00 dada Exp $
% http://dada.perl.it/shootout/
%
% contributed by John E. Davis
define ack ();
define ack (M, N)
{
!if (M)
return( N + 1 );
!if (N)
return ( ack(M-1, 1) );
return( ack(M-1, ack(M, N-1)) );
}
define main()
{
variable NUM = integer (__argv[1]);
vmessage ("Ack(3,%d): %d", (NUM, ack(3, NUM)));
}
main()
|
Array Access |
% $Id: ary3.slang,v 1.0 2003/01/03 14:06:00 dada Exp $
% http://dada.perl.it/shootout/
%
% contributed by John E. Davis
define main_slow()
{
variable n = integer (__argv[1]);
variable x = Int_Type[n];
variable y = Int_Type[n];
_for (0,n-1,1)
{
variable i = ();
x[i] = i + 1;
}
loop (1000)
{
i = [n-1:0:-1];
y[i] += x[i];
}
vmessage ("%S %S", y[0], y[-1]);
}
% define main_fast ()
% {
% variable n = integer (__argv[1]);
% variable x = [1:n];
% variable y = Int_Type[n];
%
% loop (1000)
% y += x;
%
% vmessage ("%S %S", y[0], y[-1]);
% }
main_slow();
|
Count Lines/Words/Chars |
% $Id: wc.slang,v 1.0 2003/01/03 14:41:00 dada Exp $
% http://dada.perl.it/shootout/
%
% contributed by John E. Davis
define main()
{
variable nl, nw, nc;
nl = 0; nw = 0; nc = 0;
foreach (stdin) using ("line")
{
variable line = ();
nl++;
nc += strlen (line);
nw += length (strtok (line));
}
vmessage ("%d %d %d", nl, nw, nc);
}
main()
|
Fibonacci Numbers |
% $Id: fibo.slang,v 1.0 2002/11/26 10:33:00 dada Exp $
% http://dada.perl.it/shootout/
%
% contributed by John E. Davis
define fib ();
define fib(n)
{
if (n < 2)
return 1;
fib(n-2) + fib(n-1);
}
vmessage ("%d", fib(integer (__argv[1])));
|
Hash (Associative Array) Access |
% $Id: hash.slang,v 1.0 2003/01/03 14:09:00 dada Exp $
% http://dada.perl.it/shootout/
%
% contributed by John E. Davis
define main ()
{
variable n = integer (__argv[1]);
variable X = Assoc_Type[Int_Type];
_for (1, n, 1)
{
variable i = ();
X[sprintf("%x", i)] = i;
}
variable c = 0;
_for (n, 1, -1)
{
i = ();
if (assoc_key_exists (X, string(i)))
c++;
}
vmessage ("%d", c);
}
main();
|
Hashes, Part II |
% $Id: hash2.slang,v 1.0 2003/01/03 14:09:00 dada Exp $
% http://dada.perl.it/shootout/
%
% contributed by John E. Davis
_auto_declare=1;
n = integer(__argv[1]);
hash1 = Assoc_Type[Int_Type];
_for (1, 10000, 1)
{
i = ();
hash1[strcat ("foo_", string(i))] = i;
}
hash2 = Assoc_Type[Int_Type, 0];
loop (n)
{
foreach (hash1) using ("keys", "values")
{
(k,v) = ();
hash2[k] += v;
}
}
vmessage ("%d %d %d %d",
hash1["foo_1"], hash1["foo_9999"], hash2["foo_1"], hash2["foo_9999"]);
|
Heapsort |
% $Id: heapsort.slang,v 1.0 2003/01/03 14:39:00 dada Exp $
% http://dada.perl.it/shootout/
%
% contributed by John E. Davis
variable IM = 139968;
variable IA = 3877;
variable IC = 29573;
variable LAST = 42;
define gen_random(max)
{
LAST = (LAST * IA + IC) mod IM;
return (max * LAST) / IM;
}
define heapsort(n, ra)
{
variable rra = 0, i = 0, j = 0;
variable l = (n shr 1) + 1;
variable ir = n;
forever
{
if (l > 1)
{
l--;
rra = ra[l];
}
else
{
rra = ra[ir];
ra[ir] = ra[1];
ir--;
if (ir == 1)
{
ra[1] = rra;
return;
}
}
i = l;
j = mul2(l);
while (j <= ir)
{
variable raj = ra[j];
if (j < ir)
{
variable raj1 = ra[j+1];
if (raj < raj1)
{
j++;
raj=raj1;
}
}
#iffalse
if (rra < raj)
{
ra[i] = raj;
i = j;
j = mul2 (j);
continue;
}
j = ir + 1;
#else
if (rra >= raj)
{
j = ir + 1;
break;
}
ra[i] = raj;
i = j;
j = mul2 (j);
#endif
}
ra[i] = rra;
}
}
define main()
{
variable N = integer (__argv[1]);
if (N < 1)
N = 1;
variable ary = array_map (Double_Type, &gen_random, [0:N]*0+1.0);
heapsort(N, ary);
vmessage ("%.10f", ary[N]);
}
main();
|
Hello World |
% $Id: hello.slang,v 1.0 2003/01/03 12:44:00 dada Exp $
% http://dada.perl.it/shootout/
%
% contributed by John E. Davis
vmessage("hello world");
|
List Operations |
% $Id: lists.slang,v 1.0 2003/01/03 14:38:00 dada Exp $
% http://dada.perl.it/shootout/
%
% contributed by John E. Davis
define new_list (n)
{
variable l = struct
{
root, tail, data, len
};
l.data = [1:n];
l.root = 0;
l.tail = n;
l.len = n;
return l;
}
define expand_list (l, len)
{
len += 4096;
variable data = Int_Type[len];
variable root = l.root;
variable tail = l.tail;
variable n = (tail - root);
if (n)
data[[0:n-1]] = l.data[[root:tail-1]];
l.data = data;
l.root = 0;
l.tail = n;
l.len = len;
}
define list_append (l, value)
{
variable tail = l.tail;
if (l.tail == l.len)
{
expand_list (l, l.len + 1);
tail = l.tail;
}
l.data[tail] = value;
tail++;
l.tail = tail;
}
define list_pop_right (l)
{
variable tail = l.tail;
if (tail == l.root)
return NULL;
tail--;
l.tail = tail;
return l.data[tail];
}
define list_pop_left (l)
{
variable root = l.root;
if (l.tail == root)
return NULL;
l.root = root+1;
return l.data[root];
}
define list_length (l)
{
return l.tail - l.root;
}
define reverse_list (l)
{
variable tail = l.tail;
variable root = l.root;
if (tail == root)
return;
tail--;
l.data[[root:tail]] = l.data[[tail:root:-1]];
}
define dup_list (l)
{
variable new_l = @l;
new_l.data = @l.data;
return new_l;
}
define list_to_array (a)
{
variable tail, root;
tail = a.tail;
root = a.root;
if (root == tail)
return NULL;
tail--;
return a.data[[root:tail]];
}
define check_eqs (a, b)
{
if (list_length (a) != list_length (b))
return 0;
variable data_a = list_to_array (a);
variable data_b = list_to_array (b);
if (data_a == NULL)
return 1; % same length, but empty
return not length (where (data_a != data_b));
}
variable SIZE = 10000;
define test_lists ()
{
variable L1 = new_list (SIZE);
variable L2 = dup_list (L1);
variable L3 = new_list (0);
forever
{
variable node = list_pop_left (L2);
if (node == NULL)
break;
list_append (L3, node);
}
forever
{
node = list_pop_right (L3);
if (node == NULL)
break;
list_append (L2, node);
}
reverse_list (L1);
if (L1.data[L1.root] != SIZE)
return -1;
if (0 == check_eqs (L1, L2))
return -2;
return list_length (L1);
}
define main ()
{
variable num = integer (__argv[1]);
loop (num)
num = test_lists ();
vmessage ("%d", num);
}
main ();
|
Matrix Multiplication |
% $Id: matrix.slang,v 1.0 2003/01/03 13:59:00 dada Exp $
% http://dada.perl.it/shootout/
%
% contributed by John E. Davis
variable size = 30;
define mkmatrix(rows, cols)
{
variable mx = [1:rows*cols];
reshape (mx, [rows, cols]);
return mx;
}
define main()
{
variable iter = integer (__argv[1]);
variable m1 = mkmatrix(size, size);
variable m2 = mkmatrix(size, size);
loop (iter)
variable mm = m1 # m2;
vmessage ("%.0f %.0f %.0f %.0f", mm[0,0], mm[2,3], mm[3,2], mm[4,4]);
}
main ();
|
Nested Loops |
% $Id: nestedloop.slang,v 1.0 2002/11/29 10:43:00 dada Exp $
% http://dada.perl.it/shootout/
%
% contributed by John E. Davis
define main()
{
variable x = 0;
variable iter = integer (__argv[1]);
loop (iter)
loop (iter)
loop (iter)
loop (iter)
loop (iter)
loop (iter)
x += 1;
vmessage ("%d", x);
}
main();
|
Random Number Generator |
% $Id: random.slang,v 1.0 2003/01/03 13:41:00 dada Exp $
% http://dada.perl.it/shootout/
%
% contributed by John E. Davis
variable IM = 139968;
variable IA = 3877;
variable IC = 29573;
variable LAST = 42;
define gen_random(max)
{
LAST = (LAST * IA + IC) mod IM;
(max * LAST) / IM;
}
define main()
{
variable N = integer (__argv[1]);
if (N < 1)
N = 1;
loop (N-1)
() = gen_random (100.0);
vmessage ("%.9f", gen_random(100.0));
}
main();
|
Reverse a File |
% $Id: reversefile.slang,v 1.0 2003/01/03 14:02:00 dada Exp $
% http://dada.perl.it/shootout/
%
% contributed by John E. Davis
()=array_map (Int_Type, &fputs, fgetslines (stdin)[[-1:0:-1]], stdout);
|
Sieve of Erathostenes |
% $Id: sieve.slang,v 1.0 2003/01/03 12:51:00 dada Exp $
% http://dada.perl.it/shootout/
%
% contributed by John E. Davis
define main()
{
variable NUM = integer (__argv[1]);
variable flags = Int_Type[8192 + 1];
loop (NUM)
{
flags[*] = 1;
variable count = 0;
_for (2, 8192, 1)
{
variable i = ();
if (flags[i])
{
#iftrue
flags[[i+i:8192:i]] = 0;
#else
_for (i+i, 8192, i)
{
variable j = ();
flags[j] = 0;
}
#endif
count++;
}
}
}
vmessage ("Count: %d", count);
}
main();
|
Spell Checker |
% $Id: spellcheck.slang,v 1.0 2003/01/03 14:46:00 dada Exp $
% http://dada.perl.it/shootout/
%
% contributed by John E. Davis
define main()
{
variable dict = Assoc_Type[Int_Type, 0];
foreach (fopen("Usr.Dict.Words", "r")) using ("line")
{
variable word = strtrim();
dict[word] = 1;
}
foreach (stdin)
{
word = strtrim ();
!if (dict[word])
() = fprintf (stdout, "%s\n", word);
}
}
main();
|
Statistical Moments |
% $Id: moments.slang,v 1.0 2003/01/03 13:38:00 dada Exp $
% http://dada.perl.it/shootout/
%
% contributed by John E. Davis
define main ()
{
variable nums = array_map (Double_Type, &atof, fgetslines (stdin));
variable n = length (nums);
variable sum = 0;
foreach (nums) sum += ();
variable mean = sum/n;
variable average_deviation = 0;
variable standard_deviation = 0;
variable variance = 0;
variable skew = 0;
variable kurtosis = 0;
% No slang programmer would code explicit loops.
foreach (nums)
{
variable num = ();
variable deviation = num - mean;
average_deviation += abs(deviation);
variance += deviation^2;
skew += deviation^3;
kurtosis += deviation^4;
}
average_deviation /= n;
variance /= (n - 1);
standard_deviation = sqrt(variance);
if (variance > 0.0)
{
skew /= (n * variance * standard_deviation);
kurtosis = kurtosis/(n * variance * variance) - 3.0;
}
nums = nums[array_sort(nums)];
variable mid = n/2;
variable median;
if (n mod 2)
median = nums[mid];
else
median = (nums[mid] + nums[mid-1])/2;
vmessage ("n: %d", n);
vmessage ("median: %f", median);
vmessage ("mean: %f", mean);
vmessage ("average_deviation: %f", average_deviation);
vmessage ("standard_deviation: %f", standard_deviation);
vmessage ("variance: %f", variance);
vmessage ("skew: %f", skew);
vmessage ("kurtosis: %f", kurtosis);
}
main ();
|
String Concatenation |
% $Id: strcat.slang,v 1.0 2003/01/03 13:36:00 dada Exp $
% http://dada.perl.it/shootout/
%
% contributed by John E. Davis
define main()
{
variable n = integer (__argv[1]);
variable str = typecast ("", BString_Type);
variable hello = typecast ("hello\n", BString_Type);
loop (n)
str += hello;
vmessage ("%S", strlen (str));
}
main();
|
Sum a Column of Integers |
% $Id: sumcol.slang,v 1.0 2002/11/26 10:35:00 dada Exp $
% http://dada.perl.it/shootout/
%
% contributed by John E. Davis
define main()
{
variable count = 0;
foreach (stdin) using ("line")
{
count += integer ();
}
vmessage ("%d", count);
}
main();
|
Word Frequency Count |
% $Id: wordfreq.slang,v 1.0 2003/01/03 14:43:00 dada Exp $
% http://dada.perl.it/shootout/
%
% contributed by John E. Davis
define main()
{
variable count = Assoc_Type[Int_Type, 0];
foreach (stdin) using ("line")
{
variable line = ();
foreach (strtok (strlow (line), "^a-z"))
{
variable word = ();
count [word] += 1;
}
}
variable words = assoc_get_keys (count);
variable freqs = assoc_get_values (count);
variable i = array_sort (array_map (String_Type, &sprintf, "%12d", freqs)
+ words);
i = i[[-1:0:-1]];
foreach (i)
{
i = ();
()=fprintf (stdout, "%7d\t%s\n", freqs[i], words[i]);
}
}
main();
|