What does `#:` mean in Guile?
#:
mean in guile? with practical examples, diagrams, and best practices. Covers scheme, guile development techniques with visual explanations.Categories:
Understanding #:
in Guile Scheme: The Syntax for Keywords

Explore the purpose and usage of the #:
syntax in GNU Guile Scheme, a powerful feature for defining and using keywords in function calls and data structures.
In the world of Lisp-like languages, syntax can sometimes be concise and highly expressive, leading to powerful abstractions. GNU Guile, the official extension language for the GNU operating system, is no exception. One particular syntax that often piques the curiosity of newcomers and even seasoned developers from other languages is the #:
construct. This article delves into what #:
signifies in Guile Scheme, how it's used, and why it's a valuable part of the language.
What is #:
? Introducing Keywords
The #:
prefix in Guile Scheme is used to denote a keyword. Keywords are self-evaluating objects that are distinct from symbols and strings. They are primarily used as named parameters in function calls, as keys in hash tables or association lists, and as distinct identifiers in various data structures. Unlike symbols, keywords do not need to be quoted and evaluate to themselves. Their main purpose is to provide a clear, self-documenting way to pass options or identify specific elements.
(define (greet name #:greeting greeting-message)
(display greeting-message)
(display ", ")
(display name)
(newline))
(greet "Alice" #:greeting "Hello")
; Output: Hello, Alice
(greet "Bob" #:greeting "Greetings")
; Output: Greetings, Bob
Defining and calling a function with a keyword argument.
#:
foo is different from #:
Foo.Why Use Keywords?
Keywords offer several advantages over positional arguments or plain symbols:
- Clarity and Readability: When a function takes many optional parameters, keywords make the call site much more readable. You immediately know what each argument represents without needing to consult the function's definition.
- Flexibility: The order of keyword arguments typically doesn't matter, allowing for more flexible function signatures. You can also omit optional keyword arguments, relying on default values.
- Self-Documentation: The code becomes more self-documenting, as the keyword itself describes the parameter's purpose.
- Extensibility: Adding new optional parameters to a function often doesn't break existing code that doesn't use the new parameters, as long as they are keyword-based.
flowchart TD A[Function Call] --> B{Positional Arguments?} B -->|Yes| C[Order Matters] B -->|Yes| D[Less Readable for Many Args] B -->|No| E[Keyword Arguments] E --> F[Order Doesn't Matter] E --> G[Highly Readable] E --> H[Self-Documenting] H --> I[Uses `#:` Syntax] C --> J[Potential for Errors] D --> J
Comparison of positional vs. keyword arguments in terms of readability and flexibility.
Keywords in Data Structures
Beyond function arguments, keywords are also commonly used as keys in hash tables (also known as hash maps or dictionaries) and association lists. This provides a clear and unambiguous way to access values associated with specific identifiers.
;; Using keywords as keys in a hash table
(use-modules (ice-9 hash-tables))
(define my-config (make-hash-table))
(hash-set! my-config #:port 8080)
(hash-set! my-config #:host "localhost")
(hash-set! my-config #:debug #t)
(hash-ref my-config #:port) ; => 8080
(hash-ref my-config #:host) ; => "localhost"
;; Using keywords in an association list (alist)
(define user-profile '((#:name . "Alice") (#:age . 30) (#:status . #:active)))
(assoc #:name user-profile) ; => (#:name . "Alice")
(cdr (assoc #:age user-profile)) ; => 30
Examples of using keywords as keys in hash tables and association lists.
foo
needs to be quoted ('foo
) to prevent evaluation, whereas #:
foo evaluates to itself directly. This distinction is crucial for understanding their behavior in different contexts.