Solved

reverse a list

Posted on 2004-10-05
7
273 Views
Last Modified: 2010-04-17
how to reverse a list in Lisp apart of using it's buid-in function reverse????


thanks
0
Comment
Question by:nguln
  • 3
7 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 12224700
Is this a homework problem?
You probably want to use car cdr and cons
0
 
LVL 1

Expert Comment

by:swdld
ID: 12225406
please what is "list in lisp" ???
0
 
LVL 4

Expert Comment

by:Rick_Townsend
ID: 12230495
LISP is a functional programming language designed to use lists.  Lists are defined by brackets as so:
( "a" 2 "c")
and embedded as so:
("a" (1 "b") ("c" ("d")))

which is a list with three elements, where the second element is a list with two elements, and the third element is a list with two elements, the second of which is a list.  Actually, only result lists display like that.  To define a list, use the List function, like this:

(List "a" 2 "c")

where you want the list elements to be the atoms "a", 2, and "c".
0
 
LVL 4

Accepted Solution

by:
Rick_Townsend earned 20 total points
ID: 12230531
As for answering the posted question, ozo is right, use car to get the first element of the list, and cdr to get the rest of the list.  Build your result using cons, where the two parameters will be a recursive call to your reverse function and the head of the current list.
0
 
LVL 4

Expert Comment

by:Rick_Townsend
ID: 12230543
The solution is elegant, but I think EE rules are that we can't give actual homework answers, only answer questions and help with debugging your algorithm.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question