Implementing Palindrome Predicates

Joe Marshall is a long-time Lisper with an ability to come up with really elegant solutions. Indeed, the second ever Irreal post was about Marshall’s brilliant solution to a problem from SICP. Recently, he recounted a story about a recent phone screen he had in which he was asked to implement a palindrome detector. He wrote both an iterative and a recursive version. He says he gave them the iterative version so they wouldn’t think him an idiot but that he liked the recursive version better because of its simplicity and elegance.

If you think of a palindrome detector as embodying the three rules

  1. The empty string is a palindrome
  2. A string with a single character is a palindrome
  3. If the first and last characters of a string are identical and the substring between them is a palindrome, then the original string is a palindrome

the recursive algorithm writes itself.

Marshall doesn’t show his code so of course I had to try it myself. My initial effort was to bang out an Elisp implementation:

(defun palindromep (string)
(let ((len (length string)))
  (cond
   ((zerop len) t)
   ((= 1 len) t)
   ((not (char-equal (aref string 0) (aref string (1- len)))) nil)
   (t (palindromep (substring string 1 -1))))))

That’s practically a word-for-word rendition of the three rules and it worked well. But then I started thinking that Marshall said the iterative solution would be faster for most compilers. I don’t know what language he used but it almost certainly wasn’t Lisp so I wondered if the iterative version would be faster in a Lisp language. Of course I had to write an iterative version so I could compare them.

My first inclination was to write the iterative version in Elisp too but Elisp isn’t tail recursive so it would be hard to run the recursive version on large inputs. Therefore I decided to start over with Scheme. The recursive version is the same, mutatis mutandis, as the Elisp version.

(define palindrome-r?
  (lambda (string)
    (let ((len (string-length string)))
      (cond
       ((zero? len) #t)
       ((= 1 len) #t)
       ((not (char=? (string-ref string 0) (string-ref string (1- len)))) #f)
       (#t (palindrome-r? (substring string 1 (1- len))))))))

The iterative version is a little more complicated—mostly because of the do loop—but doesn’t seem nearly as elegant as the recursive version.

(define palindrome-i?
  (lambda (string)
    (do ((h 0 (1+ h))
         (t (1- (string-length string)) (1- t))
         (palindrome? #t))
        ((or (not palindrome?) (> h t))
         palindrome?)
      (set! palindrome? (char=? (string-ref string h) (string-ref string t))))))

The real test, though, is their relative speeds. In Scheme with its tail recursion, loops are generally implemented as recursion and you wouldn’t expect much difference. I ran the comparison in Guile Scheme, which has copy-on-modify string behavior so, again, the two versions seem like they should run in about the same time.

Of course, the results were nothing like that. I ran each version 1000 times on a large palindrome (100,000 x’s) with the following

(define str (make-string 100000 #\x))

(define bench
  (lambda (rcnt f)
    (do ((i rcnt (1- i)))
        ((zero? i))
      (f str))))

and here are the results

,time (bench 1000 palindrome-r?)
;; 9.204000s real time, 9.370000s run time.  2.330000s spent in GC.
,time (bench 1000 palindrome-i?)
;; 4.582000s real time, 4.660000s run time.  1.350000s spent in GC.

The recursive version runs almost exactly twice as long so Marshall was correct even in the Lisp case.

None of this is very important, of course, but it was an interesting exercise that reinforced the lesson that it’s really hard to predict run times without benchmarking.

Posted in General | Tagged , , | Leave a comment

Emacs Screencasts

Over at Meta Redux, Bozhidar Batsov, who will need no introduction to Emacs users, has a nice post on how to make a screencast from within Emacs. The result is actually an animated GIF but the results in his demo video look very good. The nice thing about his method is that it takes a screen capture only when you do something so the resulting files are smaller and there are no pauses when you’re not doing something.

The screencasts are made with the emacs-gif-screencast package that’s available on Melpa. You’ll need a couple of extra packages as well: Imagemagick and Gifsicle both of which are available on HomeBrew for MacOS users and are probably already installed on Linux—if not prebuilt binaries or source are available at the above links.

There are a few weaknesses, which Batsov discusses but nothing serious and he concludes that it’s a “pretty cool tool” so if you’re looking for any easy way to make a screencast from within Emacs you should definitely give it a look. You won’t be able to make the type of video that, say, Mike Zamansky produces but if you need a quick screencast to demo some process, emacs-gif-screencast is ideal.

Posted in General | Tagged | Leave a comment

Emacs 27 Release Branch

Eli Zaretskii writes on the Emacs Development list that he’s created the Emacs 27 release branch. Next up is the Emacs 27.1 pretest, which he hopes to release soon. He’s also set the version number of Emacs on the Master branch to 28.0.50.

Emacs 27 is finally in sight. In the meantime, we can start obsessing about Emacs 28. If you haven’t already, take a look at John Wiegley’s video on what’s coming in Emacs 27.

Posted in General | Tagged | Leave a comment

Plain Text For the Social Sciences

Regular readers know that I’m fascinated by the ways that non-technical folks make use of Emacs. Despite our prejudiced assumptions, they are often sophisticated and subtle in the ways they leverage Emacs. Recently, Eric Fraga tweeted a pointer to a long article by Kieran Healy on using plain text and Emacs for writing in the social sciences.

The article is mainly addressed to the beginning graduate school student and makes the case that if you are going to do any quantitative work in your research then you’re better off using plain text and a text editor rather than a word processor for writing your papers and books. Most Irreal readers won’t find this the slightest bit controversial and will need no convincing but social scientists are different. Their culture and training has led them to believe that writing papers is something done with Word. Their professional journals agree. With that in mind, it’s easy to see why Haley’s position can be a hard sell.

Healy is mostly concerned with workflow and although he is an Emacs user, he doesn’t insist that it’s the only good solution. He suggests several alternatives but does note that Emacs is a powerful tool that can make writing easier. He assumes that “quantitative” means “statistical” and that his readers will be using R. His workflow involves writing in Markdown and using knitr to evaluate the R code blocks and insert the results in much the same way that Org and Babel work. Org and Babel would provide a more seamless and slightly easier solution so I’m not sure why he doesn’t use them. Healy is a Full Professor at Duke so Org probably wasn’t available when he started and he’s just stuck with his “good enough” solution. The article discusses his entire tool chain with particular attention to revision control and backups so it’s about more than just what editor and markup language to use.

As I said, the article is long—a PDF version runs to 49 pages (although with generous margins)—but it’s interesting and makes many good points even if you don’t agree with all his choices of tools. I recommend reading the PDF version because it has the footnotes and is a single document rather than a series of pages.

Posted in General | Tagged | Leave a comment

John Wiegley on Emacs 27

In another of the videos from Emacs Conference 2019, John Wiegley talks about what’s coming in Emacs 27. Wiegley is still the official maintainer but Eli Zaretskii stepped up to do the heavy lifting when Wiegley’s job intervened. Wiegley says that Zaretskii is now the de facto maintainer but that he was not available to give the talk so Wiegley made the presentation.

There are several interesting changes coming. Lispers will be happy to learn that Elisp will finally be getting BIGINT support. It still doesn’t have a full numerical tower but BIGINT support is probably the most important missing piece.

On the display side, Emacs 27 is getting support for Harfbuzz, Cairo, and better ligature support. Another display-like feature is support for tabs. This is not just tabs in the browser sense—because who needs that in Emacs—but a mechanism for maintaining window configurations and switching between them.

Some other interesting enhancements are the portable dumper (a long time coming but we’re finally free of depending on glibc hacks to build our core image), initial support for the XDG directory configuration standard, so-long-mode to help deal with very long lines slowing down Emacs, and gettext integration to support internationalization of error messages and the like.

You can get more details by watching Wiegley’s talk but, unfortunately, the audio dropped out several times during his presentation. Still, every serious Emacser will want to watch the video to see what’s coming in Emacs 27.

Posted in General | Tagged | Leave a comment

A Useful Tip for the Holidays

This is way out of the usual Irreal fare but it’s, at the same time, astounding, obvious, and useful for the Christmas season:

I am among the world’s worst gift wrappers and wish I’d seen this before. At this point, though, all my gift giving is carefully arranged to avoid the need to wrap anything so I’m passing this on for those who haven’t yet reached my stage of infirmity.

Via Karl Voit.

UPDATE [2019-12-21 Sat 13:51]: Someone has seen fit to remove a 10 second video because of valuable intellectual property or something. Here’s another video illustrating the same thing but it lacks the pithiness of the original.

UPDATE2 [2019-12-21 Sat 14:07]: Here’s the original video from the tweet. I still find it amazing.

Posted in General | Leave a comment

Emacs As Your Shell

I’m finally finding time to watch more of the videos from the Emacs Conference 2019. Howard Abrams gave an interesting talk on using Emacs as his shell and scripting language. As he says, it’s just him trying out some ideas meant for his personal use.

He starts out with a typical shell pipeline and wonders how the same thing might be done in Emacs. Why would you want to do that? The problem is that a pipeline’s intermediate results are pretty much opaque. You see the input and you see the output but not what’s happening in the middle. By using Emacs, you can see what the data looks like at every step. Abrams also says that in general Emacs tools are superior to the shell so you gain there too. It turns out not to be very difficult to use Emacs interactively to do a task that you might otherwise use a pipeline for. Abrams demonstrates this by using Emacs to do the same thing as his original pipeline.

In the second half of the talk, Abrams considers how you might use Elisp as a scripting language. He puts together some macros and functions that lets him write vaguely shell-like scripts but within Emacs. The advantage to this approach is that he has the full power of Emacs available whenever he needs it.

I can imagine circumstances where I might use Emacs interactively instead of a pipeline but I don’t think I’d bother with the scripting. I’m happy to use a shell script if that’s what the job requires. Still, Abrams’ approach was interesting and definitely worth taking a look at. You might even decided to adopt his approach.

Sadly, there were technical problems and the video feed was lost in the middle of the talk. That’s not as serious as it could have been because it happened near the end of the talk proper and mostly affected the Q&A. Secondly, Abrams has provided a sort of transcript of the talk complete with the slides. That makes it pretty easy to follow along in the part without video. As usual with Abrams’ videos, you’ll probably learns some new things and it is definitely worth your while.

Posted in General | Tagged | Leave a comment

CNBC on Open Source

The Programming subreddit has a pointer to an interesting CNBC report on Open Source. The report is in the form of a video that is just shy of 14 minutes long so while it’s not a comprehensive survey, it’s more than the usual drive-by offering we’ve come to expect from the general press.

The main conclusion is that most of the world now depends on open source software. Even those building or selling proprietary software almost certainly have a large open source component in their products. They tell the story of the Heartbleed vulnerability in OpenSSL and after explaining that it’s used by virtually all Websites note that it was maintained by just a handful of unpaid developers.

The unpaid developer angle is significant because although many companies have figured out how to monetize open source, most of the people doing the actual work are still unpaid. The report notes that the situation is improving because the industry has come to realize how much they depend of the open source developers and they are therefore devising ways to see that they are compensated.

One of the more interesting things I learned from the report is the “velocity” of changes to the Linux kernel. I’ll let you watch the video to find out what exactly that means but it is astounding.

The report is an intelligent and sympathetic look at the open source movement and definitely worth 14 minutes of your time.

Posted in General | Tagged | Leave a comment

Mailfence

Those of you who liked the suggestion from Monday’s post to get rid of your Gmail account may be wondering how to do that. Realistically, if you want an account that isn’t harvesting your data, you’re going to have to pay a small amount for it.

Everyone’s favorite alternative to Gmail seems to be Fastmail. It’s similar in look and feel to Gmail and there are apps for your tablet and smartphone but it comes without the spying. The price ranges from \$3 to \$9 a month mostly depending on how much storage you want.

Recently, I stumbled upon another mail service, Mailfence, that looks promising. If you’re extra paranoid it might be a better fit than Fastmail. For one thing, they are located in Belgium, which has very strict data privacy laws that make it hard for even judges to get at your data. For another, they offer browser based end-to-end encryption so that no one but you and the recipient can read your email. Of course, that requires that your correspondent also support a PGP capable mail reader so the encryption is optional and you can still email your Aunt Millie.

Their prices are comparable to Fastmail’s. They’re priced in Euros but at the time I’m writing this the monthly charge is between \$2.78 and \$27.75. Take a look at their Website to see what you get with the various plans.

As most of you know, I use mu4e to read my mail from my Apple Mail account. Apple’s email is arguably an exception to the “have to pay to be exempt from spying” rule but I do pay a nominal charge for storage some of which is used for email. In any event, that means that I haven’t personally used Fastmail or Mailfence so I’m depending on what others have said, reviews, and their Websites. Caveat Emptor.

Posted in General | Tagged | Leave a comment

Irreal SSL Certificate Update

The new Irreal SSL certificate is (finally) activated. Despite appearances, I’ve been dealing with this since last Friday when I discovered the old certificate had expired. Of course, I got no notification of a pending expiry, just a browser exception when I tried to access the site.

I renewed the certificate immediately but nothing happened. I thought maybe the hosting minions didn’t work on the weekend so I waited until Monday before querying my provider. The process exactly replicated what happened last year but, of course, I’d forgotten the details and had to rediscover them again this year. In any event, the rep I talked to was very helpful and got things taken care of forthwith.

Luciano Passuello told me there’s a better way: I should be using Let’s Encrypt. Perhaps I’ll try that. I don’t really mind paying the nominal renewal fee, I just want the process to be automatic so I don’t have to worry about it and Irreal readers don’t get errors when they try to read my bloviations profound thoughts. In the mean time, I’ve documented the process in my Journal so that whatever course I choose, I’ll be able to get the certificate renewed before the next ice age.

Now back to our normally scheduled bloviations.

Posted in Administrivia | Leave a comment