Anonymous recursion: Difference between revisions
Content added Content deleted
(Add Racket solution) |
|||
Line 1,236: | Line 1,236: | ||
( function(n) if (n <= 1) 1 else Recall(n-1)+Recall(n-2) )(n) |
( function(n) if (n <= 1) 1 else Recall(n-1)+Recall(n-2) )(n) |
||
}</lang> |
}</lang> |
||
=={{header|Racket}}== |
|||
In Racket, local helper function definitions inside of a function are only visible locally and do not pollute the module or global scope. |
|||
<lang lisp> |
|||
#lang racket |
|||
(define (fact n) |
|||
(define (fact-helper n acc) |
|||
(if (= n 0) |
|||
acc |
|||
(fact-helper (sub1 n) (* n acc)))) |
|||
(fact-helper n 1)) |
|||
;; unit tests |
|||
(module+ test |
|||
(require rackunit) |
|||
(check-equal? (fact 0) 1) |
|||
(check-equal? (fact 5) 120)) |
|||
</lang> |
|||
=={{header|REXX}}== |
=={{header|REXX}}== |