An Elisp Puzzle

Xah Lee points to this interesting tweet The three S’s of SUCCESS by Gary Fredericks and asks for something similar in Elisp. Fredericks’ code is

(map (vec "SUCCESS") [0 5 6])

which certainly isn’t Elisp.

My first attempt at Elisp is

(mapcar (apply-partially #'aref "SUCCESS") '(0 5 6))

which does the trick but outputs the results as integers

(83 83 83)

because that’s how Emacs represents characters.

If we want to print out the actual S’s we need to wrap the above in some extra code

(mapcar #'char-to-string (mapcar (apply-partially #'aref "SUCCESS") '(0 5 6)))

which yields

("S" "S" "S")

What we really need here is something like the Common Lisp char function that outputs the actual characters but, of course, Emacs can only do this my making strings. On the other hand, CL lacks the nice currying function apply-partially so I don’t see a nice succinct solution in it either.

Update Noam has a much better solution in the comments.

This entry was posted in Programming and tagged , . Bookmark the permalink.

4 Responses to An Elisp Puzzle

  1. Erez says:

    Wouldn’t it make more sense to find which letters of the word Success are an ‘s’ and print their locations?

    • jcs jcs says:

      Well, really, the whole thing is kind of silly. I just did what it looked to me the original code was doing. I’m not sure what language it’s written in (Clojure or Haskell perhaps) but it seems pretty clear it just prints the 3 S’s.

  2. Noam says:

    I think (map 'string (apply-partially #'aref "SUCCESS") [0 5 6]) would be equivalent.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>