Slicing Words With Elisp

I was playing around with another ITA hiring puzzle today and needed to “slice” a sequence of words. That is, I wanted to form a list of strings consisting of the first letters of each word, the second letters of each word and so on. For example

hand lest more → hlm aeo nsr dte

Think of writing the words down in rows

hand
lest
more

and forming the slices by reading down each column.

This turns out to be simpler to do in Elisp than in Common Lisp (although it’s not really hard in CL). The thing that makes it easy is the string function, which takes a sequence of characters and turns them into a string:

ELISP> (string ?a ?b ?c)
"abc"

Common Lisp also has a string function but it behaves differently. Here’s how to slice words in Elisp

ELISP> (map 'list #'string "hand" "lest" "more")
("hlm" "aeo" "nsr" "dte")

This is nothing earth-shattering, of course, but it does serve as a nice example of the conciseness of Lisp in general and of Elisp in particular.

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