2005-12-05

Hello, J World

Just a week ago I was given a link to dr_klm's article «Programming language J, the introduction»... At the same time appeared a problem easy enough to be written in the newly learned language and, besides, seemed very suitable for J.

The problem.

N teams are playing «What? Where? When?» game. The game usually consists of 25 questions. Each team can give wrong or correct answer. It is required to summarize, that can be done in different ways.

The formal rules. The amount of correct answers is counted for each team. The maximum sum is set to the 100 points. Each team receives points in proportion to the number of given correct answers.

The informal version. We think, it is unfair that the question cost does not depend on its complexity. The more teams gives the right answer for a question, the less scores each of them should receive for it. The simplest case - if only one team has the right answer, then it receives one point. If all the teams answered correctly then the question cost is 0 points. So, it should be the linear approximation between these two points. Then points are added up and normalized at 100, as usual.

The Implementation.

First go I'd like to generate a matrix that best matches the final table of the tournament: rows — questions, columns — teams. The table is filled with ones and zeros depending on how the team answered to this question.

Not that soon, the following line was born:
L=: 2 (>?) 36 25$ 5


Now L is the result of a game, consisting of 25 questions and containing 36 teams. A team can give a correct answer with probability 2/5.

The official results are getting on-the-fly:

Add up:
res:=+/"1


Find the maximum and normalize:
norm=:*&100@%>./


Use:
norm res L


In the informal case the operators of the normalization and of the counting results remain the same, but they must act on the matrix where the question cost is no longer «1». In the case of the linear approximation described above, we have the following simple formula:



n-x
Cost= ------
n-1


where n is number or teams and x is amount of correct answers for the given question.

So:
fairq=:*"1.(#-+/)%(#-1:)


Try this:
norm res fairq L


.... Well, in the case of equal distribution the difference is very unnoticable :)


Finally

The program with the comments is no longer then the problem description itself. If to be honest, I was lazy to write it in Fortran-like language (C, Java, Php...). In the case of J the coding almost was not behind the flight of thougth. I had no time for boring :).

Ranks are really something! H'm... I'm curious, whether they can be beautifully realized in Lisp syntax?

It turned out that participles, forks and hooks have nothing in common with macroses. It is easy to understand by executing several times
#~?100

A couple will be always consist of equal numbers. That's why the only way to create a matrix, filled with random elements, is to use inner property of the operator itself?

Yet, I can fink of no beautiful operator generates a random matrix filled with zeros and ones. It would be something a-la
createL 39 25;<2 5

But the introduction of unpacking operator and operator of access to the list elements is complicating the initial simple formula
p (>?) x y $ q


Working in the Interpreter is very convinient in case of input pure functional definitions. But let's do so:
p=:2
powP=.^&2

After changing p you have to run the whole program from the start. Actually, compilable languages do so.

After typing a formula in the Interpreter it was not so easy to give it a name. In 'norm' and 'fairq' definitions there are «redundant» dots and 'at' signs (@), which are unnecessary when writing an operatior explicitly.

In general, the language is very cool. I finish reading «Easy J». Proceed to «J for C Programmers».

2005-11-23

Macro in Ant

When you write an ant macro, the main problem is inabillity to define temporary property.
    <macrodef name="sample">
<sequential>
<java resultproperty="java.result">
</sequential>
</macrodef>
Property can be set only once, and later nobody can change its value. It is good behaviour,
elsewere we can run into troubles depends on order of target calls.

So, if we call the above macro second time, the value of ${java.result} stays unchanged.

But we can overcome this:
   <macrodef name="sample">
<attribute name="workDir">
<sequential>
<antcall target="internal.java">
<param name="dir" value="@{workDir}">
</antcall>
</sequential>
</macrodef>

<target name="internal.java">
<java dir="${dir}" resultproperty="java.result"/>
</target>
That macro can be called more than one time, and will be works correctly.

2005-11-14

Linux on the Notebook

I helped to a friend to configure fresh-installed Ubuntu Linux. It was long. I haven't seen such problem before. X server starts, no errors in log, but the screen stays blank.

