Write a recursive function to get the minimum number from a sequence of numbers.
ID: 3824760 • Letter: W
Question
Write a recursive function to get the minimum number from a sequence of numbers. Please name the function as min. This function should tell the minimum number in a given list. For example, (max ( 5 7 9 3 6)) will result in 3.
What does the following recursive Scheme function do
(define (TEST L)
(if ( null L) nil
(if ( atom L) ‘Not_A_LIST
(if (null (cdr L) ) (car L) (TEST ( cdr L )))
)
)
)
9. What does the function defined in problem 8 return when it is invoked as
(TEST ‘( (a b ) d e k ( f g h ) ) )
Explanation / Answer
[1]
; Find and return maximum number in a list
(define (max lst)
(cond [(null? lst) '()]
[(= (length input) 1) (list-ref input 0)]
[(> (list-ref input 0) (list-ref input (- (length input) 1))) (max (drop-right input 1))]
[(< (list-ref input 0) (list-ref input (- (length input) 1))) (max (cdr input))]
(else
(max (cdr input))
)
)
)
[2] First Check that List is empty or Not
Second check input is List or Not
Now Cdr (Contents of Decrement part of Register) will return a sublist containing all elements except the first.
If Remaining is NULL then Return First Element of List
Otherwise Recursive Call function with cdr sublist.
[3] Return f g h
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.