Subject: Re: schem function
From: (Rob Warnock)
Date: 1999/11/11
Newsgroups: comp.lang.scheme
Message-ID: <80e8hq$>
Laila Taher  <> wrote:
| write afunction to take a list as its as argument and to
|  return a new list in which the former last element becomes
| the first .if the  function name is rotate right then for example
| (rotate-right '(a b c)->'(c a b)

Sure, it's trivial:

	> (define (rotate-right x)
	   (cons (car (reverse x)) (reverse (cdr (reverse x)))))
	> (rotate-right '(a b c))
	(c a b)

Now you *are* going to tell your professor that someone else
worked the problem for you, aren't you?

Of course, the above is a "correct" but *terrible* solution.
If you want some credit for your own work, rewrite the above
to call "reverse" fewer times (or even not at all).

For even more credit, extend the solution to take optional
parameters for the direction and number of rotations, defaulting
to "right" and "1", e.g.:

	> (define (rotate x . rest) fill this in...)
	> (rotate '(a b c d e f))
	(f a b c d e)
	> (rotate '(a b c d e f) 3)
	(d e f a b c)
	> (rotate '(a b c d e f) 'left 2)
	(c d e f a b)

[If you prefer, use just one extra arg, a number which is positive
for "right" and negative for "left".]


Rob Warnock, 8L-846
Applied Networking
Silicon Graphics, Inc.		Phone: 650-933-1673
1600 Amphitheatre Pkwy.		FAX: 650-933-0511
Mountain View, CA  94043	PP-ASEL-IA