Thoughts
```js
function zip(array1, array2) {
const accum = [];
const shorterLength = Math.min(array1.length, array2.length);
for (let i = 0; i < shorterLength; i++) {
accum.push([array1[i], array2[i]]);
}
return accum;
}
function args (...a) {
return a;
}
function then(...exprs) {
return exprs;
}
function sum(rvalue1, rvalue2) {
return function (env) {
let value1, value2;
if (Object.hasOwn(env, rvalue1)) {
value1 = env[rvalue1];
}else {
value1 = rvalue1;
}
if (Object.hasOwn(env, rvalue2)) {
value2 = env[rvalue2];
}else {
value2 = rvalue2;
}
return value1 + value2;
}
}
function equals(varname, val) {
return function (env) {
if (val instanceof Function) {
val = val(env);
}
env[varname] = val;
};
}
function ret(rvalue) {
return function (env, returnCallback) {
if (Object.hasOwn(env, rvalue)) {
returnCallback(env[rvalue]);
}else {
returnCallback(rvalue);
}
}
}
function defineFunction(args, doSteps) {
const env = {};
// Everything is lazily evaluated until the function is actually called
return function (...a) {
for (const [argname, value] of zip(args, a)) {
env[argname] = value;
}
let shouldReturn = false;
let returnValue = null;
function retCallback (value) {
shouldReturn = true;
returnValue = value;
}
for (const step of doSteps) {
step(env, retCallback);
if (shouldReturn) {
return returnValue;
}
}
}
}
console.log(defineFunction(args("a"), then(equals("b", 5), equals("res", sum("b", "a")), ret("res")))(5));
```
Obviously some big problems but just as a "heh" it does actually work for that example.