Planet Scheme

September 02, 2010

Joe Marshall

For your amusement...

Some computer Tom Swifties:
  • “I thought I would free up more memory on the second pass,” Tom recollected.
  • “You should always check the pre-conditions,” Tom asserted.
  • “Did you use SSL?” Tom asked cryptically.
  • “If one of those is smaller, put it first,” Tom ordered weakly.
  • “Command continuations discard values,” Tom said expressionlessly.
  • “That isn't null or a cons cell,” Tom said listlessly.
  • “Does this return an answer?” Tom asked haltingly.
  • “This program implicitly depends upon time,” Tom stated.
  • “Maybe it's in Reverse Polish Notation,” Tom put forth.

by Jrm (eval.apply@gmail.com) at September 02, 2010 05:16 PM

September 01, 2010

Grant Rettke

Ready for Service

Yesterday I got the Clymer’s service manual in the mail from Murph’s, so with the Kawasaki service manual already in handy I’m ready for servicing my bike!

by Grant at September 01, 2010 10:11 PM

ocamlnet-3.0.0

I’m very proud to announce Ocamlnet 3.0.0, a completely overhauled version of Ocamlnet.

Wish I had some problems that needed solving with ocamlnet!

(via caml-list)

by Grant at September 01, 2010 09:55 PM

Easy Ways to Fail a Ph.D.

Here is one person’s top ten list of ways to fail to obtain a PhD.

Interesting quotes:

Ph.D. school seems to be a magnet for every kind of procrastinator.

Advisors expect near-terminal Ph.D. students to be proto-professors with intimate knowledge of the challenges in their field. They should be capable of selecting and attacking research problems of appropriate size and scope.

A Ph.D. is a small but significant contribution to human knowledge. Impact is something students should aim for over a lifetime of research. Making a big impact with a Ph.D. is about as likely as hitting a bullseye the very first time you’ve fired a gun. Once you know how to shoot, you can keep shooting until you hit it. Plus, with a Ph.D., you get a lifetime supply of ammo.

It does not matter at all what you get your Ph.D. in. All that matters is that you get one. It’s the training that counts–not the topic.

(via ycombinator)

by Grant at September 01, 2010 09:50 PM

August 31, 2010

Programming Praxis

Data Encryption Standard: Part 1

The Data Encryption Standard has been one of the most successful ciphers in history, and is still in use today, especially in its Triple DES variant. The Data Encryption Standard is officially described by FIPS 46-3, though if you are not fond of reading algorithm descriptions written by government lawyers there are many other descriptions available on the internet.

DES is a block cipher, operating on 64 bits at a time. Here is an example:

PT  P  r  o  g  P  r  a  x
HEX 50 72 6F 67 50 72 61 78
KEY 01 23 45 67 89 AB CD EF
CT  CC 99 EA 46 B1 6E 28 90

There is more than one way to encrypt a message longer than 64 bits; we will examine them in a later exercise.

Your task is to write the code to encipher and decipher a single 64-bit block using the Data Encryption Standard. When you are finished, you are welcome to read or run a suggested solution, or to post your own solution or discuss the exercise in the comments below.


by programmingpraxis at August 31, 2010 09:00 AM

August 30, 2010

Grant Rettke

Updated DrSync to Racket

Just posted the updated version of DrSync for Racket on PlaneT.

by Grant at August 30, 2010 10:26 PM

Installed tailbrights on my Connie

Buck Sport Touring sells reflective stickers called Tailbrights that stick to the rear and side portions of the hard bags. Tonight I installed them. Installation was pretty simple; only had one or two bubbles on each of the stickers. I guess that you could make these yourself; but Buck Sport figured out the right shape, and I like to support companies that fill a niche like they do. The cool thing about them is that the stickers are black; so during the day you don’t even see them.

Here are some photos:

by Grant at August 30, 2010 03:36 AM

August 27, 2010

Programming Praxis

Chinese Remainders

In ancient China, two thousand years ago, a general wanted to count his troops. He first had them line up in ranks of eleven, and there were ten troops left over in the last rank. Then he had his troops line up in ranks of twelve, and there were four left over in the last rank. Finally he had them line up in ranks of thirteen, and there were twelve troops remaining in the last rank.

Your task is to determine how many troops the general had under his command. When you are finished, you are welcome to read or run a suggested solution, or to post your own solution or discuss the exercise in the comments below.


by programmingpraxis at August 27, 2010 09:00 AM

August 26, 2010

Grant Rettke

Barcamp Milwaukee 5 approaches

