In: Computer Science
Write a Scheme function that takes a positive integer n and returns the list of all first n positive integers in decreasing order:
(decreasing-numbers 10) (10 9 8 7 6 5 4 3 2 1)
Please explain every step
Code:
(define (decreasing-numbers n)
(cond (( = n 0) '()) ;;if n = 0 returns empty list
((= n 1) (list 1)) ;;if n = 1 returns list which contains 1 i.e,
'(1)
(else (cons n (decreasing-numbers (- n 1)))))) ;;else cons the
element and recursively call with n-1
Snapshot of Code and Output:
Snapshot of Step by Step Iteration: