PHP 72
Apparently I completely missed the point of this code-golf. I'm STILL not sure if I understand it correctly. Is no output required? Can I assume the methods of g and h? Here's a new version based off the old one:
<?=$f=function($g,$h,$x){return$k=($g[0]*$h[0]*$x)+($g[0]*$h[1])+$g[1];}
If this is still wrong, I give up.
PHP 176
The requirements are kind of confusing, but here's what I think you meant:
<?=f($argv);function f($a){$x=$a[3];$g=explode("x",$a[1]);$h=explode("x",$a[2]);return$k=($g[0]*$h[0])."*$x+".(($g[0]*$h[1])+$g[1])."=".(($g[0]*$h[0]*$x)+($g[0]*$h[1])+$g[1]);}
Usage: php f.php 2x+3 3x+4 5
Output: 6*5+11=41
Another Example
Usage: php f.php 4x-7 3x+4 2
Output: 12*2+9=33
Method
As you described, we can't simply output the answer with g(h(x))
, we had to create function f(g,h)
which determines what both the functions would be together, and return that as a new function: k(x)
.
Is this what OP meant?
Ungolfed
<?php
function f($a) {
$x=$a[3];
$g=explode("x",$a[1]);
$h=explode("x",$a[2]);
$k = $g[0]*$h[0];
$k .= "*";
$k .= $x;
$k .= "+";
$k .= ($g[0]*$h[1])+$g[1];
$k .= " = ";
$k .= ($g[0]*$h[0]*$x) + ($g[0]*$h[1])+$g[1];
return $k;
}
echo f($argv);
Yes. There is a way to extract those two constants without accessing the AST. – Joe Z. – 2013-04-04T18:04:03.837
May I suggest clarifying that the return value is of function type as well, as I'm guessing is your intent? Two of the current answers return strings. – Kevin Reid – 2013-04-04T21:31:09.017
@KevinReid Done. – Joe Z. – 2013-04-04T23:03:54.877
I'm thinking of rewarding a bounty for an answer in binary lambda calculus... – Joe Z. – 2013-11-19T22:08:26.840
1I don't understand how you want the output. If it's as a function, you could just black-box compose the inputs. Do you want the pair
(a,b)
for whichk(x)=ax+b
? A function that must be written in the formk(x)=ax+b
? If the second, doa
andb
have to be given explicit assignments in that function? – xnor – 2014-05-22T18:07:16.983