Racket Programming Language **** PLEASE ANSWER ONLY THE 1ST QUESTION USING RACKE
ID: 3882192 • Letter: R
Question
Racket Programming Language
****PLEASE ANSWER ONLY THE 1ST QUESTION USING RACKET PROGRAMMING LANGUAGE*****
If a language other than racket is used I will thumb down and report the answer.
write the following three functions using Racket. 0e. 1. (factorial n) computes n! using the following recursive definition. if n = 0 2. (factorial-sum n) computes 1!+2! +.+ n!. Here, you may use the function factorial defined in 1. (factorial-sum-1 n) computes the same sum as part 2, but uses letrec and the lambda construct to define the function factorial within the definition of power-sum1. 3. Submit a .rkt file with the definitions of these three function via Canvas by 11:59 PM on Tuesday, February 13.Explanation / Answer
#lang racket
(require racket/trace)
(define factorial
(lambda (n)
(if (= n 0)
1
(* n (factorial (- n 1))))))
(trace factorial)
here example of the output:
let n will be 5
(factorial 5)
;; Output:
;; >(factorial 5)
;; > (factorial 4)
;; > >(factorial 3)
;; > > (factorial 2)
;; > > >(factorial 1)
;; > > > (factorial 0)
;; < < < 1
;; < < <1
;; < < 2
;; < <6
;; < 24
;; <120
;; 120
Here we are using one more method called fatorial-tail method this is the function we used there
(define factorial-tail
(lambda (n acc)
(if (= n 0)
acc
(factorial-tail (- n 1) (* acc n )))))
(trace factorial-tail).
we will get the sam eoutput in both the functions.
2)sum of the factoirals
(define (my-sum x)
(if (zero? x)
0
(+ x (my-sum (- x 1)))))
3)(define (sum n)
(if (zero? n)
n
(+ n (sum (sub1 n)))))
(sum 123456789) ;
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.