Any Haskell puzzlers?

I just watched Joshua Block’s talk Effective Java - Still Effective After All These Years. I’m not a Java developer1 but I still found the talk very interesting. Mr Block offers tips and tricks to deal effectively with a few aspects of Java, and I’m sure many a Java developer out there would find that part very interesting. For me however, the most interesting part was the appetizers and the dessert :-)

The appetizer and dessert consisted of three puzzlers. A puzzler is a piece of code that high-lights some peculiarity of the language or standard libraries. The puzzlers in this talk were are follows:

Simple question

What is printed by the following code, and why?

public class SimpleQuestion {
  static boolean yesOrNo(String s) {
    s = s.toLowerCase();
    if(s.equals("yes") || s.equals("t") || s.equals("y"))
      s = "true";
    return Boolean.getBoolean(s);

  public static void main(String[] args) {
    System.out.println(yesOrNo("true") + " " + yesOrNo("YeS"));


What is the result of the following code, and why?

import java.util.*;

public class Searching {
  public static void main(String[] args) {
    String[] strs = { "0", "1", "2", "3", "4", "5" };

    // Translate string array into list of integer
    List<Integer> ints = new ArrayList<Integer>();
    for(String s : strs)

    System.out.println(Collections.binarySearch(ints, 1, cmp));

  static Comparator<Integer> cmp = new Comparator<Integer>() {
    public int compare(Integer i, Integer j) {
      return i < j ? -1 : (i == j ? 0 : 1);


This one consists of two classes, which are compiled together:

public class PrintWords {
  public static void main(String[] args) {
    System.out.println(Words.FIRST + " " + Words.SECOND + " " + Words.THIRD);
public class Words {
  public static final String FIRST = "the";
  public static final String SECOND = null;
  public static final String THIRD = "set";

Now modify the latter like this:

public class Words {
  public static final String FIRST = "physics";
  public static final String SECOND = "chemistry";
  public static final String THIRD = "biology";

Compile the second version of alone and then run PrintWords, what is the result and why?

Any puzzlers for Haskell?

Of course I couldn’t help but wonder what puzzlers there are for Haskell. Do note though that puzzlers aren’t just obfuscated code; they are readable code that you think does one thing but in reality it does something else. I’d really like to read any Haskell puzzlers you can come up with. Post them on your own blogs, or as comments to this post.

NB I should probably mention that I really don’t want answers to the puzzlers. I’ve watched Josh Bloch’s presentation, and I think anyone interested in finding out should watch it for themselves.

  1. If I ever find myself in a situation that calls for Java I’m very likely to spend some time looking at Scala :-)↩︎

Dan Doel

What are the values of the following expressions?

let ( (w, x), (y, z)) = ((1, 2),    (3, 4)) in y
let ( (w, x), (y, z)) = (undefined, (3, 4)) in y
let (~(w, x), (y, z)) = (undefined, (3, 4)) in y


Dan, a nice little example of the power of irrefutable (lazy) patterns.


let 1 = 2 in print 1


@solrize, hardly the kind of code you’re likely to find in any real software project out there though, is it? :-)


f (n+1) = n
main = do
  putStrLn . show $ f 1
  putStrLn . show $ f 0


Intrigued by the puzzles, I had to look briefly at the video. It is interesting that the conclusion after the first puzzle, @09:34 is that “Strange and terrible methods lurk in libraries”. Huge libraries are a main selling point of Java, especially vs. Haskell, but sometimes there’s something to be said for the DYI approach also - something that “clearly works, as opposed to merely works”.

Leave a comment