LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
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 11-02-2011, 06:52 PM   #1
zaxonus
Member
 
Registered: Jul 2005
Posts: 53

Rep: Reputation: 15
Function parameters in Lisp.


Hi,

I am using Common Lisp and I wonder how I can pass parameters by address to a Lisp function.

As an example I would write something like:
(myfuntion v 18)
and the value of v would be 18 after calling the function, indepently of what it is before.

The function definition would probably look like this:

(defun myfuntion(p v) (setf p v))

But my problem is to know the right way to pass the parameters.

Thanks for any tip.
 
Old 11-03-2011, 06:57 PM   #2
markush
Senior Member
 
Registered: Apr 2007
Location: Germany
Distribution: Slackware
Posts: 3,974

Rep: Reputation: 849Reputation: 849Reputation: 849Reputation: 849Reputation: 849Reputation: 849Reputation: 849
Hello zaxonus,

I don't know if I can help, I'm learning functional programming (lisp and scheme) since some month, but with not too much advancement.
Quote:
Originally Posted by zaxonus View Post
...I am using Common Lisp and I wonder how I can pass parameters by address to a Lisp function.
This seems odd to me because "passing a parameter by adress to a function" is something like we do in C, but functional languages are very different from this approach.
Quote:
As an example I would write something like:
(myfuntion v 18)
and the value of v would be 18 after calling the function, indepently of what it is before.

The function definition would probably look like this:

(defun myfuntion(p v) (setf p v))
Maybe in this case you should use a Lisp-macro?

It may be helpful if you describe in a more detailed manner what your program does or should do respectivley. Maybe you'll find more help then.

Markus
 
1 members found this post helpful.
Old 11-05-2011, 09:27 AM   #3
neonsignal
Senior Member
 
Registered: Jan 2005
Location: Melbourne, Australia
Distribution: Debian Wheezy (Fluxbox WM)
Posts: 1,368
Blog Entries: 52

Rep: Reputation: 354Reputation: 354Reputation: 354Reputation: 354
Quote:
Originally Posted by zaxonus View Post
I am using Common Lisp and I wonder how I can pass parameters by address to a Lisp function.
While there are ways of modifying parameters in Lisp, destructive modification is usually the wrong way to program in the language. If your aim is simply to have more than one return value, then combine the return values together as a list.

There is a sense in which lists are already passed by reference. For example, the following will change the first element of the list:
Code:
(defun modify (n) (setf (car n) 18))
You can change a dynamically scoped variable by passing it in as a quoted from:
Code:
(defun modify (n) (set n 18))
(setf x 2)
(modify 'x)
Or you could make use of a lambda function to allow another function to modify outside values:
Code:
(defun modify (setn) (funcall setn 18))
(modify (lambda (y) (setq x y)))
But chances are that a reframing of the problem will make these unnecessary.
 
  


Reply

Tags
function, lisp, programming


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
[SOLVED] struggling with a function in Common Lisp sycamorex Programming 2 10-10-2011 05:04 AM
[SOLVED] LISP: displaying a function definition wje_lq Programming 1 02-03-2011 02:06 AM
Successor function in LISP AceKnocks Programming 12 04-08-2008 01:00 PM
Help: Problem writing simple LISP-function! raskol Programming 3 03-28-2008 07:20 PM
Emacs LISP - function and keymap? jantman Programming 5 09-28-2006 12:59 PM


All times are GMT -5. The time now is 06:45 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration