Hi,
I am total novice with LISP and I have a small problem.
I don't know how to read variable amount of parameters into
DEFUN.
I would like to make a program that could read this kind of input:
(you see the search tree, under this. The program is suposed to search
the optimal path by using A* search, modified branch and bound search.)
the tree.
(C)----+
/ \
(A) +---(E)---(F)
\ /
(B)---(D)
> find_route
'af 'abc 1 2 x y z 'bde 2 4 x y z 'cef 4 3 x y z '0 (=end mark)
� � � � � � � ... and so on, amount of parameters is NOT limited
� � � � � � �
� � � � � � �___z= under estimated distance (C)-(finish node)
� � � � � �___y= under estimeted distance (B)-(finish node)
� � � � �___x= under estimeted distance (A)-(finish node)
� � � �___Distance (A)-(C) = 2
� � �____ Distance (A)-(B) = 1
� �
� �____b,c child node
�
�_____a = start node, f = finish node
The program return the answer as a list:
A B E F
So my problem at this very starting point is how to
read all the parameters in? I am having a problem
because there can be variable amount of parameters.
Regards, Jarkko
Jarkko Konola wrote:
> Hi,
> I am total novice with LISP and I have a small problem.
> I don't know how to read variable amount of parameters into
> DEFUN.
>
> I would like to make a program that could read this kind of input:
> (you see the search tree, under this. The program is suposed to search
> the optimal path by using A* search, modified branch and bound search.)
>
> the tree.
>
> (C)----
> / +AFw-
> (A) +--(E)---(F)
> +AFw- /
> (B)---(D)
>
> > find_route
> 'af 'abc 1 2 x y z 'bde 2 4 x y z 'cef 4 3 x y z '0 (=end mark)
> ... and so on, amount of parameters is NOT limited
>
> ___z= under estimated distance (C)-(finish node)
> ___y= under estimeted distance (B)-(finish node)
> ___x= under estimeted distance (A)-(finish node)
> ___Distance (A)-(C) = 2
> ____ Distance (A)-(B) = 1
>
> ____b,c child node
>
> _____a = start node, f = finish node
>
> The program return the answer as a list:
> A B E F
>
> So my problem at this very starting point is how to
> read all the parameters in? I am having a problem
> because there can be variable amount of parameters.
>
> Regards, Jarkko
The answer to your question is the parameter &rest in the function
definition. You should define:(defun find-route (&rest arguments)
... <..> )
However, a much simpler solution is to put all your arguments in a list,
say (setq maze '(af abc 1 2 x y z bde 2 4 x y z cef 4 3 x y z 0)) and pass
this SINGLE parameter to your function, so that you can define
(defun find-route (maze) <...>)
marisa <······@kabelfoon.nl> writes:
> The answer to your question is the parameter &rest in the function
> definition. You should define:(defun find-route (&rest arguments)
> ... <..> )
>
> However, a much simpler solution is to put all your arguments in a list,
> say (setq maze '(af abc 1 2 x y z bde 2 4 x y z cef 4 3 x y z 0)) and pass
> this SINGLE parameter to your function, so that you can define
> (defun find-route (maze) <...>)
Keep in mind that most implementations limit the number of &rest args,
usually to one or two hundred. If you start to get really long lists,
the second solution above is much less likely to run into trouble.
marisa <······@kabelfoon.nl> writes:
< The answer to your question is the parameter &rest in the function
< definition. You should define:(defun find-route (&rest arguments)
< ... <..> )
<
< However, a much simpler solution is to put all your arguments in a list,
< say (setq maze '(af abc 1 2 x y z bde 2 4 x y z cef 4 3 x y z 0)) and pass
< this SINGLE parameter to your function, so that you can define
< (defun find-route (maze) <...>)
You should use defvar at the toplevel (since setq at the toplevel will
make the variable visible to every function).
Consider
(setq *data* (list 1 2 3))
(defun this () *data*)
(this) => (1 2 3).
The `setq' is the same thing as defvar in the preceding, just without
the safety of `*', and the normal bookkeeping that the lisp will do
with defvar.
Also &optional is another option instead of &rest.
You could do
(defvar *maze*
'(af abc 1 2 x y z bde 2 4 x y z cef 4 3 x y z 0))
(defun frob (&optional (data *maze*))
or
(defun frob (&optional (data (list *maze*)))
...).