This is a read-only archive of lispforum.com. The forum was locked to new users and posts and is preserved here as static HTML from a database snapshot taken on 2019-09-07.

Get a list of variables currently in scope

7 posts · 3270 views

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

For example
(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?

Re: Get a list of variables currently in scope

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

Re: Get a list of variables currently in scope

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

Re: Get a list of variables currently in scope

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

Re: Get a list of variables currently in scope

[email protected] wrote:Or is there a completely different way of doing this that I haven't thought of?
Something like the following?
(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>>>)))))

Re: Get a list of variables currently in scope

Maybe a macro:
(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))))

Re: Get a list of variables currently in scope

nuntius wrote:
  (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...