Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List all user-defined variables in scheme

Tags:

scheme

racket

In python I can use dir() and in racket (5.2) I can do

 (require xrepl)
 ,apropos

to get a list of all defined variables. What portable method exists to do the same in all schemes? That done, how do I filter out all the system and module variables? The full list of variables is rather daunting in racket.

like image 416
BnMcGn Avatar asked Jun 05 '12 13:06

BnMcGn


2 Answers

Well, here's how to do it in guile (v. >= 2.0):

scheme@(guile-user)> ,binding
%module-public-interface #<variable 9e55e98 value: #<interface (guile-user) 9df6678>>

scheme@(guile-user)> (define foo 'bar)

scheme@(guile-user)> ,binding
foo                     #<variable a06fe28 value: bar>
%module-public-interface #<variable 9e55e98 value: #<interface (guile-user) 9df6678>>

You can change contexts to get the bindings exported by certain modules:

scheme@(guile-user)> (use-modules (srfi srfi-1))
scheme@(guile-user)> ,module (srfi srfi-1)
scheme@(srfi srfi-1)> ,binding
reduce-right            #<variable 9ead2d0 value: #<procedure reduce-right (f ridentity lst)>>
delete                  #<variable 9eb7aa8 value: #<procedure delete (_ _ #:optional _)>>
lset-xor!               #<variable 9eb7c90 value: #<procedure lset-xor! (= . rest)>>
take!                   #<variable 9ead640 value: #<procedure take! (lst i)>>
...
like image 40
gcbenison Avatar answered Nov 13 '22 03:11

gcbenison


To get the names exported by a specific module in Racket use module->exports. For other implementations, you need to look it up in the documentation.

> (module->exports 'racket/list)
'((0
   (add-between ()) (append* ())    (append-map ())
   (argmax ())      (argmin ())     (cons? ()) (count ())
   (drop ())        (drop-right ()) (eighth ()) (empty ())
   (empty? ())      (fifth ())      (filter-map ())
   (filter-not ())  (first ())      (flatten ())
   (fourth ())      (last ())       (last-pair ())
   (make-list ())   (ninth ())      (partition ())
   (range ())       (rest ())       (second ())
   (seventh ())     (shuffle ())    (sixth ())
   (split-at ())    (split-at-right ()) (take ())
   (take-right ())  (tenth ()) (third ())))
'((0 (remove-duplicates ())))
like image 163
soegaard Avatar answered Nov 13 '22 03:11

soegaard