It turned out that is the standard problem for ATI Radeon drivers. Before starting it is necessary to load chipset specific AGP module.

In our case it was intel-mch-agp.

2005-11-12

Linux: MIDI Setup

There is my translated forum message. Of corse it can be extended and improved, but in order just to not forget, i will leave it as is.

For happy owners of sound cards without midi support, there is only one way to get listen midi sound is to install timidity, the software midi emulator.

I want to write about my timidity setup expirience.
$> apt-get install timidity
$> sudo /etc/init.d/timidity start

I saw error, OK, I went to /etc/timidity, and saw that it contains only one line that includes missing file.

I don't remember its name, but just looking on it, helps me to understand that something called freepars is missing. I open synaptic and found that package with exactly same name are exists.
Cool!

Later i saw that even timidity package recomends freeparst to install.

$>apt-get install freepats

One more time:
$> sudo /etc/init.d/timidity start

And one more error.

Once, i don't remember details, but it was realy easy to understood that we need to load some kernel module responsible for midi. But i have to brutforce the what exactly midi module i need to load.

And than, in order to make this module load automatically on start, i wrote it in /etc/modules,

Thats all.

2005-08-29

Installing SLIME

With the magic hook, SLIME could be run on lisp file open.

 ;Lisp enviroment installation
(setq inferior-lisp-program "clisp")
(add-to-list 'load-path "path_to_slime")
(require 'slime)
(slime-setup)

;Making slime connect to your lisp automatically when you open a lisp file.
(defun cliki:start-slime ()
(unless (slime-connected-p)
(save-excursion (slime))))
(add-hook 'slime-mode-hook 'cliki:start-slime)

Few more usefull tips can be found there.

2005-08-04

Linux on the server

Installer, with kernel 2.4 cannot found the CD-ROM. Kernel 2.6 when ACPI is turned on, hangs up in the begining of load. Manufacturer provides drivers for hardware RAID only for Red Hat and Suse with kernels 2.4.11-*. It's horrible.

I was faced with an interesting thing. Although, server have 1Gigs of RAM, kernel writes - "Warning only 896MB will be used".

Problem can be easily solved by recompiling kernel with support for more than 4 Gigs RAM. But it's still interresting - why it not works by default?

Host bridge: Intel Corp. Server Memory Controller Hub (rev 05)
VGA compatible controller: Intel Corp. Graphics Controller (rev 05)
PCI bridge: Intel Corp. 82801FB/FBM/FR/FW/FRW (ICH6 Family) PCI Express Port 1 (rev 03)
USB Controller: Intel Corp. 82801FB/FBM/FR/FW/FRW (ICH6 Family) USB UHCI #1 (rev 03)
USB Controller: Intel Corp. 82801FB/FBM/FR/FW/FRW (ICH6 Family) USB UHCI #2 (rev 03)
USB Controller: Intel Corp. 82801FB/FBM/FR/FW/FRW (ICH6 Family) USB UHCI #3 (rev 03)
USB Controller: Intel Corp. 82801FB/FBM/FR/FW/FRW (ICH6 Family) USB2 EHCI Controller (rev 03)
PCI bridge: Intel Corp. 82801 PCI Bridge (rev d3)
ISA bridge: Intel Corp. 82801FB/FR (ICH6/ICH6R) LPC Interface Bridge (rev 03)
IDE interface: Intel Corp. 82801FB/FBM/FR/FW/FRW (ICH6 Family) IDE Controller (rev 03)
IDE interface: Intel Corp. 82801FR/FRW (ICH6R/ICH6RW) SATA Controller (rev 03)
SMBus: Intel Corp. 82801FB/FBM/FR/FW/FRW (ICH6 Family) SMBus Controller (rev 03)
Ethernet controller: Intel Corp. 82541GI/PI Gigabit Ethernet Controller (rev 05)
PCI bridge: Intel Corp. PCI Bridge Hub A (rev 09)
PCI bridge: Intel Corp. PCI Bridge Hub B (rev 09)