help writing a max procedure
Posted: Wed Nov 09, 2011 1:47 pm
I am trying to write a max procedure to return the largest number in a list.
heres my code
and heres my errror
;Value: my-max
1 ]=>
;Value: l
1 ]=>
;The object 0 is not applicable.
;To continue, call RESTART with an option number:
; (RESTART 2) => Specify a procedure to use in its place.
; (RESTART 1) => Return to read-eval-print level 1.
any hints or pointers appreciated ..thanks
heres my code
Code: Select all
;;;using cond
(define my-max
(lambda ( l )
(define helper
(lambda ( hold l )
(cond
((null? (cdr l))hold)
((> (car l)(hold))
(helper (car l)(cdr l)))
(else
(helper (hold)(cdr l))))))
(helper 0 l)))
;;;using if
(define my-max
(lambda ( l )
(define helper
(lambda ( hold-value l )
(cond
((null? (cdr l)) hold-value)
(else
(if
(> (car l)(hold-value))
(helper (car l)(cdr l))
(helper (hold-value)(cdr l)))))))
(helper 0 l)))
(define l '(1 2 3 4))
(my-max l)
;Value: my-max
1 ]=>
;Value: l
1 ]=>
;The object 0 is not applicable.
;To continue, call RESTART with an option number:
; (RESTART 2) => Specify a procedure to use in its place.
; (RESTART 1) => Return to read-eval-print level 1.
any hints or pointers appreciated ..thanks