Last active
January 13, 2016 22:23
-
-
Save spacebat/2d535fe6c833cdd40da0 to your computer and use it in GitHub Desktop.
String successor like ruby - I guess
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 char-successor(c) | |
| "values are succesor and carry, same case for letters" | |
| (cond | |
| ((char= c #\9) (values #\0 t)) | |
| ((char= c #\Z) (values #\A t)) | |
| ((char= c #\z) (values #\a t)) | |
| ((alphanumericp c) (values (code-char (1+ (char-code c))) nil)) | |
| (t (values c t)))) | |
| (defun string-successor(s) | |
| (let ((i (1- (length s))) | |
| (s1 (copy-seq s)) | |
| (p1 (position-if #'alphanumericp s))) | |
| (when (null p1) | |
| (setf (char s1 i) (code-char (1+ (char-code (char s1 i))))) | |
| (return-from string-successor s1)) | |
| (loop (multiple-value-bind (succ carry) (char-successor (char s i)) | |
| (setf (char s1 i) succ) | |
| (if (not carry) | |
| (return-from string-successor s1) | |
| (if (= i p1) | |
| (return-from string-successor | |
| (concatenate 'string | |
| (subseq s1 0 i) | |
| (string (if (char= succ #\0) #\1 succ)) | |
| (subseq s1 (1+ i)))) | |
| (decf i))))))) |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Fixed https://www.reddit.com/r/lisp/comments/40p00f/a_little_of_ruby_in_lisp/cywo54i