Add all the A tasks
This commit is contained in:
parent
2dd7375f96
commit
051504d65b
1608 changed files with 18584 additions and 0 deletions
18
Task/Anonymous-recursion/Racket/anonymous-recursion-1.rkt
Normal file
18
Task/Anonymous-recursion/Racket/anonymous-recursion-1.rkt
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
#lang racket
|
||||
|
||||
;; Natural -> Natural
|
||||
;; Calculate factorial
|
||||
(define (fact n)
|
||||
(define (fact-helper n acc)
|
||||
(if (= n 0)
|
||||
acc
|
||||
(fact-helper (sub1 n) (* n acc))))
|
||||
(unless (exact-nonnegative-integer? n)
|
||||
(raise-argument-error 'fact "natural" n))
|
||||
(fact-helper n 1))
|
||||
|
||||
;; Unit tests, works in v5.3 and newer
|
||||
(module+ test
|
||||
(require rackunit)
|
||||
(check-equal? (fact 0) 1)
|
||||
(check-equal? (fact 5) 120))
|
||||
Loading…
Add table
Add a link
Reference in a new issue