help understanding a simple function

Discussion of Common Lisp
Paul
Posts: 106
Joined: Tue Jun 02, 2009 6:00 am

Re: help understanding a simple function

Post by Paul » Sat Oct 29, 2011 5:31 pm

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."

virex
Posts: 17
Joined: Fri Oct 28, 2011 3:41 pm

Re: help understanding a simple function

Post by virex » Sat Oct 29, 2011 5:38 pm

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 :?

Philipp
Posts: 6
Joined: Tue Sep 20, 2011 2:02 am

Re: help understanding a simple function

Post by Philipp » Sun Oct 30, 2011 2:06 am

...

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))))

sycamorex
Posts: 15
Joined: Sat Oct 08, 2011 4:05 am

Re: help understanding a simple function

Post by sycamorex » Fri Nov 18, 2011 3:25 am

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.

Post Reply