Wednesday, September 30, 2009

APL jot dot in scheme and php

So I am implementing APL in php... As a matter of practice of course I am prototyping things. The ultimate scheme yields an oo/fluent expression approach but immediately here is how I would implement jot dot in php and scheme:

PHP:

function jotDot($fun, $v1, $v2) {
$result = array();
foreach($v1 as $x) {
$result[$x] = array();
foreach($v2 as $y) $result[$x][$y] = $fun($x, $y);
}
return $result;
}


Scheme:

(define (mapcar func list)
(if (null? list) '()
(cons (func (car list)) (mapcar func (cdr list)))))

(define (jotDot func v1 v2)
(mapcar (lambda (x) (mapcar (lambda (y) (func x y)) v2)) v1))


I am not going to lie - the scheme is definitely more elegant but it is not as readable as the php. I think that is sort of interesting. Maybe mapcar is not the right abstraction for the job and there is a better approach. Oh yeah in APL:

The eventual "fluent expression" APL->php interpreted code to do something like calculating a times table up to 12 would look like:

$V = APL()->indexGenerator(12);
$V->jotDot('times', $V);