06.07.2013 Views

Scilab Bag Of Tricks - Claymore

Scilab Bag Of Tricks - Claymore

Scilab Bag Of Tricks - Claymore

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 4. Unknown Spots<br />

Lisp as a (functional) language which employs pure prefix syntax all functions and operators are<br />

written before all arguments.<br />

(+ 3.9 54.0 -4.5 74.5 -57 -56)<br />

(setq x (list "a" -1 (- 3 10)))<br />

(length x)<br />

The same expressions look more or less differently in <strong>Scilab</strong>:<br />

3.9 + 54.0 + (-4.5) + 74.5 + (-57) + (-56)<br />

sum( [3.9 54.0 -4.5 74.5 -57 -56] ) – alternative to previous line<br />

x = list("a", -1, (3 - 10))<br />

length(x)<br />

As becomes clear in the above example, operators are specially written functions, but otherwise<br />

behave like ordinary functions.<br />

Overloading has been hyped since to advent of C++. A closer look reveals that even Fortran-77<br />

endows certain intrinsics with an overloaded syntax. What the heck is overloading? To overload a<br />

symbol means assigning another meaning to it, augmenting the existing meaning(s). Typically, the<br />

symbol is a function name and the additional meaning is an additional function definition.<br />

How can the language decide which definition to take? That depends on the language. The most<br />

common scheme to determine which function definition to trigger is the analysis of the actual<br />

function arguments. Fortran-77 provides so-called generic functions, sin is one example, which can<br />

be called with arguments of several types and the compiler selects the routine that matches that type.<br />

program f77ovl<br />

implicit none<br />

real xr, s1<br />

double precision xd, s2<br />

complex xc, s3<br />

* floating point literals default to real*4 in f77<br />

xr = 1.0<br />

s1 = sin(xr) – compiler selects single precision routine<br />

xd = 1.0d0<br />

s2 = sin(xd) – compiler selects double precision routine<br />

xc = (1.0, 0.0)<br />

s3 = sin(xc) – compiler selects complex routine<br />

* alternative using explicit call<br />

s2 = dsin(xd) – user demands double precision routine<br />

s3 = csin(xc) – user demands complex routine<br />

end<br />

38

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!