For Programmers: Free Programming Magazines  


Home > Archive > Scheme > November 2005 > what is wrong with this?









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author what is wrong with this?
chazzwazzer

2005-11-11, 7:02 pm

I am a computer science student.
This is my first year so my question is not so complex.

I have written a recursive function to choose the maximum element of
the list but
it seems It is wrong. can someone tell me how to write this again?

(define choose-max
(lambda (list)
(cond
((null? list) null)
((> (car list) (car (cdr list))) (cons (car list) (choose-max
(cdr list))))
(else (choose-max (cdr list))))))

matteo d'addio 81

2005-11-11, 7:02 pm


chazzwazzer ha scritto:

> I have written a recursive function to choose the maximum element of
> the list but
> it seems It is wrong. can someone tell me how to write this again?
>
> (define choose-max
> (lambda (list)
> (cond
> ((null? list) null)
> ((> (car list) (car (cdr list)))
> (cons (car list) (choose-max (cdr list))))


Here the function returns a list. The text says it should return a
number.

> (else (choose-max (cdr list))))))


matteo

Pascal Bourguignon

2005-11-11, 7:02 pm

"chazzwazzer" <yildirimmuharrem@gmail.com> writes:

> I am a computer science student.
> This is my first year so my question is not so complex.
>
> I have written a recursive function to choose the maximum element of
> the list but
> it seems It is wrong. can someone tell me how to write this again?
>
> (define choose-max
> (lambda (list)
> (cond
> ((null? list) null)
> ((> (car list) (car (cdr list)))
> (cons (car list) (choose-max (cdr list))))


And here instead of returning THE maximum element, it returns a list
with an element which is greater than another, and some other such
elements.

> (else (choose-max (cdr list))))))



You need to determine:
- the base case
- the (correct) recursion rule.

Since you want to return the maximum element of the list, you cannot
call (choose-max '()), since () has no maximum element. So your base
case is wrong (it can never occur).

Them to build the correct recursion rule, it may help to start from
the base case and up:

(choose-max (list x)) --> x ; base case

(choose-max (list x y)) --> ?

(choose-max (list x y z)) --> ?

(choose-max (list x y z a)) --> ?

then you should start to see a rule emerge, and you should be able to
write the general rule:

(chose-max l) --> ?


--
__Pascal Bourguignon__ http://www.informatimago.com/

This is a signature virus. Add me to your signature and help me to live
ruankefeng@gmail.com

2005-11-12, 9:57 pm

And this would be fine:
(define choose-max
(lambda (list)
(cond
((null? list) ())
((null? (cdr list)) (car list))
((> (car list) (car (cdr list))) (choose-max (cons
(car list)
(cddr list))))
(else (choose-max (cdr list))))))

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com