Page 3 of 3

Re: help understanding a simple function

Posted: Sat Oct 29, 2011 5:31 pm
by Paul
virex wrote:Also if you really need to, you can use (declare (special variable)) directly after a let-form's variable definition to tell the compiler that variable is special, which allows shadowing, but prevents outside access since there is no global symbol associated with the variable, so you have a special local variable then.
If you declare a variable special, it's accessible from any other function that uses that symbol name for a variable in a context where it's special. I.e., the symbol used to name it is a "global symbol associated with the variable."

Re: help understanding a simple function

Posted: Sat Oct 29, 2011 5:38 pm
by virex
Paul wrote:
virex wrote:Also if you really need to, you can use (declare (special variable)) directly after a let-form's variable definition to tell the compiler that variable is special, which allows shadowing, but prevents outside access since there is no global symbol associated with the variable, so you have a special local variable then.
If you declare a variable special, it's accessible from any other function that uses that symbol name for a variable in a context where it's special. I.e., the symbol used to name it is a "global symbol associated with the variable."
Oh thanks. I should realy have tested that :?

Re: help understanding a simple function

Posted: Sun Oct 30, 2011 2:06 am
by Philipp
...

Code: Select all

(defun numbers (lst)                                                                             
           (setq new-lst '())                                                                             
           (cond                                                                                          
             ((numberp (car lst)) (cons (car lst) new-lst) (numbers (cdr lst)))                           
             ((null lst) new-lst)                                                                         
             (t (numbers (cdr lst)))))
Actually, to solve the task recursively, I'ld propose a solution quite similar to your attempt above.
You just don't need this new-lst variable.
That is, if you do (cons 1 ()) you get a new list, so there is no need to establish an explicit binding in numbers...
Just cons each number on the value of the recursive application until you reach nil. Then return nil. So the call tree would evaluate to sth. like this:

Code: Select all

(numbers '(1 2 a b 3 4)) => (cons 1 (cons 2 (cons 3 (cons 4 nil))))

Re: help understanding a simple function

Posted: Fri Nov 18, 2011 3:25 am
by sycamorex
I thought I'd post a quick update.

Once again, thank you everyone for the help so far. I haven't posted anything in a while. I haven't forgotten about this thread:)
The only reason for that is that I've got a very busy period at work and have to bring a lot of paperwork home so don't really
have time for lisp. I think I'll have more time in a week or so and will be able to come back to this thread.


See you soon.