From: Usulsuspct
Subject: Reverse a List?
Date: 
Message-ID: <tatp2dguj3ngb4@corp.supernews.com>
Can someone help me here?

I am trying to write a function in scheme that will take a list as it's
arguement and then return the list in reverse order.  For example:  (reverse
'(a(b c d)e f g) returns the list (g f e(b c d) a)

I am trying to achieve this without using repla or nconc, as well as the
built-in reverse function....

I am new to lisp, and am trying to get a handle on recursive concepts....

From: Usulsuspct
Subject: Re: Reverse a List?
Date: 
Message-ID: <tavrjvesu4h098@corp.supernews.com>
Usulsuspct wrote:

> Can someone help me here?
> 
> I am trying to write a function in scheme that will take a list as it's
> arguement and then return the list in reverse order.  For example: 
> (reverse '(a(b c d)e f g) returns the list (g f e(b c d) a)
> 
> I am trying to achieve this without using repla or nconc, as well as the
> built-in reverse function....
> 
> I am new to lisp, and am trying to get a handle on recursive concepts....
> 
> 
> 
> 
please?
From: Marco Antoniotti
Subject: Re: Reverse a List?
Date: 
Message-ID: <y6cy9u8osm3.fsf@octagon.mrl.nyu.edu>
Usulsuspct <····@netsync.net> writes:

> Usulsuspct wrote:
> 
> > Can someone help me here?
> > 
> > I am trying to write a function in scheme that will take a list as it's
> > arguement and then return the list in reverse order.  For example: 
> > (reverse '(a(b c d)e f g) returns the list (g f e(b c d) a)
> > 
> > I am trying to achieve this without using repla or nconc, as well as the
> > built-in reverse function....
> > 
> > I am new to lisp, and am trying to get a handle on recursive concepts....
> > 
> > 
> > 
> > 
> please?

In Common Lisp the function can be written as

	(defun my-reverse (list) (reverse list))

Does it help? :)  Maybe not.

So, let's think.

	1 - The reverse of the empty list is the empty list
	2 - The reserve of a non empty list is the
	    reverse of the REST of the list to which we APPEND a list
	    containing only the FIRST element of the list

Does this help?  Do I get credit for your homework? :)

Cheers

-- 
Marco Antoniotti ========================================================
NYU Courant Bioinformatics Group	tel. +1 - 212 - 998 3488
719 Broadway 12th Floor                 fax  +1 - 212 - 995 4122
New York, NY 10003, USA			http://bioinformatics.cat.nyu.edu
             Like DNA, such a language [Lisp] does not go out of style.
			      Paul Graham, ANSI Common Lisp
From: Usulsuspct
Subject: Re: Reverse a List?
Date: 
Message-ID: <tb02gc5h0hmia0@corp.supernews.com>
Marco Antoniotti wrote:

> 
> Usulsuspct <····@netsync.net> writes:
> 
> > Usulsuspct wrote:
> > 
> > > Can someone help me here?
> > > 
> > > I am trying to write a function in scheme that will take a list as
> > > it's
> > > arguement and then return the list in reverse order.  For example:
> > > (reverse '(a(b c d)e f g) returns the list (g f e(b c d) a)
> > > 
> > > I am trying to achieve this without using repla or nconc, as well as
> > > the built-in reverse function....
> > > 
> > > I am new to lisp, and am trying to get a handle on recursive
> > > concepts....
> > > 
> > > 
> > > 
> > > 
> > please?
> 
> In Common Lisp the function can be written as
> 
> (defun my-reverse (list) (reverse list))
> 
> Does it help? :)  Maybe not.
> 
> So, let's think.
> 
> 1 - The reverse of the empty list is the empty list
> 2 - The reserve of a non empty list is the
> reverse of the REST of the list to which we APPEND a list
> containing only the FIRST element of the list
> 
> Does this help?  Do I get credit for your homework? :)
> 
> Cheers

No credit for you, but it does help a little.  You are correct that this is 
a homework assignment.  It is for a paradigms of programming languages 
class I am taking and it just seems pointless to me to make us do projects 
like this when the focus of the curicullum is on C++.  Part of the problem 
is to reverse the list without using the reverse function.  I doesnt make 
sense to me either to assign a problem and then limit the solutions?  If 
their are libraries that help then use them I say!

Oh well, I guess I have to learn some scheme/lisp tonight, as I have to 
turn this and another function in tommorow.  Thanks for the help guys...are 
there ANY pointers you can give me at all to help?

Thanks
From: Joe Marshall
Subject: Re: Reverse a List?
Date: 
Message-ID: <3dcf6fkf.fsf@content-integrity.com>
Usulsuspct <····@netsync.net> writes:

