Skip to content

Instantly share code, notes, and snippets.

@lispm
Last active January 15, 2023 09:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lispm/d83221464cba99e92550e429cdd8d0b4 to your computer and use it in GitHub Desktop.
Save lispm/d83221464cba99e92550e429cdd8d0b4 to your computer and use it in GitHub Desktop.
; 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