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);

1, sqrt(2), [a, b, c]

> 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);

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);

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);

sqrt(6)

> 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]);

[1, 2, Pi]

Schleifen und Verzweigungen

> for x from 1 to 3 do
print(x);
end do:

1

2

3

> for x from 5 to 1 by -2 do
print(x):
end do:

5

3

1

> for i in [1,2,3] do
print(i)
end do:

1

2

3

> x:=0:
while (x<3) do
x:=x+1:
end do;

x := 1

x := 2

x := 3

> x:=0:
while is(x<=Pi) do:
print(x):
x:=x+Pi/4:
end do:

0

1/4*Pi

1/2*Pi

3/4*Pi

Pi

> x:=0:
do
x:=x+1:
if x>3 then break; end if;
print(x):
end do:

1

2

3

>

Variablen: Bindung, Auswertung, Gültigkeit

> test:=proc()
local i:
i:= 5;
end proc:

> i:=0: test();

5

> i;

0

> test:=proc()
global i:
i:=5;
end proc:

> i:=0: test();

5

> i;

5

> 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);

.3333333333

> _fn_digits:=20: fn(1/3);

.33333333333333333333

> gerade:=proc(m,b)
proc(x)
m*x+b;
end proc:
end proc:

> g1:=mal_x_plus_z(1/2,-1);

g1 := proc (y) y*(1/2)+(-1) end proc

> g1(6);

2

> op(1,eval(g1));op(7,eval(g1));

y

x, 1/2, z, -1

> plot(g1,-5..6,scaling=constrained);

[Maple Plot]

> 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);

z1 := proc () a := a+1 end proc

z2 := proc () a := a+1 end proc

> seq([z1(),z2()],i=1..5);

[1, 10], [2, 11], [3, 12], [4, 13], [5, 14]

> eval(op(7,(eval(z1))));eval(op(7,(eval(z2))));

a, 5

a, 14

> 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 := TABLE([empty = leer, pop = raus, push = rein]...

> s1[empty]();

true

> seq(s1[push](i^2),i=1..5);

true, true, true, true, true

> seq(s1[pop](),i=1..5);

25, 16, 9, 4, 1

>

Variablenauswertung

Interaktiv: vollständige Auswertung

> restart:

> a:=b:
b:=c:
a+1;
eval(a+1,1);

c+1

b+1

Bei lokalen Variablen nur eine Stufe

> restart:
test:=proc()
local a,b,c:
a:=b: b:=c: a+1;
end:

> test();

b+1

Parameter per Referenz übergeben

> restart:

> test:=proc(x::evaln)
x:=5;
end:

> a:=3: test(a): a;

5

> 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;

4

> a:=sin(b): test(a): a;

sin(b)+1

Ändern eines Feld-Elements

> test:=proc(x::array)
x[1]:=1:
end:

> a:=array([0,0]): test(a):
print(a);

VECTOR([1, 0])

procedure options

> restart:

> test:=proc(x)
options operator, arrow;
x^2;
end:

> print(test):

proc (x) options operator, arrow; x^2 end proc

> test:=proc(x)
options `Copyright mk 96`;
x^2;
end:

> op(3, op(test));

`Copyright mk 96`

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);

1.64480907466040092

> evalhf(test(8000));

1.64480907466040272

> sum(1/n^2,n=1..infinity);

1/6*Pi^2

> evalf(%,18);

1.64493406684822644

>

> test:=proc(x::array)
x[1]:=0:
end:

>

> a:=array([1,2]):
evalhf( test(var(a)) ): print(a);

vector([0., 2.])

Definition von Operatoren

> `&op1`:=proc(x)
x^2;
end:

> &op1 a;

a^2

> `&op2`:=proc(x,y)
x^2+y^2:
end:

> a &op2 b;

a^2+b^2

> `&op3`:=proc(x,y,z)
x+y+z;
end:

> &op3(a, b, c);

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([],[]);

10,

> test([],[], optiona=5, optionb="xyz", optionc=false);

5,

> 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):

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 :=

> s:=readstat(`Geben Sie ein Maple-Kommando ein: `);

Geben Sie ein Maple-Kommando ein: sqrt(x);

s := 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):

[1., 4., 6.6]

> readdata(`data.txt`, integer, 2);

[[1, 2], [4, 5], [6]]

> readdata(`data.txt`, float, 4);

[[1., 2., 3.], [4., 5.], [6.6, 7., .8e-7, 9.]]

> 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));

24

>

read und save

> a:=array([[1,2], [2.3, 1/3]]):
save a, `test.txt`:

> a:='a':

> read `test.txt`:

> print(a);

matrix([[1, 2], [2.3, 1/3]])

>

>

Zeichenketten (Strings)

> str:="abcde" || "fghikl";

str :=

> length(str);

11

> convert(2/3, string);

> evalf(%);

> parse(%);

2/3

> evalf(%);

.6666666667

> substring(str, 3..5);

> substring(str, -3..-1);

> searchtext("efg", str);

5

> searchtext("efg", str, 1..6);

0

> parse("diff(sin(x),x)");

diff(sin(x),x)

> parse("diff(sin(x),x)",statement);

cos(x)

> sscanf("1 2.2 3.3", "%d %d %d");

[1, 2]

> sscanf("1 2.2 3.3", "%f %f %f");

[1., 2.2, 3.3]

> sscanf("x=5,y=666", "x=%d,y=%d");

[5, 666]

> sprintf("eine ganze Zahl %d und eine Gleitkommazahl %f", 3, 2.5);

>

Zurück zum Inhaltsverzeichnis