Факториал.Хвостовая рекурсия
This commit is contained in:
10
Гарифуллин Марат/README.md
Normal file
10
Гарифуллин Марат/README.md
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
# Вычисление факториала хвостовой рекурсией
|
||||||
|
## Cсылка на видеоразбор
|
||||||
|
https://youtu.be/d7vrUHXSpFo
|
||||||
|
## Код из книги
|
||||||
|
#let rec tfact x n =
|
||||||
|
if n = 0 then x
|
||||||
|
else tfact (x * n) (n - 1);
|
||||||
|
tfact : int -> int -> int = <fun>
|
||||||
|
#let fact n = tfact 1 n;;
|
||||||
|
fact : int -> int = <fun>
|
||||||
11
Гарифуллин Марат/hvost_rec.rkt
Normal file
11
Гарифуллин Марат/hvost_rec.rkt
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
#lang racket
|
||||||
|
|
||||||
|
(define (tfact x n)
|
||||||
|
(cond
|
||||||
|
[(equal? n 0) x]
|
||||||
|
[else (tfact (* x n)(- n 1))]))
|
||||||
|
|
||||||
|
(define (fact n)
|
||||||
|
(tfact 1 n))
|
||||||
|
|
||||||
|
|
||||||
Reference in New Issue
Block a user