LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-18-2010, 12:41 AM   #1
zaxonus
Member
 
Registered: Jul 2005
Posts: 66

Rep: Reputation: 15
Variable assignment in Lisp.


Hi,

I am exploring Clisp and my previous experience is extremely limited in the LISP programming language.

My question is this :
I have a list of numbers defined by say :
(setq mylist '(34 78 5 231 873 76 43 82))

Now, for some reason I need to set the first value to 97 instead of 34
How do I do it the correct LISP way ?

If the value I want to change is the first one I can use something like :
(setq mylist (cons 97 (cdr mylist)))
But how do I do if the value I need to modify is at an arbitrary rank (for example the 39th in a long list) ?
 
Old 03-18-2010, 05:58 AM   #2
neonsignal
Senior Member
 
Registered: Jan 2005
Location: Melbourne, Australia
Distribution: Debian Bookworm (Fluxbox WM)
Posts: 1,391
Blog Entries: 54

Rep: Reputation: 360Reputation: 360Reputation: 360Reputation: 360
If you are happy to destructively modify the list, then you can use setf. For example, the following would set the 39th element to be 97:
Code:
(setf (nth 39 mylist) 97)
A more functional (non-destructive) approach could copy the list recursively, ie:
Code:
(defun nreplace (n l e)
  (cond 
    ((= n 0) (cons e (cdr l)))
    (t (cons (car l) (nreplace (- n 1) (cdr l) e)))))
(nreplace 39 mylist 97)
But these are both inefficient; if you need to do this sort of thing often, perhaps you would look at using an array instead of a list.

Last edited by neonsignal; 03-18-2010 at 06:04 AM.
 
1 members found this post helpful.
Old 03-18-2010, 06:10 AM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
Hey zaxonus

See if this helps:

http://www.lispworks.com/documentati...y/f_replac.htm
 
  


Reply



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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
about immediate and deferred variable assignment in GNU make George2 Programming 1 07-22-2011 05:55 PM
[SOLVED] [BASH] variable assignment help RaptorX Programming 2 08-30-2009 10:28 AM
trivial: bash, local variable assignment agrestic Linux - Newbie 3 01-27-2009 11:30 AM
Java String variable re-assignment Cyhaxor Programming 9 11-24-2007 03:35 PM
LISP or COMON LISP Compiler for Debian carspidey Programming 3 04-19-2006 07:46 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:16 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration