Function in Scheme that shows the minimum and the maximum numbers (integers) in a list -
is there way make function in scheme receives list of numbers (integers) , creates list maximum , minimum numbers of list ?
i know how make function each maximum,
(define (mini a) (if (null? (cdr a)) (car a) (min (car a) (mini(cdr a))) ) )
and mininum:
(define (maxi a) (if (null? (cdr a)) (car a) (min (car a) (maxi(cdr a))) ) )
what want 1 function both, in simplest way possible, because i'm new paradigm.
well you're pretty close. use functions have.
(define (maximini a) (list (maxi a) (mini a)))
a big part of writing scheme programs decomposing functionality separate, reusable procedures. encapsulating min , max behaviour mixture of concerns.
if there limitation of 1 traversal
(define (min b) (if (< b) b)) (define (max b) (if (> b) b)) (define (maximini a) (let loop [(x -inf.0) (y +inf.0) (a a)] (if (empty? a) (list x y) (loop (max (car a) x) (min (car a) y) (cdr a))))) (maximini '(-4 3 2 1 10 -5)) ; => '(10 -5)
Comments
Post a Comment