(LENGTH '(1 '(2 3) 4)) which returned 3 as I expected, so I decided to
try
(LENGTH '(1 (2 3) 4)) without the quote in front of (2 3) half expecting
an error,
but this second form also returned 3. Are there any subtleties here
that I should
be aware of that might get me into trouble later, or are these two forms
which
both return 3 really identical?
Thanks and Regards,
Jeff Stephens
Why do you "try" things at random when you have a pretty good book that will
explain this shortly? Please acquire patience -- you will need it. Read on
in the book until this is explained to you. Eagerness is good, but endeavor
to direct it whither it is most productive.
--
Erik Naggum, Oslo, Norway
Act from reason, and failure makes you rethink and study harder.
Act from faith, and failure makes you blame someone and push harder.
> but this second form also returned 3. Are there any subtleties here
> that I should be aware of that might get me into trouble later, or are
> these two forms which both return 3 really identical?
if you have encountered EQUAL (as suggested by another post up the
thread), probably you can formulate an expression that will answer this
question.
thi
I'm still a Lisp newbie but I think both Lists are NOT identical, they
only have the same length:
Actually list1 = '(1 (quote (2 3)) 4)
The difference will show up if you evaluate the following:
(eval (cadr list)) == evaluate the second element of the list.
In the first case this should return (2 3), which is the result from
evaluating
(quote (2 3)).
In the second case this should yield an error, because he will try to
evaluate
(2 3) and "2" is not a function.
Correct me if I'm wrong, didn't try this out.
Best regards...
> In the second case this should yield an error, because he will try to
> evaluate (2 3) and "2" is not a function.
No, the entire expression (which (2 3) is a part of) is protected from
evaluation, so if you wanted to call a function called 2, you could
use backquote, for example:
`(1 ,(2 3) 4)
This will yield an error if there is no function named 2. (You can
have a function named "2", by the way.)
Like this, for example:
(defun |2| () 2)
But you would have to call it like this, though:
(|2|)
If this article was confusing, just ignore it and go read your book
more thoroughly.
--
johs
but you missed the part in thelifter's post where he said (eval (cadr
list)), ie (eval (2 3))
I believe he was correct in everything he wrote.
--
Coby Beck
(remove #\Space "coby 101 @ bigpond . com")
> but you missed the part in thelifter's post where he said (eval (cadr
> list)), ie (eval (2 3))
It appears I have somehow confused elements of the post he answered to
with his post. Sorry.
--
johs
No you can't. The only things that can appear in the function position of
an expression are symbols or lambda expressions. An implementation could
provide an extension that allows numbers to be used there, but it's not
standard and I've never heard of any implementation doing so.
--
Barry Margolin, bar...@genuity.net
Genuity, Woburn, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.
> In article <lzelcgh...@unity.copyleft.no>,
> Johannes Grødem <s...@kopkillah.com> wrote:
> >`(1 ,(2 3) 4)
> >
> >This will yield an error if there is no function named 2. (You can
> >have a function named "2", by the way.)
>
> No you can't.
Sure you can. There are at least three ways to do it:
(defun \2 () ...)
(defun |2| () ...)
(setf *read-base* (+ 1 1))
(defun 2 () ...)
E.
> Sure you can. There are at least three ways to do it:
To be precise: you can't have a function whose name is the integer 2.
You can have a function whose name is a symbol whose name is "2".
(To be even more precise I would need to not talk about the names of
functions but rather work from names to function objects)
--tim
"Coby Beck" <cb...@mercury.bc.ca> skrev i en meddelelse
news:aknm2s$16p0$1...@otis.netspace.net.au...
>
> "Johannes Grødem" <s...@kopkillah.com> wrote in message
> news:lzelcgh...@unity.copyleft.no...
> but you missed the part in thelifter's post where he said (eval (cadr
> list)), ie (eval (2 3))
>
> I believe he was correct in everything he wrote.
>
> --
> Coby Beck
> (remove #\Space "coby 101 @ bigpond . com")
If Length are defined recursivily it shuld travel any sublist proberly yielding
T case there are a Cdr fo the list , anyway shuldn't a Length calculate the real
number of atoms all together .
P.C.
But acording to the spec it is not. It takes a proper sequence
and returns the number of elements it counted. A sub list is 1
element at that level.
marc
"Marc Spitzer" <ma...@oscar.eng.cv.net> skrev i en meddelelse
news:slrnamvopt...@oscar.eng.cv.net...
Now that indicate the function to be defined somhow adding one as long there are
a Cdr in the list . But don't this ask for several "Length" functions ,as
mentioned who know if one member are a function with parameters .
Still both Mapcar and Apply are also counting without checking if a member are
an atom or an symbolic expression , guess if you want the true Length or realy
need to check a list you need to rewrite the Length function, but that shuldn't
be that difficult as it shuld be easy to apply or mapcar the Length function
itself recursivly to the list.
P.C.
I wouldn't call this the `true' length. LENGTH takes a sequence
as an argument and returns the number of elements in the sequence.
The fact that some of the elements of the sequence might be aggregate
objects is unimportant.
"Joe Marshall" <prunes...@attbi.com> skrev i en meddelelse
news:cV5c9.144794$aA.31669@sccrnsc02...
You are quite right ,as long as you just need the number of sub lists with or
without functions , guess that functions can look for the present of a list with
both Car and Cdr , just guess this would be somthing like And ,while both a car
and a cdr will yield T and any other yield Nil. Case there _are_ sublists these
could proberly be defined within same function, where you then ask with "no
sublist" parameter , Or a simble call to Cdr
This is so simple to implerment with recursive functions ; I mean why not have
this as a _must_ within a Length function ---- that it carry a complete length
function, guess othervise you could use Length function in it's own redefinment
. This is Lisp
. Wonder how you could redefine Length to a Length or a (Lambda Length '(x y
......... expression ; one that automaticly redefine a Length function shuld
be allright I guess , as long as it's your own tool and you never use it ;))
Anyway Im'e quite sure that you can use Length recursivly, defing a new Length
function traveling sublists that you can call with a parameter ,isn't this kind
of things done with 3 or 5 lines Lisp code.
P.C.
P.C.
Well if you wanted a treewalker to count the number of enements go do it,
lenght does not do that. It is a simple recursive function after all.
marc
>
>
>
"Marc Spitzer" <ma...@oscar.eng.cv.net> skrev i en meddelelse
news:slrnan250...@oscar.eng.cv.net...
> In article <3d711076$0$209$edfa...@dspool01.news.tele.dk>, P.C. wrote:
> Well if you wanted a treewalker to count the number of enements go do it,
> lenght does not do that. It is a simple recursive function after all.
Right, and you are quite right if you claim that it would be meaningless to
apply functions with Mapcar ,case this was not done with lists with atleast two
Atoms ,symbols or whatever , ---- sorry about the missing &rest in the halve
Lambds example btw. Anyway traveling sublists aplying functions ,don't need a
parameter as any function call can be pointet with any symbolic represantation
as we all know ,but I only point to this off-topic issue, as then Length still
don't count each single Atom ;))
P.C.
What would you want it to return for something like this:
#(a b (c #(d e) f) "foo")
This is a sequence of four elements, two of which are themselves sequences.
What precisely does quote do, Jeff? It takes in parameters, and how
does it transform them?
Cheers,
Robert
"Joe Marshall" <prunes...@attbi.com> skrev i en meddelelse
news:4%8c9.255004$983.528907@rwcrnsc53...
>
> What would you want it to return for something like this:
>
> #(a b (c #(d e) f) "foo")
>
> This is a sequence of four elements, two of which are themselves sequences.
Eh, ------- now in the little Lisp I know, I would rather say
(defun n-length (x)
(cond
((Cdr x) ( + 1 (n-length (cdr x))))
( T (+ 1 ))
))
Anyway this don't seem to work ,as if I set "A" like this ;
(setq a '(a c (a b) a))
And evaluate (n-length a) then n-length yield 4.
P.C.
http://groups.yahoo.com/group/skuespilhus/
>
>
>
The point I think you are missing is that lenght is defind to not
count every atom. It is defined to count every element in a proper
sequence.
So you are pointing out that lenght behaves *exactly* as the standard
defines it shall. It seems to me that your major point is a complete
waste of everyones time.
marc
There are indeed two cases to handle, but these are not it.
All you care about is whether or not the input list is empty,
in other words whether or not it is the symbol NIL.
(defun my-stupidly-recursive-length-function-that-only-works-on-lists (list)
(if (null list)
0
(1+ (my-stupidly-recursive-length-function-that-only-works-on-lists
(rest list)))))