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
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:
max(Arry) -> length(longest(Arry)).
longest(Arry) -> head(lists:sort(fun(A,B) -> length(A) > length(B) end, matches(Arry))).
head([H|_]) -> H;
head() -> .
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.