Archive

Archive for the ‘Erlang’ Category

Sessions on new programming languages

July 18, 2012 Leave a comment

Over the past couple of days I attended several OSCON sessions on new (to me) programming languages:

  • CoffeeScript is a scripting language that compiles into JavaScript.  It looks very clean and uses whitespace/indentation just like Python does, which is great.
  • Erlang was not completely new to me, but I had not understood the overall architecture of the Erlang environment and its similarities to an operating systen.
  • Clojure, a Lisp dialect, seems the least practical for day-to-day use, but is also the most different and interesting to me.
Advertisements

Hacker Olympics Puzzle, Part 2

February 14, 2012 Leave a comment

I am trying to teach myself Erlang, so I gave the Hacker Olympics Puzzle another try in a complete new (to me) language.

Here are the functions that do the work, the main program with the code are in a seperate file.  Everything is in a repository on github.

-module(decode).
-export([decode/1]).

decode(Code) -> convert(process(Code)).

process(Code)                            -> process(Code, [], []).
process([], Unique, _)                   -> Unique;
process([X, Y|Code], Unique, Duplicates) ->
    Pair = {X, Y},
    IsDuplicate = lists:member(Pair, Duplicates),
    IsUnique = lists:member(Pair, Unique),
    process_pair(Pair, IsDuplicate, IsUnique, Code, Unique, Duplicates).

process_pair(_, true, _, Code, Unique, Duplicates) ->
    process(Code, Unique, Duplicates);
process_pair(Pair, _, true, Code, Unique, Duplicates) ->
    process(Code, lists:delete(Pair, Unique), [Pair|Duplicates]);
process_pair(Pair, _, _, Code, Unique, Duplicates) ->
    process(Code, [Pair|Unique], Duplicates).

convert(List)               -> convert(List, []).
convert([], Result)         -> lists:concat(Result);
convert([{X, Y}|T], Result) -> convert(T, [cchar(X) + cchar(Y)|Result]).

cchar(C)           -> cchar(C, 0, "qwertyuiop").
cchar(C, I, [C|_]) -> I;
cchar(C, I, [_|T]) -> cchar(C, I + 1, T).
Categories: Erlang, Programming Tags: