Common Lisp
Online Common Lisp
main.lisp runs as a script in SBCL 2.6 (Steel Bank Common Lisp), form by form from the top. Output isn’t buffered here, so a prompt written with format shows before read-line waits for you.
Try it aboveHow Common Lisp runs here
- Versions
- SBCL 2.6
- Run
sbcl --script main.lisp- Limits per run
- 256 MB memory, 15 s of CPU time on half a core, 2 MB of output
- Time
- Up to 10 minutes a run (30 on a paid plan); waits 2 minutes for input (5 on a paid plan)
- Sandbox
- Each run gets its own Linux sandbox with no network access. Text files your program writes next to your code open as new tabs.
A Common Lisp program that reads input
This main.lisp reads some numbers with a small helper, then sums, sorts and filters them. sort is destructive, hence the copy-list:
(defun read-number (prompt) (format t "~a" prompt) (parse-integer (read-line))) (let* ((n (read-number "How many numbers? ")) (nums (loop for i from 1 to n collect (read-number (format nil "Number ~a: " i))))) (format t "Sum: ~a~%" (reduce #'+ nums)) (format t "Sorted: ~{~a~^, ~}~%" (sort (copy-list nums) #'<)) (format t "Even: ~a~%" (remove-if-not #'evenp nums)))
$ sbcl --script main.lisp How many numbers? 4 Number 1: 12 Number 2: 7 Number 3: 30 Number 4: 8 Sum: 57 Sorted: 7, 8, 12, 30 Even: (12 30 8)
Reading input in Common Lisp
- (read-line) returns the line as a string. (parse-integer (read-line)) turns it into a number, and :junk-allowed t returns NIL for bad input instead of an error.
- (read) reads one Lisp object, so typing 42 gives a number and (1 2 3) a list. Don’t use it on untrusted text.
- Arguments from Run settings are in sb-ext:*posix-argv*.
What isn’t available here
- Quicklisp: there’s no network during a run, so (ql:quickload …) can’t download libraries.
- The REPL: the script runs top to bottom and exits, and an error ends it with a backtrace instead of the debugger.
Errors you’ll see
- “The function COMMON-LISP-USER::SQURE is undefined.” A misspelt or missing function, reported when the call runs.
- “junk in string "abc"” parse-integer got text that isn’t a whole number.
- Each error comes with “While evaluating the form starting at line 2” to point at the top-level form, then a backtrace.
Questions
Which Lisp is this?
Common Lisp with SBCL 2.6, running main.lisp as a script.
Does Scheme or Clojure code run here?
No, this is Common Lisp. Racket and Clojure have their own playgrounds.
How do I read input in Lisp?
(read-line) for a line of text, (parse-integer (read-line)) for a number. Type it in the terminal while the program runs.