In: Computer Science
Hello. Please answer the following question in Scheme. Not Python, not any form of C, but in the language Scheme. If you do not know Scheme, please do not answer the question. I've had to upload it multiple times now. Thank you.
1.2 Write a function called countdown that takes a
positive integer and uses a do expression to display a sequence of
numbers counting down to 1, each on its own line, then displaying
the string "Blastoff!".
> (countdown 5)
5
4
3
2
1
Blastoff!
The code below will work for you.
(define num (read))
(define countdown (lambda (n)
(if (= n 0)
(display "Blastoff!")
(
(display n)
(newline)
(countdown (- n 1)))
)))
(countdown num)
(newline)
Explanation:
The first line of code will define a variable with name "num" and read an input from the user.
(define num (read))
then the next line define a lambda function with name "countdown()" which take "num" as an argument
(define countdown (lambda (n)
and from the next line the body of the function will start in which the first line will check if number is equals to zero, if it is true the the function will print "Blastoff!" other wise function will display the number first then decrease the value of number and recursively call it self with the new value of number.
(if (= n 0)
(display "Blastoff!")
((display n) (newline) (countdown (- n 1))))))
then the next line consist of incial call to the function countdown() with value in the variable "num"
(countdown num)
and the aditional last line will apen an extra new line in the output.
(newline)
Thank you..