I know there is a way to recognize integers(eg. integerp) and other
primitive objects in lisp but is there a way to recognize a quoted object in
ansi lisp?
Thanx for any help.
--
> I know there is a way to recognize integers(eg. integerp) and other
> primitive objects in lisp but is there a way to recognize a quoted
> object in ansi lisp?
This works:
(defun quoted-p (thingy)
(and (consp thingy)
(eq (car thingy) 'cl:quote)))
But somehow I doubt this is really what you're trying to ask.
Ted
--
Edward O'Connor
·······@soe.ucsd.edu
"Edward O'Connor" <·······@soe.ucsd.edu> wrote
> > I know there is a way to recognize integers(eg. integerp) and other
> > primitive objects in lisp but is there a way to recognize a quoted
> > object in ansi lisp?
>
> This works:
>
> (defun quoted-p (thingy)
> (and (consp thingy)
> (eq (car thingy) 'cl:quote)))
>
> But somehow I doubt this is really what you're trying to ask.
Ok basically i want to find out if a s-expression is a constant. I thought
the easy way is to check if its an atom without being a variable and if not
then check if its a quoted object. If both of them fail them its not a
constant object.
Is there a easy way to find out if a s-expression is a constant expression
in lisp?
Help me out..i am kindda new to lisp.
Thanx.
"llama" <·····@yahoo.com> writes:
> Is there a easy way to find out if a s-expression is a constant expression
> in lisp?
CONSTANTP. See http://www.lispworks.com/reference/HyperSpec/Body/f_consta.htm.
--
Regards,
Alexey Dejneka
"<Zhivago> mutants tend to turn evil after a while, and go around
eating people's brains." -- #lisp
"Alexey Dejneka" <········@comail.ru> wrote
> CONSTANTP. See
http://www.lispworks.com/reference/HyperSpec/Body/f_consta.htm.
but (constantp '(1 2)) returns nil, even though '(1 2) is a constant object.
"llama" <·····@yahoo.com> writes:
> "Alexey Dejneka" <········@comail.ru> wrote
> > CONSTANTP. See
> http://www.lispworks.com/reference/HyperSpec/Body/f_consta.htm.
>
> but (constantp '(1 2)) returns nil, even though '(1 2) is a constant object.
did you actualy read and attempt to comprehend the page cited? My
personel oppinion is that you did not. Here are the examples from the
page in question:
Examples:
(constantp 1) => true
(constantp 'temp) => false
;;;; the above line maps to '(1 2)
(constantp ''temp)) => true
(defconstant this-is-a-constant 'never-changing) => THIS-IS-A-CONSTANT
(constantp 'this-is-a-constant) => true
(constantp "temp") => true
(setq a 6) => 6
(constantp a) => true
(constantp '(sin pi)) => implementation-dependent
(constantp '(car '(x))) => implementation-dependent
(constantp '(eql x x)) => implementation-dependent
(constantp '(typep x 'nil)) => implementation-dependent
(constantp '(typep x 't)) => implementation-dependent
(constantp '(values this-is-a-constant)) => implementation-dependent
(constantp '(values 'x 'y)) => implementation-dependent
(constantp '(let ((a '(a b c))) (+ (length a) 6))) => implementation-dependent
It appears that your real problem is that you have a vauge, at best,
understanding of what ' aka (quote the function ) does and what it
does to acheave those ends. Go work on quote and forget about
constantp for now.
Remember just because you come up with counter examples *that you
think are correct* but is in the examples section of the hyperspec for
constantp and behave as documented does not mean you are correct, it
is the opposite as a mater of fact.
You will do much better here if you at least give the impression of
doing some work to understand the help that is offered, if you want to
continue getting it.
go study quote
marc
From: Christopher C. Stacy
Subject: Re: recognizer in lisp?
Date:
Message-ID: <uk7e1fut5.fsf@dtpq.com>
>>>>> On Fri, 11 Apr 2003 05:20:09 GMT, Marc Spitzer ("Marc") writes:
Marc> It appears that your real problem is that you have a vauge, at best,
Marc> understanding of what ' aka (quote the function ) does
My suspicion is that this is part of a homework problem
involving pattern matching for trivial symbolic algebra.
He's trying to figure out how to write a REDUCE function.
"llama" <·····@yahoo.com> writes:
> but (constantp '(1 2)) returns nil, even though '(1 2) is a constant object.
That is because the quote is lost before CONSTANTP sees it.
Consider:
(defparameter form1 '(1 2))
;; => FORM1
(defparameter form2 ''(1 2))
;; => FORM2
form1
;; => (1 2)
form2
;; => '(1 2)
(constantp form1)
;; => NIL
(constantp form2)
;; => T
There are two evaluation levels here. First Lisp evaluates the
argument of the CONSTANTP function, here FORM2. The result is
'(1 2). CONSTANTP sees this value and checks whether it would
behave as a constant form if it were evaluated again. Yes it
would, since it begins with QUOTE.
"llama" <·····@yahoo.com> writes:
> (constantp '(1 2)) returns nil, even though '(1 2) is a constant object.
* '(1 2)
(1 2)
* (car '(1 2))
1
You did not ask whether '(1 2) was a constant form, but whether (1 2)
was? But (1 2) is syntactically invalid form:
* (1 2)
; in: LAMBDA NIL
; (1 2)
;
; caught ERROR:
; illegal function call
so it is not a good idea to ask about its constantness.
* (constantp ''(1 2))
T
* ''(1 2)
'(1 2)
* (car ''(1 2))
QUOTE
* (cadr ''(1 2))
(1 2)
Compare it with:
* (constantp '(random 10))
NIL
* (eval '(random 10))
4
* (eval '(random 10))
8
* (constantp ''(random 10))
T
* (eval ''(random 10))
(RANDOM 10)
* (eval ''(random 10))
(RANDOM 10)
--
Regards,
Alexey Dejneka
"<Zhivago> mutants tend to turn evil after a while, and go around
eating people's brains." -- #lisp
"Alexey Dejneka" <········@comail.ru> wrote
> > (constantp '(1 2)) returns nil, even though '(1 2) is a constant object.
>
> * '(1 2)
> (1 2)
> * (car '(1 2))
> 1
>
> You did not ask whether '(1 2) was a constant form, but whether (1 2)
> was? But (1 2) is syntactically invalid form:
Thanx for clarifying. I am confused about one more thing:
O.K quoted objects get evaluated to itself. So in an expression like (foo
'(1 2)), the car function should receive the argument (1 2) since '(1 2)
got evaluated to (1 2). But shouldn't the formal parameter of foo be bound
to '(1 2) and not (1 2) because otherwise lisp would think (1 2) was a
function call. Right?
What *exactly* is the formal parameter of foo, lets say a, be bound to the
above function call?
"llama" <·····@yahoo.com> writes:
> O.K quoted objects get evaluated to itself.
The form '<obj>, i.e. (QUOTE <obj>), is evaluated to <obj>.
> So in an expression like (foo
> '(1 2)), the car function should receive the argument (1 2) since '(1 2)
> got evaluated to (1 2). But shouldn't the formal parameter of foo be bound
> to '(1 2) and not (1 2)
No. The formal parameter is bound to the object, which a function
receives as an argument. When a form (FOO <arg-form>) is evaluated, the
function named FOO is called with the result of evaluating
<arg-form>. '(1 2) is evaluated to (1 2), so the functional value of
FOO is called with the argument (1 2).
> because otherwise lisp would think (1 2) was a
> function call. Right?
> What *exactly* is the formal parameter of foo, lets say a, be bound to the
> above function call?
List (1 2).
See http://www.lispworks.com/reference/HyperSpec/Body/03_aba.htm for
detailed explanation of evaluation.
--
Regards,
Alexey Dejneka
"<Zhivago> mutants tend to turn evil after a while, and go around
eating people's brains." -- #lisp
"llama" <·····@yahoo.com> wrote in message news:<············@news-int.gatech.edu>...
> "Alexey Dejneka" <········@comail.ru> wrote
> > CONSTANTP. See
> http://www.lispworks.com/reference/HyperSpec/Body/f_consta.htm.
>
> but (constantp '(1 2)) returns nil, even though '(1 2) is a constant object.
CONSTANTP is a *function* which tests whether the argument object is a
constant form---in other words a piece of Lisp source code that
behaves as a constant expression when it is evaluated.
Because CONSTANTP is a function, its argument is evaluated, so what
you are testing is not the form (QUOTE (1 2)) but the result of its
evaluation. The result of its evaluation is the list (1 2) which is an
invalid form; if you evaluate it, there is an error because 1 is not a
function name. An invalid form is something other than a constant
form, hence CONSTANTP returns NIL.