Archive for the 'Erlang' Category

06/11 Erlang/gs Event loop

I have now a cool-ish event loop in place that declares a buttonpress/click for each known GUI element. An event loop is created with all elements referenced, and basic messages are implemented [clicks/buttonpresses]. If a pushbutton as “quit” as caption, app termination is provided for. Ditto when closing the window. [Mouse] Button presses provide the number of the mouse button, and the position. What else do ya need? ;-)

Erlang

06/11 Erlang/gs progress

I solved most of my problems with listboxes and grids. I should probably add a couple of other elements, like the editor [aka EditField]. But now is the time to turn to linking the existing elements to events inside an event loop, which looks like this:

loop() ->
    receive
        {gs,_Win,destroy,_Data,_Args} -> bye;
        {gs,_Gridline,click,_Data,[Col,Row,Text|_]} ->
            io:format("Click at col:~p row:~p text:~p~n",[Col,Row,Text]),
            loop();
        Msg ->
            io:format("Got ~p~n",[Msg]),
            loop()
    end.

You set up a receiver and match messages following this pattern: {gs, IdOrName, EventType, Data, Args}

The original in RB’s IDE

The end result in X11

Erlang

06/10 Messing with Erlang/gs

Building GUI apps in Erlang is pretty primitive. And it’s text-only work. As in, you have to hard code GUI elements into a text editor.

W_Window1 = gs:create(window, gs:start(), [{width,789}, {height,544}, {x,100}, {y,100}, {title,"First Try"}]),
CV_Canvas1 = gs:create(canvas, W_Window1, [{y,14}, {x,20}, {width,483}, {height,483}]),
CVP_Canvas1 = gs:create(image, CV_Canvas1, [{'load_gif', "tiger.gif"}]),

Blech…

So I am working on a GUI editor for Erlang, but with a twist I had used before for Python: I am using RB’s own GUI editor to build a nice window, and then I make a call in the window’s Open() event to a library I wrote that outputs Erlang/gs code for said window. The app runs, produces the code, and quits. It’s far from complete, and I am having difficulties with gs grids, aka multiple-column listboxes. But it is already enough to easily produce some skeletton code, and I hope I can then move on to linking GUI elements to code managing events. Like when a pushbutton is clicked, provide basic code to handle this.

More later… But see this already:

The original in RB

The end result in gs/X11

Erlang

04/13 The Morris-Pratt algorithm

I put all the bits together, and it would be too much pretified Erlang code than necessary [and while highlight is nice, it produces monster amounts of HTML/CSS cruft]. Instead, get the code here. That thing seems to work, from preliminary tests – not that I don’t trust either the algorithm or Christian, but rather my Erlang coding skillz… – and I might rewrite that monster in a more user-friendly language, like RB.

Now I should really try and understand how it works. I think I have a vague understanding, but it’d be better if the legs I’m standing on weren’t that shaky…

Update
The code has been updated to reflect changes in the coursework. Apparently I flat-footed on some errors in the algorithms. Happy to be of some help :-)

Erlang

04/12 Beta

Two new Erlang functions here, beta/2 which calculates the β function at offset n, and allBetas/1, which computes all β and returns them in a list [could be a tuple if you want :-) ]

-module(rinder).
-compile(export_all).
beta(X,0) -> -1;
beta(X,1) -> 0;
beta(X,J) ->
 I= 1+beta(X,J-1),
 Cond=element(I,X),
 Cond2=element(J,X),
 case Cond of
  Cond2 -> I;
  _ -> beta(X,I)
 end.

allBetas(X) ->
 XX=list_to_tuple(X),
 allBetas(XX,[-1,0],2,size(XX)).
allBetas(X,Acc,J,M) when J>M->
 Acc;
allBetas(X,Acc,J,M) ->
 I=1+beta(X,J-1),
 Cond=element(I,X),
 Cond2=element(J,X),
 case Cond of
  Cond2 ->
   allBetas(X,lists:flatten([Acc,I]),J+1,M);
  _ ->
   allBetas(X,lists:flatten([Acc,element(I+1,list_to_tuple(Acc))]),J+1,M)
 end.

log_to_screen(A,B) -> io:format(A,B).

 rinder:beta(list_to_tuple("abacabac"),3).
1

Yes I know too lazy to provide a conversion from list to tuple. Next time…

 rinder:allBetas("abacabac").
[-1,0,0,1,0,1,2,3,4]

which thankfully matches what Christian calculated…

Since Erlang’s tuples and lists start at 1, the line
allBetas(X,lists:flatten([Acc,element(I+1,list_to_tuple(Acc))]),J+1,M)
has I + 1 instead of i in the algorithm (else β[j] < -- β[i]).
Erlang