I don't like that I have to wrap my statements in the sbcl in parentheses even if it is just one line. So instead of (print "hello"), i'd like to write print "hello" if it is just a single line like that. Is that possible?
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
A rebel wanting to defy the sacred parentheses of Lisp! I like your spirit!
What you're asking for is called "top-level forms" or "REPL syntax" customization. Unfortunately, it's not possible to completely eliminate parentheses in SBCL (or any other Lisp implementation) for single-line statements, but I'll explain why and offer a few alternatives to make your REPL experience more comfortable.
Why parentheses are mandatory:
Lisp syntax is homoiconic: Code is data, and data is represented as nested lists (s-expressions). Parentheses denote these lists. Even a single function call like print "hello" is, under the hood, a list: (print "hello"). The Lisp reader (parser) converts the text input into this internal representation.
Reader macros: Lisp has a powerful mechanism called reader macros, which allow extending the syntax. However, these macros still operate on the s-expression structure. To recognize print "hello" as a valid form, you'd need to rewrite the reader, which is not straightforward (more on this later).
Evaluation model: Lisp evaluates forms (those parenthesized expressions) in a specific way: the first element is treated as the operator (function, macro, special form), and the rest are arguments. Without parentheses, the evaluator wouldn't know what is the operator and what are the arguments.
Now, the good news! You have a few options to make your REPL experience more concise:
1. Use the * macro (almost what you want):
SBCL (and many other Lisps) provides a built-in reader macro * (asterisk) for the REPL only. It allows you to write:
lisp
!print "hello"
The ! invokes the * macro, which simply wraps the rest of the line in parentheses and evaluates it. So, !print "hello" becomes (print "hello"). Not exactly what you asked for, but close!
2. Implement a custom REPL:
You can write a simple wrapper around the SBCL REPL using the readline library (available in Quicklisp). This custom REPL can preprocess input lines, adding parentheses if needed. Here's a basic example:
This simplistic implementation doesn't handle all edge cases (e.g., strings containing parentheses), but it illustrates the idea.
3. Modify the Lisp reader (for the brave):
You can, in theory, redefine the Lisp reader by specifying a custom READTABLE. This involves:
* Creating a new readtable.
* Defining reader macros for "command-style" syntax (without parentheses).
* Setting *READTABLE* to your new readtable.
Here's a sketch (not production-ready):
lisp
(defvar *my-readtable* (copy-readtable nil))
(set-macro-character #\space
(lambda (stream char)
(declare (ignore char))
(let ((word (read stream t nil t)))
(if (symbolp word)
`(,word ,@(loop for arg = (read stream t nil t)
until (or (null arg) (eq arg #\newline))
collect arg))
word)))
nil
*my-readtable*)
;; Use your new readtable:
(let ((*readtable* *my-readtable*))
(eval (read-from-string "print \"hello\"")))
Warning: Modifying the reader can break existing code, as you're altering the fundamental syntax of Lisp. Proceed with caution!
Conclusion:
While you can't entirely eliminate parentheses, these alternatives can streamline your REPL experience:
Use ! (asterisk macro) for a similar convenience.
Implement a custom REPL wrapper for more control.
Redefine the reader (if you're feeling adventurous and aware of the risks).
Happy Lisping, and may the parentheses be ever so slightly more bearable!