help writing a max procedure

Discussion of Scheme and Racket

help writing a max procedure

Postby MadMuppet006 » 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
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
MadMuppet006
 
Posts: 6
Joined: Wed Nov 02, 2011 11:56 pm

Re: help writing a max procedure

Postby sepuku » Wed Nov 09, 2011 3:48 pm

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.
sepuku
 
Posts: 12
Joined: Thu May 12, 2011 11:00 am

Re: help writing a max procedure

Postby MadMuppet006 » Wed Nov 09, 2011 6:26 pm

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 )
  ..)

MadMuppet006
 
Posts: 6
Joined: Wed Nov 02, 2011 11:56 pm

Re: help writing a max procedure

Postby saulgoode » Thu Nov 10, 2011 2:12 am

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) )
saulgoode
 
Posts: 45
Joined: Tue Dec 14, 2010 1:39 am

Re: help writing a max procedure

Postby MadMuppet006 » Thu Nov 10, 2011 1:33 pm

yeah just had a look at it this morning .. makes sense :)

thanks for the help ..
MadMuppet006
 
Posts: 6
Joined: Wed Nov 02, 2011 11:56 pm


Return to Scheme

Who is online

Users browsing this forum: No registered users and 2 guests

cron