> No credit for you, but it does help a little.  You are correct that this is 
> a homework assignment.  It is for a paradigms of programming languages 
> class I am taking and it just seems pointless to me to make us do projects 
> like this when the focus of the curicullum is on C++.  Part of the problem 
> is to reverse the list without using the reverse function.  I doesnt make 
> sense to me either to assign a problem and then limit the solutions?  If 
> their are libraries that help then use them I say!

It is a contrived problem.

How would you do it in C, then?


-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----==  Over 80,000 Newsgroups - 16 Different Servers! =-----
From: Usulsuspct
Subject: Re: Reverse a List?
Date: 
Message-ID: <tb03n9bfrs2nd2@corp.supernews.com>
Joe Marshall wrote:

> Usulsuspct <····@netsync.net> writes:
> 
> > No credit for you, but it does help a little.  You are correct that this
> > is
> > a homework assignment.  It is for a paradigms of programming languages
> > class I am taking and it just seems pointless to me to make us do
> > projects
> > like this when the focus of the curicullum is on C++.  Part of the
> > problem
> > is to reverse the list without using the reverse function.  I doesnt
> > make
> > sense to me either to assign a problem and then limit the solutions?  If
> > their are libraries that help then use them I say!
> 
> It is a contrived problem.
> 
> How would you do it in C, then?
> 
> 
> -----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
> http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
> -----==  Over 80,000 Newsgroups - 16 Different Servers! =-----


You are correct, that's were my frustration comes in to play.  Regardless 
of what you may think I dont ussually hit the newsgroups for answers to my 
homework, I am a good (fairly good) student who ussually does all my 
homework myself.  It's just that this is the last week before spring break, 
and there are allot of other things to get done.....

In C, I would just  pop the first item of the list off and push it into 
another list, until I reached the end of the list.  I really gotta hit the 
web for some tutorials on Scheme....I will check back later if ya gotta any 
recomendations for me...

wish me luck...luck me wish   see it's not that hard....
From: Joe Marshall
Subject: Re: Reverse a List?
Date: 
Message-ID: <y9u750fc.fsf@content-integrity.com>
Usulsuspct <····@netsync.net> writes:

> In C, I would just  pop the first item of the list off and push it into 
> another list, until I reached the end of the list.  

I think this would work in Scheme or Lisp as well.






-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----==  Over 80,000 Newsgroups - 16 Different Servers! =-----
From: Marco Antoniotti
Subject: Re: Reverse a List?
Date: 
Message-ID: <y6c7l1r6pyr.fsf@octagon.mrl.nyu.edu>
Usulsuspct <····@netsync.net> writes:

> Joe Marshall wrote:
> 
> > Usulsuspct <····@netsync.net> writes:
> > 
> > > No credit for you, but it does help a little.  You are correct that this
> > > is
> > > a homework assignment.  It is for a paradigms of programming languages
> > > class I am taking and it just seems pointless to me to make us do
> > > projects
> > > like this when the focus of the curicullum is on C++.  Part of the
> > > problem
> > > is to reverse the list without using the reverse function.  I doesnt
> > > make
> > > sense to me either to assign a problem and then limit the solutions?  If
> > > their are libraries that help then use them I say!
> > 
> > It is a contrived problem.
> > 
> > How would you do it in C, then?
> > 
> > 
> > -----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
> > http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
> > -----==  Over 80,000 Newsgroups - 16 Different Servers! =-----
> 
> 
> You are correct, that's were my frustration comes in to play.  Regardless 
> of what you may think I dont ussually hit the newsgroups for answers to my 
> homework, I am a good (fairly good) student who ussually does all my 
> homework myself.  It's just that this is the last week before spring break, 
> and there are allot of other things to get done.....
> 
> In C, I would just  pop the first item of the list off and push it into 
> another list, until I reached the end of the list.

You mean like

* (defun my-reverse (list)
    (let ((reversed-list ()))
      (dolist (item list reversed-list)
         (push item reversed-list))))
