i need help writing a program that compares 5 numbers and returns the max, i have the code that compares 3 numbers but i don't know how to expand it to 5.

here's the code for the max of 3

(define max3
	(lambda (x y z)
		(if	(> x y)
			(if	(> y z)
				x
				(if	(> x z)
					x
					z)
			)
			(if	(> y z)
				y
				(if	(> x z)
					x
					z)
			)
		)
	)
)

Have you thought about using recursion? After all, the maximum of a list of numbers is the maximum between the first number and the maximum of the rest of the numbers.

Actually, never mind. The solution is pretty advanced for someone just learning. Here it is, for educational purposes:

(define (mymax arg . lst)
  (cond
   ((null? lst)
      arg)
   ((> arg (car lst))
    (apply mymax arg (cdr lst)))
   (#t
    (apply mymax (car lst) (cdr lst)))))

If you're allowed to use max3 in your definition of max5, you could save a lot of typing, though...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.