2010-08-13, 15:30
Just in case you ever need one:
xmlCharDeref :: String -> String
xmlCharDeref [] = []
xmlCharDeref ('&':'#':'x':r) = let
(digits, remainder) = span (/= ';') r
c = chr (read ("0x" ++ digits))
in
c : xmlCharDeref (tail remainder)
xmlCharDeref ('&':'#':r) = let
(digits, remainder) = span (/= ';') r
c = chr (read digits)
in
c : xmlCharDeref (tail remainder)
xmlCharDeref (c:r) = c : xmlCharDeref r |
In ghci:
*Foo> xmlCharDeref "hello there"
"hello there"
*Foo> xmlCharDeref "hello there"
"hello there"
*Foo> xmlCharDeref "hello2there"
"hello2there" |
2010-08-01, 18:24
I just watched Joshua Block’s talk Effective Java – Still Effective After All These Years. I’m not a Java developer 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"));
}
} |
Searching
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)
ints.add(Integer.valueOf(s));
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);
}
};
} |
PrintWords
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 Words.java 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.