max - How do I use a pair to find which of two functions will evaluate the largest value? Scheme -
basically there pair made of 2 functions , code has take pair input x find highest evaluation x , print evaluation.
receive error : car: contract violation expected: pair? given: 4
define (max x) (lambda (x) ;i wanted lambda highest suitable function (if (> (car x) (cdr x)) (car x) (cdr x)))) (define one-function (lambda (x) (+ x 1))) (define second-function (lambda (x) (+ (* 2 x) 1))) ;my 2 functions ((max (cons one-function second-function)) 4)
and functions being called? , have two parameters called x
, must have different names. try this:
(define (max f) ; must use different parameter name (lambda (x) (if (> ((car f) x) ((cdr f) x)) ; call functions ((car f) x) ((cdr f) x))))
now it'll work expected:
((max (cons one-function second-function)) 4) => 9
Comments
Post a Comment