Exemple de base
Produit des n premiers entiers
> Produit:=proc(n)
local i,s;
s:=1;
for i from 1 to n do
s:=s*i
od;
RETURN(s)
end:
> Produit(5);
Options trace et remember
Option trace
> ProduitTra:=proc(n)
local i,s;
option trace;
s:=1;
for i from 1 to n do
s:=s*i
od;
RETURN(s)
end:
> ProduitTra(5);
{‐‐> enter ProduitTra, args = 5
<‐‐ exit ProduitTra (now at top level) = 120}
Option remember
> ProduitRem:=proc(n)
local i,s;
option remember;
s:=1;
for i from 1 to n do
s:=s*i
od;
RETURN(s)
end:
> print("temps1",time());ProduitRem(1000):print("temps2",time());
> print("temps1",time());ProduitRem(1000):print("temps2",time());
>
Variable locales et globales
Variable locales
> ProduitVL:=proc(n)
# J'oublie de déclarer les variables internes à la procédure local i,s;
s:=1;
for i from 1 to n do
s:=s*i
od;
RETURN(s)
end:
Warning, `s` is implicitly declared local
Warning, `i` is implicitly declared local
> ProduitVL(5);i;s;
Variable globales
> ProduitGL:=proc(n)
# Je déclare les variables globales
global i,s;
s:=1;
for i from 1 to n do
s:=s*i
od;
RETURN(s)
end:
> ProduitGL(5);i;s;
Variable locales et globales
> ProduitVLGL:=proc(n)
local i,s;
global suite;
s:=1;
for i from 1 to n do
s:=s*i
od;
suite.n:=s;
RETURN(s)
end:
> ProduitVLGL(5);
> suite5;
> suite4;
P aramètres par valeur uniquement
Récursivité
Un exemple simple
> ProduitRec:=proc(n)
if n=1 then
RETURN(1)
else
RETURN(n*ProduitRec(n-1))
fi
end:
> ProduitRec(5);
> ProduitRecTr:=proc(n)
option trace;
if n=1 then
RETURN(1)
else
RETURN(n*ProduitRecTr(n-1))
fi
end:
> ProduitRecTr(5);
{‐‐> enter ProduitRecTr, args = 5
{‐‐> enter ProduitRecTr, args = 4
{‐‐> enter ProduitRecTr, args = 3
{‐‐> enter ProduitRecTr, args = 2
{‐‐> enter ProduitRecTr, args = 1
<‐‐ exit ProduitRecTr (now in ProduitRecTr) = 1}
<‐‐ exit ProduitRecTr (now in ProduitRecTr) = 2}
<‐‐ exit ProduitRecTr (now in ProduitRecTr) = 6}
<‐‐ exit ProduitRecTr (now in ProduitRecTr) = 24}
<‐‐ exit ProduitRecTr (now at top level) = 120}
On peut faire plusieurs appels
> Fibo:=proc(n)
option trace;
if (n=0) or (n=1) then
RETURN(1)
else
RETURN(Fibo(n-1)+Fibo(n-2))
fi
end:
> Fibo(3);
{‐‐> enter Fibo, args = 3
{‐‐> enter Fibo, args = 2
{‐‐> enter Fibo, args = 1
<‐‐ exit Fibo (now in Fibo) = 1}
{‐‐> enter Fibo, args = 0
<‐‐ exit Fibo (now in Fibo) = 1}
<‐‐ exit Fibo (now in Fibo) = 2}
{‐‐> enter Fibo, args = 1
<‐‐ exit Fibo (now in Fibo) = 1}
<‐‐ exit Fibo (now at top level) = 3}
> FiboRe:=proc(n)
option trace,remember;
if (n=0) or (n=1) then
RETURN(1)
else
RETURN(FiboRe(n-1)+FiboRe(n-2))
fi
end:
> FiboRe(3);
{‐‐> enter FiboRe, args = 3
{‐‐> enter FiboRe, args = 2
{‐‐> enter FiboRe, args = 1
<‐‐ exit FiboRe (now in FiboRe) = 1}
{‐‐> enter FiboRe, args = 0
<‐‐ exit FiboRe (now in FiboRe) = 1}
<‐‐ exit FiboRe (now in FiboRe) = 2}
value remembered (in FiboRe): FiboRe(1) ‐> 1
<‐‐ exit FiboRe (now at top level) = 3}
> FiboRe(4);
{‐‐> enter FiboRe, args = 4
value remembered (in FiboRe): FiboRe(3) ‐> 3
value remembered (in FiboRe): FiboRe(2) ‐> 2
<‐‐ exit FiboRe (now at top level) = 5}
Un autre exemple: Problème du sac à dos
> SacADos:=proc(masse,candidat)
global s;
if masse=0 then
RETURN(true)
elif masse<0 or candidat>Npoids then
RETURN(false)
elif SacADos(masse-poids[candidat],candidat+1) then
s:=s,poids[candidat];
RETURN(true)
else
RETURN(SacADos(masse, candidat+1))
fi
end:
> poids:=array(1..7,[1,2,3,4,5,6,7]):Npoids:=7:
for i from 1 to Npoids do
s:=NULL:
SacADos(12,i);print("12=",s)
od: