First, is if I have a string that names a function, is there any way
to funcall a string, or convert a string into a function object?
Second, (max 2 3 4 2 2) would return 4. If I had (2 3 4 2 2) as a
list, I would naturally use (apply '(2 3 4 2 2)) to the same effect.
Is there any other way to do this? I am basically wondering how that
list can be spliced into 2 3 4 2 2 not as a list, which can just be
fed as parameters to max. Thanks. Also, I have no specific use for
this, but was just wondering, so the functions and numbers chosen were
arbitrary.
dstein64 wrote:
> First, is if I have a string that names a function, is there any way
> to funcall a string, or convert a string into a function object?
[1]> (funcall (read-from-string "+") 4 5)
9
> Second, (max 2 3 4 2 2) would return 4. If I had (2 3 4 2 2) as a
> list, I would naturally use (apply '(2 3 4 2 2)) to the same effect.
> Is there any other way to do this? I am basically wondering how that
> list can be spliced into 2 3 4 2 2 not as a list, which can just be
> fed as parameters to max.
[2]> (let ((a '(2 3 4 2 2)))
(eval `(max ,@a)))
4
(using EVAL works, but if you use it in real code, then you are
probably doing something wrong)
If you knew the list at compile time, you could use a macro.
On Apr 9, 10:05 am, dstein64 <········@gmail.com> wrote:
> First, is if I have a string that names a function, is there any way
> to funcall a string, or convert a string into a function object?
> Second, (max 2 3 4 2 2) would return 4. If I had (2 3 4 2 2) as a
> list, I would naturally use (apply '(2 3 4 2 2)) to the same effect.
> Is there any other way to do this? I am basically wondering how that
> list can be spliced into 2 3 4 2 2 not as a list, which can just be
> fed as parameters to max. Thanks. Also, I have no specific use for
> this, but was just wondering, so the functions and numbers chosen were
> arbitrary.
1)
;; Funcall
CL-USER> (funcall (intern "LIST") 1 2)
(1 2)
;; Convert a string to a function object
CL-USER> (defmacro string-to-function (string)
`(function ,(intern string)))
STRING-TO-FUNCTION
CL-USER> (string-to-function "LIST")
#<FUNCTION LIST>
2)
;; As a list
CL-USER> (reduce #'max '(2 3 4 2 2))
;; Not a a list
CL-USER> (defun max2 (&rest args)
(apply #'max args))
MAX2
CL-USER> (max2 2 3 4 2 2)
4
:-]
From: Kent M Pitman
Subject: Re: Two Questions about Functions
Date:
Message-ID: <u1w5fej5g.fsf@nhplace.com>
dstein64 <········@gmail.com> writes:
> First, is if I have a string that names a function,
You can't. This statement suggests a misconception.
Strings don't name functions.
You might have a string that "names a symbol in a given package that
has a function definition" (e.g., as "MAX" is the name of a symbol in
the "COMMON-LISP" (or "CL") package; CL:MAX is a symbol, not a string,
and is the name of the function; there might be other symbols named
"MAX" in other packages that do other things and do not necessarily
name that function.
(Make sure you know that the reader translates to uppercase internally
by default, so the symbol max is seen by the reader as if you typed MAX
and names the symbol "MAX" (not "max") in the "CL" (not "cl") package.
In general, the internals of Common Lisp packages and symbols are
case-sensitive even if the reader does case translation by default ... and
all pre-defined names are all-uppercase.)
> is there any way to funcall a string,
> or convert a string into a function object?
Strings can't be funcalled. There are operations that allow you to look
up string names in packages (which can also be named by strings but which
are not themselves strings) and get a symbol from the lookup. See
FIND-SYMBOL and INTERN, for example.
You'll also want to read about FUNCTION (a special form) and
SYMBOL-FUNCTION (an ordinary function).
> Second, (max 2 3 4 2 2) would return 4. If I had (2 3 4 2 2) as a
> list, I would naturally use (apply '(2 3 4 2 2)) to the same effect.
(apply #'max '(2 3 4 2 2)), I assume you mean.
> Is there any other way to do this?
REDUCE is usually preferrable to APPLY for things like this.
It doesn't have a maximum length on the list.
> I am basically wondering how that
> list can be spliced into 2 3 4 2 2 not as a list, which can just be
> fed as parameters to max. Thanks. Also, I have no specific use for
> this, but was just wondering, so the functions and numbers chosen were
> arbitrary.
As others have suggested, you might want to learn about the backquote
facility.
But also, it's worth reading about REDUCE.
Your real bottleneck is your understanding of the relationship of strings
to symbols to packages to function/value cells, though.
Good luck.