Barcamp Milwaukee 5 will be held from Saturday October 2, 2010 at 10AM through Sunday October 3, 2010 at 4PM.

Mike plans to present on Clojure, which should be pretty fun to attend.

by Grant at August 26, 2010 01:30 AM

Virtual Gravel Rash

Tonight after removing some of the caked-on dust from under the seat, I decided to investigate the gravel rash on the right of the lower fairing of the bike.

Getting down on my hands and knees to take a look, expecting deep grooves, I was shocked to instead find something like silver paint stuck to the fairing. It was caked on there, maybe one or two millimeters. Using a damp paper towel though I was able to remove all of the shiny crud.

My best guess is that the previous owner rode into something, or something rode into him, that left the material there.

I like easy fixes!

by Grant at August 26, 2010 12:57 AM

August 25, 2010

Grant Rettke

Okasaki’s Purely Functional Data Structures in Typed Racket

A library of purely functional data structures in Typed Racket. Data structures in the library are based on Chris Okasaki’s book Purely Functional Data Structures, work by Phil Bagwell and others

(via racket)

by Grant at August 25, 2010 03:09 PM

Dominique Boucher

A small experiment with HTML5 canvas and Scheme to JavaScript conversion

Lately, I've been playing around with some of the upcoming features of HTML5. Last week, I decided to try the HTML canvas. In order to make a realistic test, I tried to port the parse tree viewer in NuGram IDE, one of Nu Echo's products, to JavaScript and the HTML canvas.

To debug a speech recognition grammar with NuGram IDE, an Eclipse plugin, the developer simply enters a test sentence and get a parse tree in the Interpretation view:


NuGram IDE is written almost entirely in Kawa Scheme. The parse tree viewer does not use any graph layout toolkit, the layout algorithm has been written from scratch in Scheme (about 200 lines of commented code). It was a good candidate for a rewrite in JavaScript. Here is what I ended up with (in Chrome):



The conversion process

I translated the whole layout algorithm by hand. I did not use (or develop) any automatic translation tool. I wanted to see how different the JavaScript code would look like compared to the original Scheme code.

I knew that JavaScript/ECMAScript is one of Scheme's very close cousins, but it was amazingly easy to convert the Scheme code into very similarly looking JavaScript code. For instance, take this procedure definition:
(define (compute-nodes-size! gc tree-node level)
(compute-node-size! gc (get-graph-node tree-node))
(set-level! tree-node level)
(for-each (lambda (child)
(compute-nodes-size! gc child (+ level 1)))
(get-node-children tree-node)))

It translates to the following JavaScript definition:
function computeNodesSize(ctx, treeNode, level) {
computeNodeSize(ctx, treeNode.graphnode);
treeNode.level = level;
treeNode.children.forEach(function(child) {
computeNodesSize(ctx, child, level + 1);
});
}

Granted, it's a very simple example. There were some pieces of code that demanded a more complex rewrite. Uses of the Scheme apply procedure is an example. I had to translate the call
(apply max nodes-y)
to
var maxY = 0;
nodesY.forEach(function(y) { if (y > maxY) { maxY = y; } });

In JavaScript, the max function is not variadic function. It accepts exactly two arguments. It would have been possible to write a function that takes a function of two arguments and turns it into a variadic version of it. But it wasn't worth it, since there was only a single usage of apply and max in the whole algorithm.

Update: I was completely mistaken here, as pointed out by Jon-Carlos Rivera in another post. the max function is variadic. I could instead have written:
Math.max.apply(this, nodesY)
My mistake was to not use apply properly. The first argument to apply must be an object, and not the list of arguments. I should have RTFM before writing such nonsense.

Canvas Support

I tried my parse tree viewer on 3 different browsers (Chrome 5.0.375.29 beta, Firefox 3.6.8, Opera 10.61) on Ubuntu 9.04.

How portable is the resulting widget? Not as much as advertised. I know, HTML5 is not a standards yet, but most of the major browsers claim to support HTML5 canvases. There were two majors aspects that differed between browsers: rendering and user experience.

On the rendering side, Firefox is the worst. Here is what I get:

Some lines appear on top of the nodes (boxes), even though they were drawn before. Opera is not bad, but the texts are not always rendered properly for some font sizes.

On the user experience side, Opera seems the fastest of the three. Chrome comes close. In Firefox, however, the canvas is not redrawn while dragging the parse tree. The parse tree is redrawn only when the button is released.

Conclusion

