Last active
January 15, 2023 09:30
-
-
Save lispm/d83221464cba99e92550e429cdd8d0b4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
; Implementing a loop via circular code in Common Lisp. | |
; | |
; This code could work with a Lisp interpreter, but not a Lisp compiler. | |
; #n= is a label for a s-expression | |
; #n# references a labeled s-expression | |
; note that we can use SBCL for this, too. We just have to switch to its interpreter. | |
#+sbcl (setf sb-ext:*evaluator-mode* :interpret) | |
(setf *print-circle* t) | |
(defun circular-code (f n) | |
#1= (progn | |
(if (zerop n) | |
(return-from circular-code 'done) | |
(progn | |
(funcall f n) | |
(decf n))) | |
#1#)) | |
#| | |
CL-USER > (circular-code 'print 10) | |
10 | |
9 | |
8 | |
7 | |
6 | |
5 | |
4 | |
3 | |
2 | |
1 | |
DONE | |
|# |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment