Google Grupper har inte längre stöd för nya Usenet-inlägg eller -prenumerationer. Historiskt innehåll förblir synligt.
Dismiss

SCHEME question

2 visningar
Hoppa till det första olästa meddelandet

Chris Manuel

oläst,
23 okt. 1998 03:00:001998-10-23
till
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

oläst,
23 okt. 1998 03:00:001998-10-23
till
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

oläst,
23 okt. 1998 03:00:001998-10-23
till

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

Chris Manuel

oläst,
23 okt. 1998 03:00:001998-10-23
till
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

oläst,
23 okt. 1998 03:00:001998-10-23
till
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

oläst,
23 okt. 1998 03:00:001998-10-23
till

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

oläst,
24 okt. 1998 03:00:001998-10-24
till
* 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 nya meddelanden