guile-user
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: get all available symbols in scheme


From: Arne Babenhauserheide
Subject: Re: get all available symbols in scheme
Date: Mon, 13 Jun 2016 23:46:20 +0200
User-agent: mu4e 0.9.16; emacs 24.5.1

Hi,

Welcome to Guile!

source liu writes:

> I wonder if there is some way to dump all available symbols in current
> enviroment(something like “dir” in python), i think it is very useful when
> you are trying new modules

> I have tried the guile reference guide as well as google,but cant find any
> clue

It’s pretty hidden. The easiest way is activating readline and just
hitting tab twice:

    echo "(use-modules (ice-9 readline))(activate-readline)" >> ~/.guile

Essentially this:

(module-map (λ (sym var) sym) (resolve-interface '(guile)))


and

    ,use ; returns (guile-user)
    ,in (guile-user) ,use ; returns module listing
    ,in (guile) ,b ; returns the bindings
    ,in ...

Essentially this:

    (map (λ (x) (cons (module-name x) (module-map (λ (sym var) sym) 
(resolve-interface (module-name x))))) (module-uses (resolve-module 
'(guile-user))))


There actually isn’t a dir function, yet…

(import (ice-9 optargs))
(import (oop goops))
(use-modules (texinfo reflection))

; define basic dir
(define* (dir #:key (all? #f))
  (if all?
      (map (λ (x) (cons (module-name x)
                        (module-map (λ (sym var) sym) (resolve-interface 
(module-name x)))))
           (module-uses (current-module)))
      (module-map (λ (sym var) sym) (current-module))))
; add support for giving the module as argument
(define-generic dir)
(define-method (dir (all? <boolean>)) (dir #:all? all?))
(define-method (dir (m <list>)) (module-map (λ (sym var) sym) 
(resolve-interface m)))
; add support for using modules directly (interfaces are also modules, so this 
catches both)
(define-method (dir (m <module>)) (module-map (λ (sym var) sym) 
(resolve-interface (module-name m))))


Now there is dir, but its output is a bit unwieldy for large modules…


You can use it this:

(dir) ; all local bindings, excluding imported modules
(dir #t) ; all available bindings, including imported modules
(dir #:all? #t) ; same as above

(dir '(ice-9 optargs)) ; all exported bindings from the (ice-9 optargs) module
; => (let-optional* define* let-keywords let-keywords* define*-public defmacro* 
defmacro*-public let-optional lambda*)

(dir (resolve-module '(ice-9 optargs)) ; all bindings in the module
; => (let-optional* parse-lambda-case %module-public-interface let-keywords 
let-keywords* define*-public defmacro* defmacro*-public *uninitialized* 
let-optional vars&inits)


I just added this to guile-basics: 
http://www.draketo.de/proj/guile-basics/#sec-2-5


Best wishes,
Arne
-- 
Unpolitisch sein
heißt politisch sein
ohne es zu merken

Attachment: signature.asc
Description: PGP signature


reply via email to

[Prev in Thread] Current Thread [Next in Thread]