Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

SCHEME question

2 views
Skip to first unread message

Chris Manuel

unread,
Oct 23, 1998, 3:00:00 AM10/23/98
to
HI;

I have this recursion program in Scheme and I am not sure on how it goes
about getting the answer:

(define (flip L) (if (null? L) ()
(append (flip (cdr L))
(cons (car L) ()))))

1 ]=> (flip '(a b c))

;Value 1: (c b a)

can anyone explain to me how this reverses what u pass as parameters?
Thanks

Chris Manuel

Rainer Joswig

unread,
Oct 23, 1998, 3:00:00 AM10/23/98
to
In article <3630922C...@acadiau.ca>, Chris Manuel
<031...@acadiau.ca> wrote:

I have next week's homework task for you:

Please write a "better" implementation and explain your solution.

--
http://www.lavielle.com/~joswig

Chris Manuel

unread,
Oct 23, 1998, 3:00:00 AM10/23/98
to

Umm, I dont know why u posted that under my article.

Chris Manuel

unread,
Oct 23, 1998, 3:00:00 AM10/23/98
to
Windsor wrote:
>
> In article <3630922C...@acadiau.ca>, 031...@acadiau.ca says...

> > HI;
> >
> > I have this recursion program in Scheme and I am not sure on how it goes
> > about getting the answer:
> >
> > (define (flip L) (if (null? L) ()
> > (append (flip (cdr L))
> > (cons (car L) ()))))
> >
> > 1 ]=> (flip '(a b c))
> >
> > ;Value 1: (c b a)
> >
> > can anyone explain to me how this reverses what u pass as parameters?
> > Thanks
> >
> > Chris Manuel
> >
> Here is an explanation in LISP fragments:
>
> I assume you understand cdr, car, cons do.
>
> 1. (append (flip (b c)) (a))
> 2. (append (flip (c)) (b))
> 3. (append (flip ()) (c))
> 4. (cond ((null ()) ())) <= end the recursion and start
> returning "up"
>
> Line 4 returns nil.
> Line 3 appends nil and (c) => (c)
> Line 2 appends (c) and (b) => (c b)
> Line 1 appends (c b) and (a) => (c b a)

So basically when you (append (flip (cdr L) L gets replaced by (b c)
then eventually works down to L being ()?
And the append funtion works by appending c then b then a, therefore
giving you the reverse of (a b c) which is (c b a)?

Thanks a lot!!!!

Barry Margolin

unread,
Oct 23, 1998, 3:00:00 AM10/23/98
to
In article <3630BD88...@acadiau.ca>,

Chris Manuel <031...@acadiau.ca> wrote:
>So basically when you (append (flip (cdr L) L gets replaced by (b c)
>then eventually works down to L being ()?

If you have to ask this, it suggests that you don't know some of the
simplest things about function calling in Lisp or Scheme. Before you can
understand recursion you need to understand function calling in general.

--
Barry Margolin, bar...@bbnplanet.com
GTE Internetworking, Powered by BBN, Burlington, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.

Chris Manuel

unread,
Oct 23, 1998, 3:00:00 AM10/23/98
to

Believe me I know some of the simpilest things about function calling in
Scheme. This is just a program our prof gaves us today and Im trying to
figure it out...

Erik Naggum

unread,
Oct 24, 1998, 3:00:00 AM10/24/98
to
* Chris Manuel <031...@acadiau.ca>

| Believe me I know some of the simpilest things about function calling in
| Scheme. This is just a program our prof gaves us today and Im trying to
| figure it out...

I hear they are more sympathetic to using USENET for solving students'
homework problems over in comp.lang.scheme, which is also more specific
to your homework problem. please don't tell them I sent you. thank you
for not posting homework problems to comp.lang.lisp in the future.

#:Erik
--
The Microsoft Dating Program -- where do you want to crash tonight?

0 new messages