Last active
November 8, 2017 19:22
-
-
Save Jach/b92d70d01ac55a07f357c41bf5c01d24 to your computer and use it in GitHub Desktop.
How to write Lisp like C...
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
| (defun reverse-string-c (string string-length) | |
| (prog ((pos 0) (half (truncate (/ string-length 2)))) | |
| START (if (= pos half) | |
| (return string)) | |
| (let ((swap-pos (- string-length pos 1))) | |
| (rotatef (schar string pos) (schar string swap-pos))) | |
| (incf pos) | |
| (go START))) | |
| (defun reverse-string (string) | |
| (reverse-string-c string (length string))) | |
| (reverse-string "hello") |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Second revision more lispy, handles 0- and 1-length strings, but still using a goto instead of a loop :)