MY-REVERSE
* (my-reverse '(1 2 3 4 5))
(5 4 3 2 1)
* (my-reverse '(1 2 THREE 4 5))
(5 4 THREE 2 1)
* (my-reverse '(1 2 THREE 4 "five"))
("five" 4 THREE 2 1)

I cheated a little.  This is Common Lisp and not the toy language you
were asked to program in.

Now, for extra credit, write a shorter C/C++ program that does the
same thing.  You cannot cheat.  The length of the program will be
measured by my C/C++ formatting standards (they are not much different
form GNU C formatting standards.)  Hence no "Obfuscated C".

> I really gotta hit the 
> web for some tutorials on Scheme....I will check back later if ya gotta any 
> recomendations for me...

Forget Scheme. Do the real thing: http://www.alu.org.

> wish me luck...luck me wish   see it's not that hard....

Good luck.

Cheers

-- 
Marco Antoniotti ========================================================
NYU Courant Bioinformatics Group	tel. +1 - 212 - 998 3488
719 Broadway 12th Floor                 fax  +1 - 212 - 995 4122
New York, NY 10003, USA			http://bioinformatics.cat.nyu.edu
             Like DNA, such a language [Lisp] does not go out of style.
			      Paul Graham, ANSI Common Lisp
From: Kent M Pitman
Subject: Re: Reverse a List?
Date: 
Message-ID: <sfwelvzn9fu.fsf@world.std.com>
Usulsuspct <····@netsync.net> writes:

> It is for a paradigms of programming languages 
> class I am taking and it just seems pointless to me to make us do projects 
> like this when the focus of the curicullum is on C++.  Part of the problem 
> is to reverse the list without using the reverse function.  I doesnt make 
> sense to me either to assign a problem and then limit the solutions?  If 
> their are libraries that help then use them I say!

I disagree strongly with this.

The fact is that this problem isn't about teaching you to reverse lists.
It's about teaching you the importance of how to order and accumulate
things in recursion.  Being able to set up a recursion is an important
skill, regardless of whether reversing is.  If they were teaching you
how to reverse, I'd completely agree.  But my guess is that if you 
knew how to set up a recursion, you would think this was a totally trivial
problem.  And if you don't know, then you need to learn to do this.

As to libraries, you may not be able to write some kinds of libraries
without knowing this skill.  And even as a consumer, you may not be able
to usefully aggregate the results of applying library routines without
knowing this skill.

> Oh well, I guess I have to learn some scheme/lisp tonight, as I have to 
> turn this and another function in tommorow.  Thanks for the help guys...are 
> there ANY pointers you can give me at all to help?

Someone already gave you a complete textual solution to the problem.
If you didn't get it, you need to go back and look again.
From: Usulsuspct
Subject: Re: Reverse a List?
Date: 
Message-ID: <tb09bk7fj4n448@corp.supernews.com>
Kent M Pitman wrote:

> Usulsuspct <····@netsync.net> writes:
> 
> > It is for a paradigms of programming languages
> > class I am taking and it just seems pointless to me to make us do
> > projects
> > like this when the focus of the curicullum is on C++.  Part of the
> > problem
> > is to reverse the list without using the reverse function.  I doesnt
> > make
> > sense to me either to assign a problem and then limit the solutions?  If
> > their are libraries that help then use them I say!
> 
> I disagree strongly with this.
> 
> The fact is that this problem isn't about teaching you to reverse lists.
> It's about teaching you the importance of how to order and accumulate
> things in recursion.  Being able to set up a recursion is an important
> skill, regardless of whether reversing is.  If they were teaching you
> how to reverse, I'd completely agree.  But my guess is that if you
> knew how to set up a recursion, you would think this was a totally trivial
> problem.  And if you don't know, then you need to learn to do this.
> 
> As to libraries, you may not be able to write some kinds of libraries
> without knowing this skill.  And even as a consumer, you may not be able
> to usefully aggregate the results of applying library routines without
> knowing this skill.
> 
> > Oh well, I guess I have to learn some scheme/lisp tonight, as I have to
> > turn this and another function in tommorow.  Thanks for the help
> > guys...are there ANY pointers you can give me at all to help?
> 
> Someone already gave you a complete textual solution to the problem.
> If you didn't get it, you need to go back and look again.


I dont mean to sound like I am whining or complaining...it's just a 
procrastinators rant.....I understand recursion...but it helps to 
understand the language....I am just getting confused with how am I to 
create a new list with the first element of the current list at the end....

I have been trying to get this to work with a statement similar to this:

;define a list
(Define x '(1 2 3)) 

(cons (cdr x)(car x))

this just seems to return a new list containg the list  (2 3) at the 
begining and a single atom 1 at the end?  How am I to create a new list if 
by peeling off the end with cdr and creating a new list with cons....if 
that peeled off portion is a list in itself?  

Back to the books I go....
From: Kent M Pitman
Subject: Re: Reverse a List?
Date: 
Message-ID: <sfwk85ren4q.fsf@world.std.com>
Usulsuspct <····@netsync.net> writes:

> 
> Kent M Pitman wrote:
> 
> > Usulsuspct <····@netsync.net> writes:
> > 
> > > It is for a paradigms of programming languages
> > > class I am taking and it just seems pointless to me to make us do
> > > projects
> > > like this when the focus of the curicullum is on C++.  Part of the
> > > problem
> > > is to reverse the list without using the reverse function.  I doesnt
> > > make
> > > sense to me either to assign a problem and then limit the solutions?  If
> > > their are libraries that help then use them I say!
> > 
> > I disagree strongly with this.
> > 
> > The fact is that this problem isn't about teaching you to reverse lists.
> > It's about teaching you the importance of how to order and accumulate
> > things in recursion.  Being able to set up a recursion is an important
> > skill, regardless of whether reversing is.  If they were teaching you
> > how to reverse, I'd completely agree.  But my guess is that if you
> > knew how to set up a recursion, you would think this was a totally trivial
> > problem.  And if you don't know, then you need to learn to do this.
> > 
> > As to libraries, you may not be able to write some kinds of libraries
> > without knowing this skill.  And even as a consumer, you may not be able
> > to usefully aggregate the results of applying library routines without
> > knowing this skill.
> > 
> > > Oh well, I guess I have to learn some scheme/lisp tonight, as I have to
> > > turn this and another function in tommorow.  Thanks for the help
> > > guys...are there ANY pointers you can give me at all to help?
> > 
> > Someone already gave you a complete textual solution to the problem.
> > If you didn't get it, you need to go back and look again.
> 
> 
> I dont mean to sound like I am whining or complaining...it's just a 
> procrastinators rant.....I understand recursion...but it helps to 
> understand the language....I am just getting confused with how am I to 
> create a new list with the first element of the current list at the end....
> 
> I have been trying to get this to work with a statement similar to this:
> 
> ;define a list
> (Define x '(1 2 3)) 
> 
> (cons (cdr x)(car x))
> 
> this just seems to return a new list containg the list  (2 3) at the 
> begining and a single atom 1 at the end?  How am I to create a new list if 
> by peeling off the end with cdr and creating a new list with cons....if 
> that peeled off portion is a list in itself?  
> 
> Back to the books I go....

You need to create the series of calls:

 (cons 3 (cons 2 (cons 1 '())))

To do this, you will need to create the innermost call first in order to
be able to compose its result into the next one. 

So you need to find a way to get from (1 . (2 . (3 . ()))) to
(cons 1 '()). [Hint: the () here isn't the same () as the one in the
other list.]  Having once figured out how to make (cons 1 '()), you can
figure out how to make (cons 2 that-thing-you-just made).  If you're
clever, you can figure out how to divide the first list into 

 1

and

 (2 3)

so that you can use the 1 to make the (cons 1 '()) and you can use the
(2 3) as data for later.  Once having done this, you can figure out how
to get the 2 off of (2 3) because it's going to be good fodder for making
  (cons 2 ...stuff you just made from (cons 1 '()) ...)

If you really are familiar with how to do this in C, it may help you to
know that you can rewrite any:

 for ( i=0; i<n; i++)...actions...

into

 (defun do-it (i n)
   (cond ((= i n) nil)
         (t 
          ...actions...
          (do-it (+ i 1) n))))

The thing that's different in lisp, because it's expression-oriented is
that each iteration returns a value.  And in the recursive do-it I wrote
above, the innermost call to do-it will return NIL, so the next one out will,
and so on.  So no result of the actions will be seen. But in lisp you can
rewrite this so that the innermost calls can contribute values to the outer
calls.  so, for example,

 result = 1;
 for ( i = 1; i<n; i++ ) result = result*i; 
 return result;

is effectively a way to pass data from one iteration to the next.  in lisp
this could be writtten with a communication variable the same as here, but
it's more natural to write:

 (defun do-it (i n result)
   (cond ((= i n) result)
         (t
          (do-it (+ i 1) n (* result i)))))

or 

 (defun do-it (n)
   (cond ((= n 0) 1)
         (t 
           (* n (do-it (- n 1))))))

Transforming it to this last one requires observing some things about
commutatitivy and associativity so that you can re-order the
aggregation operations (the multiplications).  It doesn't work in
general but does work in some cases.  But the point is that there are
variosu ways to allow the previous iteration to return a value
meaningful to the next iteration.

So if, as you say, you want to iterate down a list, consing  elements
onto the previous result, then you can get the previous result by employing
a pattern such as one of the above.

I've tried hard not to answer your question directly but rather to speak
to the general issue of recursion here, so you won't be robbed of the chance
to think this trhough yourself.
From: Marco Antoniotti
Subject: Re: Reverse a List?
Date: 
Message-ID: <y6cae6n6qel.fsf@octagon.mrl.nyu.edu>
Usulsuspct <····@netsync.net> writes:

> Marco Antoniotti wrote:

> > 
> > In Common Lisp the function can be written as
> > 
> > (defun my-reverse (list) (reverse list))
> > 
> > Does it help? :)  Maybe not.
> > 
> > So, let's think.
> > 
> > 1 - The reverse of the empty list is the empty list
> > 2 - The reserve of a non empty list is the
> > reverse of the REST of the list to which we APPEND a list
> > containing only the FIRST element of the list
> > 
> > Does this help?  Do I get credit for your homework? :)
> > 
> > Cheers
> 
> No credit for you, but it does help a little.  You are correct that this is 
> a homework assignment.  It is for a paradigms of programming languages 
> class I am taking and it just seems pointless to me to make us do projects 
> like this when the focus of the curicullum is on C++.

I am sorry for you.  I cannot help you, and I offer you only my deepest
sympathy for your sorry predicament :)

> Part of the problem is to reverse the list without using the reverse
> function.

If you are writing a recursive function, then you are using the
function itself.  Now, it seems to me that you are not allowed to use
the BUILT-IN reverse function.  Then call your function MY-REVERSE!

> I doesnt make 
> sense to me either to assign a problem and then limit the solutions?  If 
> their are libraries that help then use them I say!

Well, there is some educational value in that.

> Oh well, I guess I have to learn some scheme/lisp tonight, as I have to 
> turn this and another function in tommorow.  Thanks for the help guys...are 
> there ANY pointers you can give me at all to help?
> 
> Thanks


You are welcome.

Cheers

-- 
Marco Antoniotti ========================================================
NYU Courant Bioinformatics Group	tel. +1 - 212 - 998 3488
719 Broadway 12th Floor                 fax  +1 - 212 - 995 4122
New York, NY 10003, USA			http://bioinformatics.cat.nyu.edu
             Like DNA, such a language [Lisp] does not go out of style.
			      Paul Graham, ANSI Common Lisp
From: Kent M Pitman
Subject: Re: Reverse a List?
Date: 
Message-ID: <sfw8zm80xcz.fsf@world.std.com>
Usulsuspct <····@netsync.net> writes:

> Usulsuspct wrote:
> 
> > Can someone help me here?
> > 
> > I am trying to write a function in scheme that will take a list as it's
> > arguement and then return the list in reverse order.  For example: 
> > (reverse '(a(b c d)e f g) returns the list (g f e(b c d) a)
> > 
> > I am trying to achieve this without using repla or nconc, as well as the
> > built-in reverse function....
> > 
> > I am new to lisp, and am trying to get a handle on recursive concepts....
>
> please?

This sounds suspiciously like a homework assignment.

It's hard to know what to tell you that does not completely solve the
problem.  I think you need to talk to your instructor to find out how
he/she wants you to think about it and what they want you to learn
from this.

Personally, I think this is a bad problem to be doing in Lisp, or any
other written language.  I think you should be doing this in pictures
and have the answer clearly down in your head of what you are trying
to do before you sit down to code it.  Lisp notation is a good language
for expressing the result, but if you don't know what the result is 
supposed to be or how you get there, Lisp notation isn't going to help
you see that any better.
From: Tim Bradshaw
Subject: Re: Reverse a List?
Date: 
Message-ID: <nkj3dcfs2hd.fsf@tfeb.org>
Usulsuspct <····@netsync.net> writes:
> please?

Although this isn't a scheme newsgroup I feel this is a case where a
lisp1 adds a certain .. something:

(define rev
  (lambda (l)
    ((lambda (c)
       (c c l '()))
     (lambda (c l a)
       (if (null? l)
	   a
	   (c c (cdr l) (cons (car l) a)))))))

The translation into CL is fairly straightforward.

--tim
From: Liang Cheng
Subject: Re: Reverse a List?
Date: 
Message-ID: <#LYMd0ErAHA.235@cpmsnbbsa09>
hmm, this seems like a homework doesn't it...
anyways, I've done this before.  code as follows:

(defun full-reverse (x) (if (atom x) x (reverse (mapcar #'full-reverse x))))
If you don't understand it, email me for more detailed explanation.

Usulsuspct <··········@hotmail.com> wrote in message
···················@corp.supernews.com...
> Can someone help me here?
>
> I am trying to write a function in scheme that will take a list as it's
> arguement and then return the list in reverse order.  For example:
(reverse
> '(a(b c d)e f g) returns the list (g f e(b c d) a)
>
> I am trying to achieve this without using repla or nconc, as well as the
> built-in reverse function....
>
> I am new to lisp, and am trying to get a handle on recursive concepts....
>
>
>
>