Get a list of variables currently in scope

Discussion of Common Lisp
Post Reply
[email protected]
Posts: 20
Joined: Sun Nov 28, 2010 10:34 pm

Get a list of variables currently in scope

Post by [email protected] » Thu Feb 09, 2012 8:42 am

Is there a way to get a list of all of the symbols currently in scope?

For example

Code: Select all

(defun test-conditions (state)
    (let ((test-1 (list "Test 1 Name" 
                                  (lambda (state) <<<run tests on state return a value>>>)))
           (test-2 (list "Test 2 Name" 
                                  (lambda (state) <<<run tests on state return a value>>>)))
           (test-3 (list "Test 3 Name" 
                                  (lambda (state) <<<run tests on state return a value>>>)))
           (test-4 (list "Test 4 Name" 
                                  (lambda (state) <<<run tests on state return a value>>>))))
      (PROCESS RESULTS (GET-ALL-TEST-RESULTS))))
GET-ALL-TEST-RESULTS would return (test-1 test-2 test-3 test-4)

That way if I want to add a test I just add the clause in the let. Or is there a completely different way of doing this that I haven't thought of?

gugamilare
Posts: 406
Joined: Sat Mar 07, 2009 6:17 pm
Location: Brazil
Contact:

Re: Get a list of variables currently in scope

Post by gugamilare » Thu Feb 09, 2012 2:16 pm

There is no portable way of doing that. The only way to do that would be using a macro with an &env argument and inspecting it to know what symbols are in scope. However, "environments" differ from implementation to implementation. Of course, you can try to do that using tools provided by your particular implementation :)

[email protected]
Posts: 20
Joined: Sun Nov 28, 2010 10:34 pm

Re: Get a list of variables currently in scope

Post by [email protected] » Thu Feb 09, 2012 2:58 pm

Thanks. At least I am asking hard questions now! There must be a cleaner way to implement this. I will keep thinking.

crlf0710
Posts: 2
Joined: Sat Jan 21, 2012 3:34 am

Re: Get a list of variables currently in scope

Post by crlf0710 » Thu Feb 09, 2012 9:02 pm

Maybe represent those items with an a-list or something like that will be easier.

nuntius
Posts: 538
Joined: Sat Aug 09, 2008 10:44 am
Location: Newton, MA

Re: Get a list of variables currently in scope

Post by nuntius » Thu Feb 09, 2012 10:18 pm

[email protected] wrote:Or is there a completely different way of doing this that I haven't thought of?
Something like the following?

Code: Select all

(defun test-conditions (state)
  (process results
   (list
    (list "Test 1 Name" 
          (lambda (state) <<<run tests on state return a value>>>))
    (list "Test 2 Name" 
          (lambda (state) <<<run tests on state return a value>>>))
    (list "Test 3 Name" 
          (lambda (state) <<<run tests on state return a value>>>))
    (list "Test 4 Name" 
          (lambda (state) <<<run tests on state return a value>>>)))))

gugamilare
Posts: 406
Joined: Sat Mar 07, 2009 6:17 pm
Location: Brazil
Contact:

Re: Get a list of variables currently in scope

Post by gugamilare » Fri Feb 10, 2012 5:28 am

Maybe a macro:

Code: Select all

(defmacro with-tests (vars &body body)
  `(let ,vars
     (flet ((get-all-test-results ()
              (list ,@(mapcar #'first vars))))
       ,@body)))

(defun test-conditions (state)
  (with-tests ((test-1 (list "Test 1 Name"
                             (lambda (state) <<<run tests on state return a value>>>)))
               (test-2 (list "Test 2 Name"
                             (lambda (state) <<<run tests on state return a value>>>)))
               (test-3 (list "Test 3 Name"
                             (lambda (state) <<<run tests on state return a value>>>)))
               (test-4 (list "Test 4 Name"
                             (lambda (state) <<<run tests on state return a value>>>))))
    (process results (get-all-test-results))))

[email protected]
Posts: 20
Joined: Sun Nov 28, 2010 10:34 pm

Re: Get a list of variables currently in scope

Post by [email protected] » Fri Feb 10, 2012 2:00 pm

nuntius wrote:

Code: Select all

  (defun test-conditions (state)
      (process results
       (list
        (list "Test 1 Name"
              (lambda (state) <<<run tests on state return a value>>>))
        (list "Test 2 Name"
              (lambda (state) <<<run tests on state return a value>>>))
        (list "Test 3 Name"
              (lambda (state) <<<run tests on state return a value>>>))
        (list "Test 4 Name"
              (lambda (state) <<<run tests on state return a value>>>)))))
n tests on state return a value>>>))))
    (process results (get-all-test-results))))

I came up with a solution very similar to this, but move the common tests into a labels expression...

Post Reply