Page 1 of 1

help writing a max procedure

Posted: Wed Nov 09, 2011 1:47 pm
by MadMuppet006
I am trying to write a max procedure to return the largest number in a list.

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)

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

Re: help writing a max procedure

Posted: Wed Nov 09, 2011 3:48 pm
by sepuku
I'm still a learner but shouldn't you use a parenthesis in (define (my-max ...

( I mean before 'my-max'.)

Also my second question might be silly but i'll ask anyway.

I believe that you don't have both procedures in the same file.Right?Cause if you are not then there could be some name conflict ... i guess. :/

Hope i helped.

Re: help writing a max procedure

Posted: Wed Nov 09, 2011 6:26 pm
by MadMuppet006
found out what I was doing wrong

putting ( ) around hold as in ((> (car l)(hold)) was causing the error ..

I tried

Code: Select all


(define my-max
  (lambda ( l )
    (define helper
      (lambda ( hold-value l )
	(define bigger
	  (lambda ( m n )
	    (if
	     (> m n)
	     m
	     n)))
	(cond
	 ((null? (cdr l))
	  (bigger hold-value (car l)))
	 (else
	  (if
	   (> (car l)hold-value)
	   (helper (car l)(cdr l))
	   (helper hold-value(cdr l)))))))
    (helper 0 l)))

and it works fine ..

as to the define statement there are two ways to do it :

Code: Select all


(define my-max
  (lambda ( n )
   .. ))
(define ( my-max n )
  ..)


Re: help writing a max procedure

Posted: Thu Nov 10, 2011 2:12 am
by saulgoode
MadMuppet006 wrote:

Code: Select all

(define my-max
  (lambda ( l )
    (define helper
      (lambda ( hold-value l )
	(define bigger
	  (lambda ( m n )
	    (if
	     (> m n)
	     m
	     n)))
	(cond
	 ((null? (cdr l))
	  (bigger hold-value (car l)))
	 (else
	  (if
	   (> (car l)hold-value)
	   (helper (car l)(cdr l))
	   (helper hold-value(cdr l)))))))
    (helper 0 l)))
and it works fine ..
It is a little strange that you define a 'bigger' function and use it for the final comparison, but do not use it for the preceding comparisons.

Personally, I would just return the 'hold-value' when the list is exhausted. This means the function will return "0" for the empty list (whereas your function will error on taking its 'car').

Code: Select all

(define (my-max lis)
  (define (helper hold-value lis)
    (if (null? lis)
      hold-value
      (helper (if (< hold-value (car lis))
                (car lis)     
                hold-value )
              (cdr lis) )))
  (helper 0 lis) )

Re: help writing a max procedure

Posted: Thu Nov 10, 2011 1:33 pm
by MadMuppet006
yeah just had a look at it this morning .. makes sense :)

thanks for the help ..