#include #include #include // learned has to come afer stdlib, which uses the word tag // Usage: // ./a.out 5 // example does an unchecked reference to argv[1] forall( [@N@] ) $\C{// array dimension}$ array( bool, @N@) & f( array( float, @N@ ) & x, array( float, @N@ ) & y ) { array( bool, @N@ ) & ret = *@alloc@(); $\C{// sizeof ret used by alloc}$ for ( i; @N@ ) { ret[i] = 0.005 > 2 * (abs( x[i] - y[i] )) / (abs( x[i]) + abs(y[i] )); } return ret; } // TODO: standardize argv int main( int argc, char * argv[] ) { const int @n@ = ato( argv[1] ); $\C{// deduce conversion type}$ array( float, @n@ ) x, y; $\C{// VLAs}$ for ( i; n ) { $\C{// initialize arrays}$ x[i] = 3.14 / (i + 1); y[i] = x[i] + 0.005 ; } array( bool, @n@ ) & result = @f( x, y )@; $\C{// call}$ sout | "result: " | nonl; $\C{// print result}$ for ( i; n ) sout | result[i] | nonl; sout | nl; free( &result ); $\C{// free result storage}$ } /* $\$$ ./a.out 5 result: true true true false false $\$$ ./a.out 7 result: true true true false false false false */ void fred() { array( float, @10@ ) x; array( float, @20@ ) y; f( x, x ); f( y, y ); // f( x, y ); } #ifdef SHOWERR1 forall( [M], [N] ) void bad( array(float, M) &x, array(float, N) &y ) { f( x, x ); $\C[1.5in]{// ok}$ f( y, y ); $\C{// ok}$ f( x, y ); $\C{// error}\CRT$ } #endif forall( [M], [N] ) void bad_fixed( array( float, M ) & x, array( float, N ) & y ) { if ( M == N ) f( x, @(array( float, M ) &)@y ); $\C{// cast y to matching type}$ } // Local Variables: // // compile-command: "sed -f sedcmd hello-array.cfa > ../build/tmp.cfa; cfa ../build/tmp.cfa -Wall -Wextra" // // End: //