|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
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
|
|||
|
|||
|
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. |
|
#2
|
||||
|
||||
|
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 Short • Understanding the C/C++ Preprocessor Taming Python • A 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. |
![]() |
| Viewing: Dev Shed Forums > Programming Languages - More > Other Programming Languages > LISP questions |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|