, . , , . , , . , , , , . .
. n! (n ) (n)*(n-1)*(n-2)*... *(1). 0 1. ,
n! = (n)*(n-1)!.
. .
function Factorial(n:integer): integer;
Begin
if n = 0 then Factorial:= 1
else Factorial:= n * Factorial(n-1);
end;
, .
.
(defun factorial (n)
(if (= n 0) 1
(* n (factorial (- n 1)))
)
)
>(factorial 3)
, , , . , .
. TRACE: (trace factorial).
factorial .
>(factorial 3)
0 FACTORIAL > (3)
1 FACTORIAL > (2)
2 FACTORIAL > (1)
3 FACTORIAL > (0)
3 FACTORIAL < (1)
2 FACTORIAL < (1)
1 FACTORIAL < (2)
0 FACTORIAL < (6)
, , :
0 FACTORIAL > (3)
, 0 , FACTORIAL , > , ( < ), (3) FACTORIAL.
. :
function Factorial(n:integer):integer;
|
|
var i,result:integer;
Begin
result:= 1;
for i:= 1 to n do
result:= result*i;
Factorial:= result;
end;
:
(defun factorial (n)
(do ((i 1 (+ i 1)) (result 1 (* result i)))
((> i n) result)
)
. , , . .
. ( ) . , . . , :
1. .
2. .
:
type TList = ^TElemList;
TElemList = record;
Inf: char;
Next: TList;.
end;
function Find(Elem:har; List:TList):boolean;
Begin
{ , }
if (List = Nil) then Find:=false
else { }
{ }
if List^.Inf = Elem then Find:= true
{ }
else Find:= Find(Elem, List^.Next);
end;
Find : , .
:
(defun find(elem list)
(cond ((null list) nil)
((eql (car list) elem) t)
(t (find elem (cdr list)))))
, . :
1. ((null list) nil) , nil (). , .
2. ((eql (car list) elem) t) , t ().
3. (t (find elem (cdr list))) , . , .
find.
>(trace find)
FIND
>(find 3 (1 2 3 4))
0 FIND > (3 (1 2 3 4)); 1
1 FIND > (3 (2 3 4)); 2
2 FIND > (3 (3 4)); 3
2 FIND < (T); 3
1 FIND < (T); 2
0 FIND < (T); 1
T
, . , list (cdr list). (eql (car list) elem) T. T .
|
|