## Knight or Knave or Sleepwalker?

Welcome to the Island of Knights, Knaves, and Sir Mix-a-lot! There are a few things you should know about this island:

• Knights always tell the truth
• Knaves always lie
• There is exactly one Knight named “Sir Mix-a-lot”. Like every other knight, he cannot lie.

Today, we are on the island of sleepwalking knights and knaves. At night, half of the population sleepwalks around the island. When a resident sleepwalks, they are living in a dream world where everything is the opposite of reality. In other words, every belief held by a sleepwalking resident will be wrong. For example, if you ask an awake knight if he is a knight, he will say “yes”. If you ask a sleepwalking knight if he is a knight, he will say “no”! He is sleepwalking, so he believes he is a knave. Since he is actually a knight, he answers all questions truthfully, so he will tell you that he is actually a knave!

Let’s say you run into a resident of this island during the night. Last time, you wanted to know if someone was a knight or a knave. Now you want to know if this other resident is sleepwalking or not. Can you figure it out in one question?

#### Solution

One possibility is “Are you a knight?” An awake knight will tell the truth and say “yes.” An awake knave will lie and also say “yes.” Easy enough so far, and we now know that no awake resident can say “no.” But what do sleepwalkers say?

A sleepwalking knight will believe himself to be a knave. When asked if he is a knight, he will tell you the truth about that belief and say “no.” A sleepwalking knave will believe himself to be a knight. He will then lie to you about that belief and will also say “no.” So a “yes” answer means that person is awake, and a “no” answer means that person is sleepwalking. We don’t know if we are talking to a knight or a knave, but we do know if that person is sleepwalking or not.

## Functions from Functions

I’ve been going through The Little Schemer recently. Many of the concepts in the book are simple and yet mind-blowing at the same time. One idea that I found particularly enlightening is that of functions returning other functions.

Here’s an example from page 127:

Lo and behold, a function that returns a function. What function does it return? Let’s use it and find out. The outer lambda is a function that takes a single argument, so let’s give it one.

This returns a function that takes a single argument. This returned function will test its argument to see if it is equal to ‘salad or not. Essentially, it returns the function:

So, how is this helpful? This example is not particularly useful in a practical sense, but it introduces the idea of functions returning functions, and sets up the introduction to the idea of passing functions to other functions as arguments. It’s at this point the book increases its rate of MBPP* significantly.

* mind blowings per page

## Even More Hash Defaults

Let’s create a hash object in ruby!

Nice. Did you see what I did there? I passed in  Time.now as the default object for our hash. Let’s try it out.

This might not be what you expected. Every time we try a missing key, we’re getting a Time  object as the default object all right, but it’s stuck as one specific time. It turns out that this time is the exact moment I created this hash. Time.now  returned a Time  object, and that is the same time object we are getting every time the default object is returned by the hash. What if we wanted to get the current time, updated every time we tried to use the hash? We could do it like so:

Much better. In this case, another_new_hash  was given a block as its default object. That block will be executed each and every time the default object is needed, so we get a new and updated Time.now  each and every time. This is useful when needed, but it’s also a good reminder to be careful with blocks as default objects if the code within that block is slow or resource intensive in any way.

## Sleepwalkers!

Welcome to the Island of Knights, Knaves, and Sir Mix-a-lot! There are a few things you should know about this island:

• Knights always tell the truth
• Knaves always lie
• There is exactly one Knight named “Sir Mix-a-lot”. Like every other knight, he cannot lie.

A little known fact about the island of Knights, Knaves, and Sir Mix-a-lot is that it is part of a chain of islands. The knights and knaves on each island have peculiar traits. Today, we are on the island of sleepwalking knights and knaves! At night, half of the population sleepwalks around the island. When a resident sleepwalks, they are living in a dream world where everything is the opposite of reality. In other words, every belief held by a sleepwalking resident will be wrong. For example, if you ask an awake knight if he is a knight, he will say “yes”. If you ask a sleepwalking knight if he is a knight, he will say “no”! He is sleepwalking, so he believes he is a knave. Since he is actually a knight, he answers all questions truthfully, so he will tell you that he is actually a knave! Knaves will behave in a similar manner.

Let’s say it is nighttime on this particular island, and you run into a resident walking down the street. You want to know if this resident is a knight or a knave. How many questions do you need to ask to find out?

#### Solution

You can find this out in one question. One that works is “Are you sleepwalking?” Let’s examine why:

