Other Programming Languages
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming Languages - MoreOther Programming Languages

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Get inside! Sample the range of functionality easily built with JMSL Library for Time Series Data Analysis, Heat Maps, Portfolio Optimization, Monte Carlo Simulation, Stock Price Charting and more. Download Now!
  #1  
Old April 10th, 2008, 05:36 PM
rockytrh rockytrh is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2004
Posts: 118 rockytrh User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 17 h 18 m 19 sec
Reputation Power: 5
LISP questions

Sorry for the repost, i posted my last one when I was extremly angry, thought it got locked and opened a new calmer message, but my other one wasn't locked. Sorry, it has been a long day

In Common Lisp, how do you change atoms in lists. This is probably the root of my issue, i have no idea how to edit an item in a list. The only thing i was able to think of was using car to change it, but that wouldn't really do it as i think car only returns the first element of the supplied list.

The other thing i though of was i could rebuild the list, but that didn't work for me either. I tried:
(list :a (cdr x)) ; assuming x is my list
but that returns
(a (rest of the list x))

So anyway, i need some help with how to replace a list atom.

Any help is appreciated. Thanks

Last edited by rockytrh : April 10th, 2008 at 05:42 PM.

Reply With Quote
  #2  
Old April 11th, 2008, 12:01 AM
Schol-R-LEA's Avatar
Schol-R-LEA Schol-R-LEA is offline
Commie Mutant Traitor
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Jun 2004
Location: The People's Republic of Berkeley
Posts: 1,082 Schol-R-LEA User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Schol-R-LEA User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Schol-R-LEA User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Schol-R-LEA User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Schol-R-LEA User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Schol-R-LEA User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Schol-R-LEA User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Schol-R-LEA User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Schol-R-LEA User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Schol-R-LEA User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Schol-R-LEA User rank is Lieutenant Colonel (40000 - 50000 Reputation Level) 
Time spent in forums: 3 Weeks 4 Days 10 h 16 m 50 sec
Reputation Power: 446
There are a couple of approaches to this, and which is better will depend on what you're actually trying to do by changing the list.

There are two basic approaches: replace the list with the one you want, or actually change the list itself. This may seem obvious, but there are differences; if you change the list itself, then you'll change any other lists that point to it.

The replacement approach is actually more typical of Lisps, though less so in Common Lisps than in some of it's more functional-programming oriented cousins. What you had was on the right track, but the problem is in using (LIST), which creates a new list with whatever arguments it is given. The function you would want is actually (CONS), which creates a list which has the first argument as it's (CAR) rather than a list with the argument in it. So, if you have
Code:
(setf  x '(the rest of the list x))

such that (cdr x) is (REST OF THE LIST X), then
Code:
(cons 'a '(cdr x)) 

should return (A REST OF THE LIST X)

This hasn't replaced the list yet; it just returns a new one with that value. If you're trying to stick to an FP approach, that would be fine, but to actually change x, you still need to (SETF) it to the new list:
Code:
(setf x (cons 'a (cdr x)))

which is the equivalent of the Python
Code:
x = ['a'] + x[1:]

which may seem more familiar even if you don't know Python.

Note that (CONS) actually creates what is called a 'dotted pair' out of it arguments; if the first argument is a list, such as
Code:
(cons x 'too)

then it would give something like ((A REST OF THE LIST X) . TOO)
to create a list where x is the first element and a new atom is the second, you would use (APPEND), and if the last argument is an atom, you would want to apply (LIST) to it first:
Code:
(append x (list 'too))

which returns (A REST OF THE LIST X TOO).

Now, you might be thinking that this is a lot of hassle to change something, and in fact it is. There is actually a way to change the list directly using (RPLACA):
Code:
(rplaca x 'a)

replaces the car of x with a, making x:

(A REST OF THE LIST X)

The related function (RPLACD) will change the cdr of a list. The names, like those of (CAR) and (CDR), and to be honest are a bit unfortunate; unlike the list selectors, you can't even compose them. Also, you need to watch for possible side effects:
Code:
(rplacd (cdr x) 'period))

would change x as well, giving (A REST PERIOD), which may not have been what you wanted.
__________________
Rev First Speaker Schol-R-LEA;2 JAM LCF ELF KoR KCO BiWM TGIF
#define KINSEY (rand() % 7) λ Scheme is the Red Pill
Scheme in ShortUnderstanding the C/C++ Preprocessor
Taming PythonA Highly Opinionated Review of Programming Languages for the Novice, v1.1

FOR SALE: One ShapeSystem 2300 CMD, extensively modified for human use. Includes s/w for anthro, transgender, sex-appeal enhance, & Gillian Anderson and Jason D. Poit clone forms. Some wear. $4500 obo. tverres@et.ins.gov

Last edited by Schol-R-LEA : April 11th, 2008 at 12:14 AM.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - MoreOther Programming Languages > LISP questions


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway