As of the latest version of Google Maps app (for Android, at least), you can finally set up your navigation to use metric! Yay!

Sometimes, less customisation is _not_ more

In recent years, there’s a trend towards providing less and less customisation options in programs, with a view towards getting the defaults “right” in the first place. Unfortunately, there’s a class of settings for which there is no single “right” answer, and customisation is necessary: locale-related settings.

  • I live in the US, but I’m a New Zealander and I resent having to use US customary units (yes, I have to convert all the road signs to metric in my head, and I’m used to it, but the less of it I have to do, the better, you know?). It really frustrates me that there’s still no sane way to get Google Maps to use metric, and it’s a key reason why I think the mainstream GPS navigation products still have a use. (Update: there now is, finally, a way to get Google Maps to use metric!)
  • Date formatting is another pitfall. My preferred date format is %d/%m/%Y %H:%M:%S or %Y-%m-%d %H:%M:%S (where %H is the 24-hour hour counter, for people unfamiliar with strftime format strings), yet some apps (like HipChat) likes to enforce the use of 12-hour time everywhere, even when my phone’s locale settings is explicitly set to use 24-hour time only (as you may have noticed from the numerous phone screenshots on this blog).
  • The Amazon MP3 Android app looks at your language settings to decide which country you’re in. This is really silly, because I use UK English for UI (yes, I’d really much rather see -ise rather than -ize, and colour instead of color, and enrol instead of enroll, etc.), but I live in the US and have US credit cards, and so need to purchase everything from US Amazon.

Really, these kinds of settings are not as trivial as whether trimURLs is enabled (though it does irk me that Chrome doesn’t offer a way to turn that off). There is serious usability impact, for people who don’t use the same units (or date/time formats) as “most people in the US”. Imagine what response I’d get if I wrote a program that only used %Y-%m-%d %H:%M:%S timestamps and metric units, in the name of “sane defaults for the majority of the (English-speaking) world”.

Yes, I get that i18n is not a priority for many early-stage products. But seriously, Google Maps is by no means an early-stage product, and it is used by people all over the world. So frankly, I am surprised that they still haven’t got with the programme yet.

I really would prefer to use NZ English for UI, though, since I still primarily use $ rather than £. I wish more programs offered that option.

My favourite Scheme feature: named `let`

Recently, on Stack Overflow, there was a discussion on what named let was all about, so I thought I’d write a post on the topic.

What’s a named let?

So, let’s consider a function for merging two sorted lists of numbers (since I felt that factorial or fibonacci were way overused as examples of self-recursive functions):

(define (merge lhs rhs)
  (cond ((null? lhs) rhs)
        ((null? rhs) lhs)
        ((< (car rhs) (car lhs))
         (cons (car rhs) (merge lhs (cdr rhs))))
        (else
         (cons (car lhs) (merge (cdr lhs) rhs)))))

This is fine and good, but some people look at this and say, OMG, this isn’t tail-recursive, so if given long lists, it’ll blow the stack! Okay, fine. Let’s write a tail-recursive version:

(define (merge lhs rhs)
  (define (loop result lhs rhs)
    (cond ((null? lhs) (append-reverse! result rhs))
          ((null? rhs) (append-reverse! result lhs))
          ((< (car rhs) (car lhs))
           (loop (cons (car rhs) result) lhs (cdr rhs)))
          (else
           (loop (cons (car lhs) result) (cdr lhs) rhs))))
  (loop '() lhs rhs))

This is also fine, but now you had to write an inner function. And of course, whenever you’re creating a function for the express purpose of immediately calling it, guess what the answer is? :-)

(define (merge lhs rhs)
  (let loop ((result '())
             (lhs lhs)
             (rhs rhs))
    (cond ((null? lhs) (append-reverse! result rhs))
          ((null? rhs) (append-reverse! result lhs))
          ((< (car rhs) (car lhs))
           (loop (cons (car rhs) result) lhs (cdr rhs)))
          (else
           (loop (cons (car lhs) result) (cdr lhs) rhs)))))

That is what a named let is.

How do you define named let?

Many Scheme implementations implement let as a macro that creates a lambda and calls it, rather than as a built-in.

An unnamed let is easy to implement:

(define-syntax let
  (syntax-rules ()
    ((let ((id value) ...)
       body ...)
     ((lambda (id ...)
        body ...)
      value ...))))

A named let is similarly easy; the lambda is simply wrapped in a rec:

(define-syntax let
  (syntax-rules ()
    ((let tag ((id value) ...)
       body ...)
     ((rec tag (lambda (id ...)
                 body ...))
      value ...))))

All the rec does here is to give the lambda a name that can be used within the lambda for self-recursive calls.

But what is rec?

rec is a macro that wraps up a letrec (or an internal definition) in a pretty way. You can implement it in terms of either letrec:

(define-syntax rec
  (syntax-rules ()
    ((rec name value)
     (letrec ((name value))
       name))))

or an internal definition:

(define-syntax rec
  (syntax-rules ()
    ((rec name value)
     (let ()
       (define name value)
       name))))

In fact, in Scheme implementations where letrec is implemented as a macro on top of internal definitions (or the other way around), the two may well expand to identical code.

rec also provides syntactic sugar for defining a recursive function, much like define does:

(define-syntax rec
  (syntax-rules ()
    ((rec (name . args)
       body ...)
     (rec name (lambda args
                 body ...)))))

Putting it all together

(define-syntax rec
  (syntax-rules ()
    ((rec (name . args)
       body ...)
     (rec name (lambda args
                 body ...)))

    ((rec name value)
     (letrec ((name value))
       name))))

(define-syntax let
  (syntax-rules ()
    ((let ((id value) ...)
       body ...)
     ((lambda (id ...)
        body ...)
      value ...))

    ((let tag ((id value) ...)
       body ...)
     ((rec (tag id ...)
        body ...)
      value ...))))

How to use Dvorak or Colemak on Ice Cream Sandwich (Android 4.0)

Recently, Google released the Google Keyboard app, which provides all the functionality of the built-in keyboard from Jelly Bean. So, just install that app, then follow the instructions I wrote for Jelly Bean. It’s as simple as that!

Here’s a screenshot of my Nook HD+ using the Dvorak layout:

Nook HD+ with Dvorak

Bullseye (Google Code Jam 2013, round 1A, problem A)

I only solved one problem in GCJ 2013 round 1A, and that’s problem A, Bullseye. I really liked my solution to this problem (written in Racket):

(define (rings r t)
  (quotient (- (integer-sqrt (+ (* 4 r r) (* -4 r) (* 8 t) 1))
               r r -1)
            4))

Is the problem really that simple? Why, yes! It’s much simpler than many other entries I’ve seen, so I wanted to explain my solution approach.

So, let’s look at the problem step by step:

  1. You are given t units of paint, and each unit of paint fills in a circle of radius 1.
  2. This means that it takes 4 units of paint to fill in a circle of radius 2, 9 units for radius 3, etc., and generally, n² units to fill in a circle of radius n.
  3. For a black ring with inner boundary r, and outer boundary r + 1, the total units used is, thus, (r + 1)² - r², or fully expanded as 2r + 1.
  4. The next black ring has inner boundary r + 2, and outer boundary r + 3. More generally, for ring i, the inner boundary is r + 2i - 2, and the outer boundary is r + 2i - 1.
  5. The total paint used for the first k rings is thus (2r + 1) + (2r + 5) + (2r + 9) + … + (2r + 4k - 3).
  6. This is the same as k(2r - 3) + 4(1 + 2 + … + k), or in other words, k(2r - 3) + 2k(k + 1), which then becomes 2k² + (2r - 1)k.
  7. You want to find the largest k is that satisfies 2k² + (2r - 1)kt, or in other words, 2k² + (2r - 1)k - t ≤ 0.
  8. Solving this is easy: just use the quadratic formula! In this case, the factors used are a = 2, b = 2r - 1, and c = -t. Plugging this into the quadratic formula gives ((1 - 2r) ± sqrt((2r - 1)² + 8t)) / 4.
  9. We only care about the positive (you can’t have a negative number of rings), so this simplifies to (sqrt(4r² - 4r + 8t + 1) - 2r + 1) / 4, which is almost exactly the code you see.
  10. Finally, we are asked to disregard partial rings, so we simply floor the result. Done.

Anonymous said: why haven't you set your tag as "ruby on rails" or "rails" but only "ruby" on SO? Anything interesting ?

On SO, or SO Careers? To me, I’m interested in Ruby the language. I use Rails at work, but I don’t have a special interest in it; it’s just another framework (albeit a very popular one).

Anonymous said: what's your name?

My name is Chris Jester-Young. You can also find this by clicking “About me” on the site. :-)

Whoa, people really keep their work code after leaving?

Apparently, I just read that some people actually keep their work code after they leave a company. Really?!

Maybe because I’m so gung-ho about copyright, I would never consider such a thing. Even having read that thread, I still would never consider such a thing. When I quit a job, I do not retain work code for any purpose. If I had any work code on my personal machine, I would wipe the machine clean before moving on.

My personal sense of ethics would not permit otherwise.

I’ve written some stuff for work that I would consider pretty technically impressive. But the past is the past; I know that I will write even better things in future. Tomorrow is another day.

Anonymous said: Hey, this is Patrick, the red-neck from Datomic yesterday, where did you take your mensa exam? Not important that you answer. Just saying hey. You are an interesting dude.

Hi Patrick! Wow, sorry for the late reply! I haven’t checked my Tumblr inbox for a long time, it seems.

I took my Mensa exam in Auckland, back in September 2004. Things may or may not have changed since then. You should definitely take the exam, though! It’s fun. :-)

How to use Dvorak or Colemak on Jelly Bean (Android 4.1)

Prior to Jelly Bean, if you wanted to type on your Android device using Dvorak or Colemak, you’d have to use a third-party keyboard app. (My favourite is SwiftKey, by the way.) But from Jelly Bean onwards, Dvorak and Colemak support is built in! No more third-party apps required! Here’s how:

  1. Hold down the comma key, then select “Android keyboard settings”.

    Input options

  2. Scroll to the bottom and choose “Advanced settings”, then “Custom input styles”. (On factory default settings, the German QWERTY and French QWERTZ layouts are already present. You can remove them, of course.) Choose “Add style”.

    Custom input styles

  3. Choose your desired language and layout, then add it.

    Add style

  4. You’ll now be prompted to enable it.

    Before enabling new layout

  5. Untick the “Use system language” box, then tick the Dvorak or Colemak layout that you’ve chosen. Finally, you may also wish to untick the QWERTY layout if you never use it.

    After enabling new layout

  6. That’s it! Your shiny new Dvorak or Colemak layout is ready for you to use.

    Ooh shiny!