If you ask this question to an awake knight, he will truthfully answer “no”. If you ask a sleepwalking knight, he will also answer “no”! Remember, all of his beliefs are false. He does not believe he is sleepwalking, and since he is a knight he will truthfully (according to his beliefs) answer “no”.

An awake knave knows he is not sleepwalking, so he will lie to you and answer “yes”. A sleepwalking knave will also (wrongly) believe that he is not sleepwalking, so he will lie to you and answer “yes”, as well. So in this case, a “no” answer means you are talking to a knight and a “yes” answer means you are talking to a knave.  Notice that even though you now know if you are talking to a knight or a knave, you still don’t know if that person is sleepwalking or not.

## Command Line Window

Let’s imagine you had some sort of file open in a vim buffer. With me so far? Good. Let’s further imagine that you wanted to indent by one level every line that starts with the text “begin”, whether it has leading whitespace or not. Let us also imagine that you are as terrible at writing regular expressions as I am, so you will probably mess up on at least the first try.

Let’s try :g/\s+begin/normal >>. Surprise surprise, it didn’t work. We only changed lines that have leading whitespace before the “begin”. If we replace that + with a *, it should work. So, do we have to type that out again? We could, but what if we were using a much more complicated regular expression? That could be tedious and error prone. Instead, let’s use the command line window.

Pressing q:  in normal mode takes us to the command line window. You may have seen this window accidentally — the command is pretty similar to :q .

The command line window contains a history of our recent command line commands. Two attributes of this window that will help us in this situation are –

• You can move around and edit text in this window like any other buffer
• Pressing enter on any line in this window will execute that line

So how does this help us? Well, instead of retyping our entire command to make one small change, we can instead open the command line window, find our failed regular expression, make the single change we need to make, and press enter to run the now correct command! You sit back, proud of your accomplishment. You have mastered the command line window in vim, and are one step closer to the completion of your master plan. They all doubted you! They said you were mad! Well, could a madman master vim’s command line window? COULD HE?!?

You let out a maniacal laugh as you shake your fist at the heavens.

## MzScheme

Don’t ask why, but I tried to compile vim with support for MzScheme yesterday. The attempt was… misguided. I got close, but it didn’t quite work.

That said, there is a lack of information out there about how to accomplish this, so I thought I’d share the steps I tried in case someone else feels brave enough to carry on.

I used homebrew as a package manager, and I was running OSX 10.9.

• Step 1: Install racket with  brew install plt-racket
• Step 2: In your shell, set the environment variable PLTHOME  to the path of the newly installed plt-racket.
• Step 4: Find the lib directory in the download from step 3. It should contain Racket.framework. Copy that file into /System/Library/Frameworks.
• Step 5: Run brew install vim --with-mzscheme . Tag --verbose  to the end of that if you want to keep score at home.

After these steps, vim “successfully” compiled, and +mzscheme was staring back at me in the version information. All was not well, however. Vim would prompt me to press enter to continue at bizarre times, like after leaving insert mode. Buffers behaved oddly at times. Fugitive buffers would fill with seemingly random strings of ascii characters. I don’t think I lasted ten minutes before I pulled the abort lever with brew remove vim . I’m pretty sure this didn’t work because I was trying to use a recent version of Racket in place of the older MzScheme. Perhaps one day I’ll try again using an older version.

I don’t regret trying, however. For ten minutes, I could write functions in vim using scheme. When it was over, as the sadness enveloped me, I sank to the ground and whispered–

“Never regret thy fall,
O Icarus of the fearless flight
For the greatest tragedy of them all
Is never to feel the burning light.

## He cannot lie

Welcome to the Island of Knights, Knaves, and Sir Mix-a-lot! There are a few things you should know about this island:

• Knights always tell the truth
• Knaves always lie
• There is exactly one Knight named “Sir Mix-a-lot”. Like every other knight, he cannot lie.

You run into three inhabitants on the island. Earlier in the day, a friend of yours (a knight, so we know his statements to be true) told you that one of these three is Sir Mix-a-lot. They make the following statements to you:

A: At least one of the three of us is a knave
B: C is a knight.

Keeping in mind that there is exactly one knight named Sir Mix-a-lot, which one of the three is he?

#### Solution

First, we need to figure out what A is. Let’s say he is a knave and is therefore lying. That would make his statement about at least one of them being a knave true, so we have a contradiction. It’s impossible for A to be a knave in this case, so he must be a knight. That means that either B or C (or possibly both) has to be a knave.

With that in mind, let’s look at B’s statement. He claims that C is a knight. If that’s true, then that makes B a knight as well for making a true statement. We know that can’t be the case, however, since we know from A that at least one of them is a knave. B’s statement has to be false, because if it were true it would be a contradiction. We now know B to be a knave and the statement he made to be false. Therefore B and C are both knaves, and A is the knight named Sir Mix-a-lot.

## Backspace

The single thing I disliked the most about vim at first was the strange way it treated the backspace key. Specifically, I could not backspace beyond the point where I entered insert mode. This happened to me a lot. I would hit backspace multiple times, furrowing my brow in an effort to comprehend why it wasn’t working. I would then grunt and lurch around the room, throwing heavy objects about.

I realize that there are more vim-like ways of deleting characters, and if you’re hitting backspace repeatedly to delete a bunch of characters there is probably a better and easier way to accomplish the same thing in vim. Sometimes, though, you just want backspace to work like it does in every other program written since the dawn of time. You can accomplish this by putting set backspace=start  in your .vimrc. This will allow you to backspace past the point where you entered insert mode. To see the other settings for this option, use :help 'backspace' . Note the single quotes, they are necessary here.

## Non-Recursive Macros in Vim

Earlier this week I read a blog post from thoughtbot about recursive macros in vim. As cool as they look, I almost never use them. I have nothing against recursive macros, but I think it’s important to note a few other ways to replay a macro multiple times in vim.

Let’s look at the problem as presented in the blog post. We have a file that consists of a list of dates like such:

We want to change the file so that each line contains the date in two formats:

If that’s all that’s involved, a recursive macro is a pretty good way to go. But what if our macro doesn’t apply to each and every single line in a file? What if, perchance, our consecutive lines of dates were occasionally interrupted by medieval English poetry?

That’s a goofy example, but it gets the point across. Our large list of dates could have multiple lines with comments, or blank lines, or any other number of things that could cause our macro to fail. As soon as a motion fails (in the case of the macro presented in the blog post, this would be the motion F/ ), the macro will end and we’ll have to restart it. If that happens more than a few times, things can get pretty tedious. So what do we do? I’m going to highlight two different ways of running a macro multiple times.

The first is fairly simple. The command to invoke a macro in vim can take a count. If the macro we want to run multiple times is in the q register, we can use <count>@q  to run the command <count> number of times. The upside to this technique is that we can control the number of times a macro is run. There are a few downsides, though. We still need to have the macro set itself up to run the next iteration of itself, so we really don’t save any work compared to a recursive macro. This method will also abort if it runs into an error like a failed motion, just like recursive macros do. Again, we have just as much work to do after a failed macro as we do with a recursive macro, if not more. Running a macro multiple times by giving it a count is the most common way I use macros because it’s easy and quick when I need to use it on just a few lines or a few regions, but for our particular problem, it’s less than ideal.

Luckily for us, there is a way we can run the macro exactly once for the entire file no matter how many interruptions there are. Not only that, but we can simplify the macro at the same time. Here is the macro as presented in the original blog post:  y\$A<Space>:<Space><Escape>pF/r-;.^j@q . It yanks, puts, and then reformats the date. Our new macro would look the same, except for the last four characters — moving the cursor to the beginning of the next line and invoking the macro recursively. We don’t need them. It’s a small simplification, but not totally insignificant, especially if setting ourselves up for the next macro was more involved than just going to the beginning of the next line.

So now we’ve got a macro that doesn’t set itself up for the next iteration. We can only run it one line at a time. So how do we run this on the entire file? Like so:  :g/^\d/normal @q . Let’s unpack this. The :g command takes a pattern and a command. For every line that matches the pattern, the command is run on that particular line. Our pattern here will match every line that starts with a number. The rest of the command invokes the normal mode command @q — our macro. Our macro runs on every matched line, and we only needed to do it once.

## The Viminfo File

You may have noticed that vim “remembers” some things from session to session. Lowercase marks don’t persist by default, but uppercase marks do. Your command history and the contents of your registers also persist by default. But how much history do they keep? For how many files? Can we adjust these settings? Can we have vim persists other things from session to session? How many Emmys did Angela Lansbury win for her role as Jessica Fletcher in Murder, She Wrote?

The answers are lots, a bunch, yes, yes, and zero.

We can adjust this stuff using the .viminfo file. The viminfo  option takes a string that follows a particular format. Depending on what you put in this string, you can affect the types and amount of information vim stores in the .viminfo file. For example, including the character '  followed by a number in your viminfo option will instruct vim to keep save local marks between sessions for the number of files indicated.

This topic is too big for one post, but be sure to check out :help viminfo , :help viminfo-file , and :help 21.3  for more information.