Zurück zum Inhaltsverzeichnis
Kapitel 29
Parameter einer Prozedur
> restart:
>
test:=proc(x,y,z)
x,y,z:
end:
> test(1,sqrt(2), [a,b,c], x);
> test(1,2):
Error, (in test) test uses a 3rd argument, z, which is missing
>
test:=proc(x,y)
local n;
x, y, seq( args[n], n=3..nargs):
end:
> test(a,b,c,d,e);
>
Parameter in [ ] Klammern
> restart:
>
fn:=proc(x3)
print(op(1,procname), op(2,procname), x3);
end:
> fn[a,b](c);
Typ-Überprüfung
>
test:=proc(x,y)
if not type( [args],
[nonnegint, nonnegint]) then
error "Falsche Eingabewerte:",args
end if:
sqrt(x*y);
end proc:
> test(3,2);
> test(-1,2);
Error, (in test) Falsche Eingabewerte:, -1, 2
>
test:=proc(x::nonnegint,
y::nonnegint)
sqrt(x*y);
end proc:
> test(1.5, 2):
Error, test expects its 1st argument, x, to be of type nonnegint, but received 1.5
>
test:=proc(x::list( {constant} ))
print(x);
end:
> test( [a,2,Pi]);
Error, test expects its 1st argument, x, to be of type list({constant}), but received [a, 2, Pi]
> a:=1:test( [a,2,Pi]);
Schleifen und Verzweigungen
>
for x from 1 to 3 do
print(x);
end do:
>
for x from 5 to 1 by -2 do
print(x):
end do:
>
for i in [1,2,3] do
print(i)
end do:
>
x:=0:
while (x<3) do
x:=x+1:
end do;
>
x:=0:
while is(x<=Pi) do:
print(x):
x:=x+Pi/4:
end do:
>
x:=0:
do
x:=x+1:
if x>3 then break; end if;
print(x):
end do:
>
Variablen: Bindung, Auswertung, Gültigkeit
>
test:=proc()
local i:
i:= 5;
end proc:
> i:=0: test();
> i;
>
test:=proc()
global i:
i:=5;
end proc:
> i:=0: test();
> i;
> restart:
>
fn := proc(x)
global _fn_digits:
if _fn_digits='_fn_digits' then
evalf(x)
else evalf(x,_fn_digits)
end if:
end proc:
> fn(1/3);
> _fn_digits:=20: fn(1/3);
>
gerade:=proc(m,b)
proc(x)
m*x+b;
end proc:
end proc:
> g1:=mal_x_plus_z(1/2,-1);
> g1(6);
> op(1,eval(g1));op(7,eval(g1));
> plot(g1,-5..6,scaling=constrained);
>
make_zähler:=proc(init)
local a;
a:=init;
proc ()
a:=a+1;
end proc:
end proc:
> z1:=make_zähler(0);z2:=make_zähler(9);
> seq([z1(),z2()],i=1..5);
> eval(op(7,(eval(z1))));eval(op(7,(eval(z2))));
>
makestack:=proc()
local stapel,leer,rein,raus;
stapel:=[];
leer:=proc()
is(nops(stapel)=0);
end proc;
rein:=proc(obj)
stapel:=[obj,stapel[]];
true;
end proc;
raus:=proc()
local erg;
erg:=stapel[1];
stapel:=stapel[2..-1];
erg;
end proc;
table([(empty)=leer,
(push)=rein,
(pop)=raus]);
end proc:
> s1:=makestack();
> s1[empty]();
> seq(s1[push](i^2),i=1..5);
> seq(s1[pop](),i=1..5);
>
Variablenauswertung
Interaktiv: vollständige Auswertung
> restart:
>
a:=b:
b:=c:
a+1;
eval(a+1,1);
Bei lokalen Variablen nur eine Stufe
>
restart:
test:=proc()
local a,b,c:
a:=b: b:=c: a+1;
end:
> test();
Parameter per Referenz übergeben
> restart:
>
test:=proc(x::evaln)
x:=5;
end:
> a:=3: test(a): a;
>
test:=proc(x::evaln)
x:=x+1;
end:
> a:=3: test(a): a;
Error, too many levels of recursion
>
test:=proc(x::evaln)
x:=eval(x)+1;
end:
> a:=3: test(a): a;
> a:=sin(b): test(a): a;
Ändern eines Feld-Elements
>
test:=proc(x::array)
x[1]:=1:
end:
>
a:=array([0,0]): test(a):
print(a);
procedure options
> restart:
>
test:=proc(x)
options operator, arrow;
x^2;
end:
> print(test):
>
test:=proc(x)
options `Copyright mk 96`;
x^2;
end:
> op(3, op(test));
evalhf (Gleitkommazahlen mit Hardware)
> restart:
>
test:=proc(n)
local i;
0:
for i from 1 to n do:
%+1/i^2:
end do:
%:
end proc:
> test(8000): evalf(%,18);
> evalhf(test(8000));
> sum(1/n^2,n=1..infinity);
> evalf(%,18);
>
>
test:=proc(x::array)
x[1]:=0:
end:
>
>
a:=array([1,2]):
evalhf( test(var(a)) ): print(a);
Definition von Operatoren
>
`&op1`:=proc(x)
x^2;
end:
> &op1 a;
>
`&op2`:=proc(x,y)
x^2+y^2:
end:
> a &op2 b;
>
`&op3`:=proc(x,y,z)
x+y+z;
end:
> &op3(a, b, c);
Funktionen mit eigenen Optionen
>
test:=proc(l1::list, l2::list)
local opts, available, value, opta, optb,optc;
opts:=[args[3..nargs]];
if not hasoption(opts, optiona=posint, opta, 'opts') then
opta:=10:
end if:
if not hasoption(opts, optionb=string, optb, 'opts') then
optb:="default":
end if:
if not hasoption(opts, optionc=boolean, optc, 'opts') then
optc:=false:
end if:
if nops(opts)>0 then
error "invalid option", opts:
end if:
print(opta,optb,optc);
end proc:
> test([],[]);
> test([],[], optiona=5, optionb="xyz", optionc=false);
> test([],[], optiona=xy);
Error, (in hasoption) The optiona option must be a posint but got xy
> test([],[], optiona=5, optionb="xyz", optionc=false, optiond=3);
Error, (in test) invalid option, [optiond = 3]
>
Ein- und Ausgabe mit Dateien
Ein- und Ausgabe im Worksheet
> restart:
> lprint(x^2, 1/3);
x^2, 1/3
> print(x^2,1/3):
> i:=3: f:=evalf(sqrt(3)):
> printf(`i hat den Wert %d, f hat den Wert % f`, i, f);
i hat den Wert 3, f hat den Wert 1.732051
> writeline(terminal, `abc`, `def`):
abc
def
>
printf(`Geben Sie eine Zeichenkette ein: `):
s:=readline(terminal);
Geben Sie eine Zeichenkette ein:
> hallo
> s:=readstat(`Geben Sie ein Maple-Kommando ein: `);
Geben Sie ein Maple-Kommando ein: sqrt(x);
Textdateien lesen
> fn:=fopen(`data.txt`, READ):
> s:=readline(fn):
>
while(s<>0) do:
lprint(s):
s:=readline(fn):
end do:
fclose(fn):
"1 2 3"
"4 5"
"6.6 7 8e-8 9"
>
fn:=fopen(`data.txt`, READ):
readdata(fn);
fclose(fn):
> readdata(`data.txt`, integer, 2);
> readdata(`data.txt`, float, 4);
>
fn:=fopen(`data.txt`, READ):
while(not feof(fn)) do
lst:=fscanf(fn, `%f %f %f`):
lprint(lst):
end do:
fclose(fn):
[1., 2., 3.]
[4., 5., 6.6]
[7., .8e-7, 9.]
Textdateien schreiben
> restart:
>
f1:=fopen(`data.txt`,READ):
f2:=fopen(`datacopy.txt`, WRITE):
s:=readline(f1):
while(s<>0) do:
writeline(f2,s):
s:=readline(f1):
end do:
fclose(f1): fclose(f2):
>
f1:=fopen(`data.txt`,READ):
f2:=fopen(`datacopy.txt`, WRITE):
while(not feof(f1)) do:
s:=readline(f1):
fprintf(f2, `%s\n`, s):
end do:
fclose(f1): fclose(f2):
Binärdateien
> writebytes(`datacopy.txt`,readbytes(`data.txt`,infinity));
>
read und save
>
a:=array([[1,2], [2.3, 1/3]]):
save a, `test.txt`:
> a:='a':
> read `test.txt`:
> print(a);
>
>
Zeichenketten (Strings)
> str:="abcde" || "fghikl";
> length(str);
> convert(2/3, string);
> evalf(%);
> parse(%);
> evalf(%);
> substring(str, 3..5);
> substring(str, -3..-1);
> searchtext("efg", str);
> searchtext("efg", str, 1..6);
> parse("diff(sin(x),x)");
> parse("diff(sin(x),x)",statement);
> sscanf("1 2.2 3.3", "%d %d %d");
> sscanf("1 2.2 3.3", "%f %f %f");
> sscanf("x=5,y=666", "x=%d,y=%d");
> sprintf("eine ganze Zahl %d und eine Gleitkommazahl %f", 3, 2.5);
>
Zurück zum Inhaltsverzeichnis