In conclusion, I've been fairly pleased by the overall experience. The conversion from Scheme to JavaScript was painless, and the result was nice looking. However, and even though it's only based on anecdotal evidence or my lack of experience with the HTML5 canvas, portability is clearly not there yet. My guess is that we'll have to devote a lot of our development time fixing portability issues (we're already spending too much time doing exactly that for plain HTML, right?).

Update: I put the canvas demo online here. Try it and let me know if portability can be improved, if I did things the wrong way, etc.

by Dominique Boucher (noreply@blogger.com) at August 25, 2010 01:47 PM

ECMAScript - turning a binary function into a variadic one

In my previous post, I mentioned that it's easy to take a binary JavaScript function and turn it into a variadic one. Here's the helper function to do that:
function variadic(fun, val0f, val1f) {
return function() {
var len = arguments.length;
if (len == 0) {
return val0f();
} else if (len == 1) {
if (val1f != undefined) {
return val1f(arguments[0]);
}
else {
return arguments[0];
}
} else {
var tmp = fun(arguments[0], arguments[1]);
for (index = 2; index < len; index++) {
tmp = fun(tmp, arguments[index]);
}
return tmp;
}
};
}
The 'variadic' function takes three parameters:
  1. the binary function;
  2. a function that returns the value for the variadic version when called with zero arguments
  3. a function that returns the value for the variadic version when called on a single. If not specified, the first argument to the variadic function is returned as is when called on a single argument.
For example, the summation operator is defined as
var sum = variadic(function(x,y) {return x+y;},
function() { return 0;},
function(x) { return x;});

while the equivalent of Scheme's / function is defined as
var div = variadic(function(x,y) {return x/y; },
function() { throw "not enough arguments to div"; },
function(x) { return 1/x;});
It is now possible to call
div.apply(this, [1,2,3,4,5])
to get 0.008333333333333333.


by Dominique Boucher (noreply@blogger.com) at August 25, 2010 01:42 PM

August 24, 2010

Programming Praxis

Daniel Shanks’ Square Form Factorization Algorithm

In the mid-1970s, Daniel Shanks exploited the binary quadratic forms introduced by Legendre in the late eighteenth century and perfected by Gauss in the early nineteenth century to create a method of factoring integers. Binary quadratic forms are expressions of the form Ax2 + Bxy + Cy2 with A, B and C integers, normally represented as the triple (A, B, C). Binary quadratic forms are often called square forms.

We won’t discuss the mathematics of square forms, but we do need to know how to reduce a square form. A discriminant delta is calculated as Δ = b2 − 4ac. A single reduction step is called a rho reduction, and is given by ρ(a, b, c) = (c, r, (r2−Δ)/4c) where r is that unique integer b mod 2c such that −|c| < r <= |c|, if √Δ < |c|, or √Δ − 2|c| < r < √Δ, if |c| < √Δ.

A square form is in reduced form if |√Δ−2|a|| < b < √Δ; this reduction is accomplished by continually applying ρ until the reduced form is achieved.

Shanks’ method uses two loops. First, an initial square form is calculated, based on the number to be factored, which must be odd, composite, and not a perfect square; the details of that initialization are tedious, and appear in the solution on the next page. Then the square form is repeatedly reduced (this is the “forward” loop) until it is in proper form, which is described below. Then the reduced inverse square root of the square form calculated in the forward loop is reduced in a “backward” loop until a factor is identified when two successive square forms (A, B, C=c2) have the same B; the factor is then c, if c is odd, or c/2, if c is even. The reduced inverse square root of (A, B, C=c2) is initially (−c, B, −cA), which is then put into reduced form as described above.

Shanks defined a square form as proper using a queue of values derived in a rather complicated way. We will use a simpler method known as a sufficient list. Let L be the integer square root of the square root of the discriminant. If a square form (A, B, C) is encountered with |C| < L with C even, or |C| < L/2 with C odd, then C is placed on the sufficient list. Then a square form (A, B, C=c2) is proper if c is not on the sufficient list.

Your task is to write a function to factor integers using Shanks’ square form algorithm. When you are finished, you are welcome to read or run a suggested solution, or to post your own solution or discuss the exercise in the comments below.


by programmingpraxis at August 24, 2010 09:00 AM

Grant Rettke

Wrapping up the 2009-2010 School Year

This past May, I completed the Simulation and Parallel and Distributed Systems class that I was attending. While taking two classes while working full time was challenging; the pure fun of it all more than made up for the challenge! I will have fond memories of that semester for a long time.

I can’t wait to get started with Applied Mathematical Analysis next week.

by Grant at August 24, 2010 03:36 AM