SICP : Ex1.4

Scheme

(define (a-plus-abs-b a b)
  ((if (> b 0) + -) a b))

Delphi

function aPlus(x, y: Integer): Integer;
  begin
    Result := x + y;
  end;
function aMinus(x, y: Integer): Integer;
  begin
    Result := x - y;
  end;

function PlusAbs(x, y: Integer): Integer;
  type
    TFunc = function(x, y: Integer): Integer;
  var
    aFunc: TFunc;
  begin
    if y > 0 then aFunc := aPlus else aFunc := aMinus;
    Result := aFunc(x, y);
  end;

Delphiでは、条件を式として使えない(と思う)ので、一旦変数に格納する必要がある。
また、演算子に対するポインタが使えない(と思う)ので、加減算関数を別途定義する必要がある。

C

int aPlus(x, y)
	int x, y;
{
	return x+y;
}
int aMinus(x, y)
	int x, y;
{
	return x-y;
}
int plusAbs(x, y)
	int x, y;
{
	return (y>0?aPlus:aMinus)(x,y);
}

Cでは、演算子に対するポインタが使えない(と思う)ので、加減算関数を別途定義する必要がある。

Concurrent Clean

aPlusAbs :: Int Int -> Int
aPlusAbs x y
	= (if (y>0) (+) (-)) x y