Erlang vs. Haskell
Well, I’m on the Erlang train, now. It’s really hard to resist some of the platform’s features; in particular:
- The dead-simple threading and message passing
- The syntactic sugar for binary matching
- The VM, which provides support for dynamically modifying code at runtime
- All of the support for supervisors, which provides massive reliability with minimal effort
What I don’t like is the syntax. Erlang is a horrible, horrible language when it comes to syntax. It’s verbose, and awkward, especially when coming from a language like Haskell. Take, for example, the following code, which finds the longest prefix that is also a suffix in a string (where prefix is defined as any sequence of characters including the first but excluding the last, and suffix as any sequence of characters including the last but excluding the first)
import List as D
longest arry = head $ D.sortBy (x y -> compare (length y) (length x)) (matches arry)
matches x@(a:as) = D.intersect (substrs x) (map reverse (substrs (reverse as)))
where substrs xs = [ take n xs | n <- [1..length xs]]
Pretty simple, eh? Here’s the exact same algorithm in Erlang:
-module(presuf).
-export([longest/1, max/1]).
max(Arry) -> length(longest(Arry)).
longest(Arry) -> head(lists:sort(fun(A,B) -> length(A) > length(B) end, matches(Arry))).
head([H|_]) -> H;
head([]) -> [].
matches(Xs) ->
Pres = substrs(1,Xs),
Sufs = lists:map( fun lists:reverse/1, substrs( 1, lists:reverse(Xs) ) ),
sets:to_list( sets:intersection( sets:from_list(Pres), sets:from_list(Sufs) ) ).
substrs( N, Xs ) when N < length(Xs) -> [lists:sublist( Xs, N )|substrs(N+1, Xs)];
substrs( _, _ ) -> [].
Pretty ugly. Despite this, the other platform features are, as I’ve said, irresistible, so I’m trying to wrap my head around the stupid syntax, crossing my fingers, and hoping for the best.