LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Function parameters in Lisp. (http://www.linuxquestions.org/questions/programming-9/function-parameters-in-lisp-911561/)

zaxonus 11-02-2011 05:52 PM

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.

markush 11-03-2011 05:57 PM

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 (Post 4514474)
...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

neonsignal 11-05-2011 08:27 AM

Quote:

Originally Posted by zaxonus (Post 4514474)
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.


All times are GMT -5. The time now is 10:18 AM.